Skip to content

Commit 5e1d86b

Browse files
committed
WebAudioSoundManager.setAudioContext is a new method that allows you to set the Sound Manager Audio Context to a different context instance. It will also disconnect and re-create the gain nodes on the new context.
1 parent eac54a6 commit 5e1d86b

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/sound/webaudio/WebAudioSoundManager.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,50 @@ var WebAudioSoundManager = new Class({
114114
return new AudioContext();
115115
},
116116

117+
/**
118+
* This method takes a new AudioContext reference and then sets
119+
* this Sound Manager to use that context for all playback.
120+
*
121+
* As part of this call it also disconnects the master mute and volume
122+
* nodes and then re-creates them on the new given context.
123+
*
124+
* @method Phaser.Sound.WebAudioSoundManager#setAudioContext
125+
* @since 3.21.0
126+
*
127+
* @param {AudioContext} context - Reference to an already created AudioContext instance.
128+
*
129+
* @return {this} The WebAudioSoundManager instance.
130+
*/
131+
setAudioContext: function (context)
132+
{
133+
if (this.context)
134+
{
135+
this.context.close();
136+
}
137+
138+
if (this.masterMuteNode)
139+
{
140+
this.masterMuteNode.disconnect();
141+
}
142+
143+
if (this.masterVolumeNode)
144+
{
145+
this.masterVolumeNode.disconnect();
146+
}
147+
148+
this.context = context;
149+
150+
this.masterMuteNode = context.createGain();
151+
this.masterVolumeNode = context.createGain();
152+
153+
this.masterMuteNode.connect(this.masterVolumeNode);
154+
this.masterVolumeNode.connect(context.destination);
155+
156+
this.destination = this.masterMuteNode;
157+
158+
return this;
159+
},
160+
117161
/**
118162
* Adds a new sound into the sound manager.
119163
*

0 commit comments

Comments
 (0)