@@ -55,14 +55,21 @@ export class Player extends EventEmitter<Events> {
5555 connect ( isReconnect : boolean = false ) {
5656 this . expectClose = ! isReconnect ;
5757 this . ws = new WebSocket ( this . options . url ) ;
58+
59+ // Reset audio output/syncing on new connection
60+ if ( ! isReconnect ) {
61+ this . audioContext = new AudioContextClass ( ) ;
62+ this . serverTimeDiff = 0 ;
63+ this . serverTimeDiffSamples = [ ] ;
64+ }
65+
5866 // Expect binary data as ArrayBuffer
5967 this . ws . binaryType = "arraybuffer" ;
6068
6169 let timeSyncInterval : number | null = null ;
6270
6371 this . ws . onopen = ( ) => {
6472 this . logger . log ( "WebSocket connected" ) ;
65- this . audioContext = new AudioContextClass ( ) ;
6673 this . serverTimeDiffSamples = [ ] ;
6774 this . expectClose = false ;
6875 this . sendHello ( ) ;
@@ -97,6 +104,7 @@ export class Player extends EventEmitter<Events> {
97104 this . ws . onclose = ( ) => {
98105 this . logger . log ( "WebSocket connection closed" ) ;
99106 clearTimeout ( timeSyncInterval ! ) ;
107+ this . sessionInfo = null ;
100108 this . audioContext ! . close ( ) ;
101109 this . audioContext = null ;
102110 this . fire ( "close" , {
@@ -154,16 +162,10 @@ export class Player extends EventEmitter<Events> {
154162 break ;
155163
156164 case "session/end" :
165+ this . sessionInfo = null ;
157166 this . logger . log ( "Session ended" ) ;
158167 this . fire ( "session-update" , null ) ;
159- // Re-instate the audioContext or else the old buffer keeps playing.
160- this . audioContext ! . close ( ) ;
161- this . audioContext = new AudioContextClass ( ) ;
162- this . serverTimeDiffSamples = [ ] ;
163- // Sync player time with the server.
164168 this . sendPlayerTime ( ) ;
165- // Clear session information when session ends.
166- this . sessionInfo = null ;
167169 break ;
168170
169171 case "metadata/update" :
@@ -372,12 +374,5 @@ export class Player extends EventEmitter<Events> {
372374 this . ws . close ( ) ;
373375 this . ws = null ;
374376 this . serverInfo = null ;
375- this . sessionInfo = null ;
376-
377- if ( this . audioContext && this . audioContext . state !== "closed" ) {
378- this . audioContext . close ( ) ;
379- }
380-
381- this . audioContext = null ;
382377 }
383378}
0 commit comments