@@ -11,6 +11,7 @@ import {
1111 ON_CURRENT_TIME_UPDATE ,
1212 ON_VOICE_PLAYER_PAUSE ,
1313 ON_VOICE_PLAYER_PLAY ,
14+ RESET_AUDIO_UNIT ,
1415 SET_CURRENT_PLAYER ,
1516} from './dux/actionTypes' ;
1617import {
@@ -72,7 +73,7 @@ export const VoicePlayerProvider = ({
7273 }
7374 } ;
7475
75- const pause = ( groupKey : string | null ) => {
76+ const pause = ( groupKey : string | null ) => {
7677 if ( currentGroupKey === groupKey && currentPlayer !== null ) {
7778 logger . info ( 'VoicePlayer: Pause playing(by group key).' ) ;
7879 currentPlayer ?. pause ( ) ;
@@ -100,7 +101,7 @@ export const VoicePlayerProvider = ({
100101 }
101102
102103 logger . info ( 'VoicePlayer: Start getting audio file.' ) ;
103- new Promise < File > ( ( resolve ) => {
104+ new Promise < File > ( ( resolve , reject ) => {
104105 voicePlayerDispatcher ( {
105106 type : INITIALIZE_AUDIO_UNIT ,
106107 payload : { groupKey } ,
@@ -128,62 +129,74 @@ export const VoicePlayerProvider = ({
128129 } ) ;
129130 resolve ( audioFile ) ;
130131 logger . info ( 'VoicePlayer: Get the audioFile from URL.' ) ;
131- } ) ;
132- } ) . then ( ( audioFile : File ) => {
133- const voicePlayerRoot = document . getElementById ( VOICE_PLAYER_ROOT_ID ) ;
134- logger . info ( 'VoicePlayer: Succeeded getting audio file.' , { audioFile } ) ;
135- const currentAudioUnit = audioStorage [ groupKey ] || AudioUnitDefaultValue ( ) as AudioStorageUnit ;
136- const audioPlayer = new Audio ( URL ?. createObjectURL ?.( audioFile ) ) ;
137- audioPlayer . id = VOICE_PLAYER_AUDIO_ID ;
138- audioPlayer . currentTime = currentAudioUnit . playbackTime ;
139- audioPlayer . volume = 1 ;
140- audioPlayer . loop = false ;
141- audioPlayer . onplaying = ( ) => {
142- logger . info ( 'VoicePlayer: OnPlaying event is called from audioPlayer' , { groupKey, audioPlayer } ) ;
143- voicePlayerDispatcher ( {
144- type : ON_VOICE_PLAYER_PLAY ,
145- payload : { groupKey, audioFile } ,
146- } ) ;
147- } ;
148- audioPlayer . onpause = ( ) => {
149- logger . info ( 'VoicePlayer: OnPause event is called from audioPlayer' , { groupKey, audioPlayer } ) ;
150- voicePlayerDispatcher ( {
151- type : ON_VOICE_PLAYER_PAUSE ,
152- payload : { groupKey } ,
153- } ) ;
154- } ;
155- audioPlayer . ontimeupdate = ( ) => {
132+ } )
133+ . catch ( reject ) ;
134+ } )
135+ . then ( ( audioFile : File ) => {
136+ const voicePlayerRoot = document . getElementById ( VOICE_PLAYER_ROOT_ID ) ;
137+ logger . info ( 'VoicePlayer: Succeeded getting audio file.' , { audioFile } ) ;
138+ const currentAudioUnit = audioStorage [ groupKey ] || AudioUnitDefaultValue ( ) as AudioStorageUnit ;
139+ const audioPlayer = new Audio ( URL ?. createObjectURL ?.( audioFile ) ) ;
140+ audioPlayer . id = VOICE_PLAYER_AUDIO_ID ;
141+ audioPlayer . currentTime = currentAudioUnit . playbackTime ;
142+ audioPlayer . volume = 1 ;
143+ audioPlayer . loop = false ;
144+ audioPlayer . onplaying = ( ) => {
145+ logger . info ( 'VoicePlayer: OnPlaying event is called from audioPlayer' , { groupKey, audioPlayer } ) ;
146+ voicePlayerDispatcher ( {
147+ type : ON_VOICE_PLAYER_PLAY ,
148+ payload : { groupKey, audioFile } ,
149+ } ) ;
150+ } ;
151+ audioPlayer . onpause = ( ) => {
152+ logger . info ( 'VoicePlayer: OnPause event is called from audioPlayer' , { groupKey, audioPlayer } ) ;
153+ voicePlayerDispatcher ( {
154+ type : ON_VOICE_PLAYER_PAUSE ,
155+ payload : { groupKey, duration : audioPlayer . duration , currentTime : audioPlayer . currentTime } ,
156+ } ) ;
157+ } ;
158+ audioPlayer . ontimeupdate = ( ) => {
159+ voicePlayerDispatcher ( {
160+ type : ON_CURRENT_TIME_UPDATE ,
161+ payload : { groupKey } ,
162+ } ) ;
163+ } ;
164+ audioPlayer . onerror = ( error ) => {
165+ logger . error ( 'VoicePlayer: Failed to load the audioFile on the audio player.' , error ) ;
166+ voicePlayerDispatcher ( {
167+ type : RESET_AUDIO_UNIT ,
168+ payload : { groupKey } ,
169+ } ) ;
170+ } ;
171+ audioPlayer . dataset . sbGroupId = groupKey ;
172+ // clean up the previous audio player
173+ try {
174+ voicePlayerRoot ?. childNodes . forEach ( ( node ) => {
175+ const element = node as HTMLAudioElement ;
176+ const thisGroupKey = element . dataset ?. sbGroupKey ;
177+ if ( thisGroupKey !== groupKey ) {
178+ element ?. pause ?.( ) ;
179+ voicePlayerRoot . removeChild ( element ) ;
180+ logger . info ( 'VoicePlayer: Removed other player.' , { element } ) ;
181+ }
182+ } ) ;
183+ } finally {
184+ audioPlayer ?. play ( ) ;
185+ voicePlayerRoot ?. appendChild ( audioPlayer ) ;
186+ voicePlayerDispatcher ( {
187+ type : SET_CURRENT_PLAYER ,
188+ payload : { groupKey, audioPlayer } ,
189+ } ) ;
190+ logger . info ( 'VoicePlayer: Succeeded playing audio player.' , { groupKey, audioPlayer } ) ;
191+ }
192+ } )
193+ . catch ( ( error ) => {
194+ logger . warning ( 'VoicePlayer: Failed loading audio file with URL.' , error ) ;
156195 voicePlayerDispatcher ( {
157- type : ON_CURRENT_TIME_UPDATE ,
196+ type : RESET_AUDIO_UNIT ,
158197 payload : { groupKey } ,
159198 } ) ;
160- } ;
161- audioPlayer . dataset . sbGroupId = groupKey ;
162- // clean up the previous audio player
163- try {
164- voicePlayerRoot ?. childNodes . forEach ( ( node ) => {
165- const element = node as HTMLAudioElement ;
166- const thisGroupKey = element . dataset ?. sbGroupKey ;
167- if ( thisGroupKey !== groupKey ) {
168- element ?. pause ?.( ) ;
169- voicePlayerDispatcher ( {
170- type : ON_VOICE_PLAYER_PAUSE ,
171- payload : { groupKey } ,
172- } ) ;
173- voicePlayerRoot . removeChild ( element ) ;
174- logger . info ( 'VoicePlayer: Removed other player.' , { element } ) ;
175- }
176- } ) ;
177- } finally {
178- audioPlayer ?. play ( ) ;
179- voicePlayerRoot ?. appendChild ( audioPlayer ) ;
180- voicePlayerDispatcher ( {
181- type : SET_CURRENT_PLAYER ,
182- payload : { groupKey, audioPlayer } ,
183- } ) ;
184- logger . info ( 'VoicePlayer: Succeeded playing audio player.' , { groupKey, audioPlayer } ) ;
185- }
186- } ) ;
199+ } ) ;
187200 } ;
188201
189202 return (
0 commit comments