Sakeer
Sakeer

Reputation: 81

Paying the sounds left and right speaker alternatively using flash

I am having set of sounds in an array and that will start playing randomly when I click START button. Now I am wondering that how to play the sounds in left and right speaker alternatively . For ex. if the first sound is played in the left speaker of my head phone the second one should be played in right speaker and so on. Is it possible to do?

Upvotes: 0

Views: 456

Answers (1)

Pixel Elephant
Pixel Elephant

Reputation: 21403

You can use the SoundTransform class to accomplish this. Take a look at this help article for an example http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d1f.html

Here's an extract in case that link ever dies:

An individual SoundChannel object controls both the left and the right stereo channels for a sound. If an mp3 sound is a monaural sound, the left and right stereo channels of the SoundChannel object will contain identical waveforms.

You can find out the amplitude of each stereo channel of the sound being played using the leftPeak and rightPeak properties of the SoundChannel object. These properties show the peak amplitude of the sound waveform itself. They do not represent the actual playback volume. The actual playback volume is a function of the amplitude of the sound wave and the volume values set in the SoundChannel object and the SoundMixer class.

The pan property of a SoundChannel object can be used to specify a different volume level for each of the left and right channels during playback. The pan property can have a value ranging from -1 to 1, where -1 means the left channel plays at top volume while the right channel is silent, and 1 means the right channel plays at top volume while the left channel is silent. Numeric values in between -1 and 1 set proportional values for the left and right channel values, and a value of 0 means that both channels play at a balanced, mid-volume level.

The following code example creates a SoundTransform object with a volume value of 0.6 and a pan value of -1 (top left channel volume and no right channel volume). It passes the SoundTransform object as a parameter to the play() method, which applies that SoundTransform object to the new SoundChannel object that is created to control the playback.

var snd:Sound = new Sound(new URLRequest("bigSound.mp3"));
var trans:SoundTransform = new SoundTransform(0.6, -1);
var channel:SoundChannel = snd.play(0, 1, trans);

You can alter the volume and panning while a sound is playing by setting the pan or volume properties of a SoundTransform object and then applying that object as the soundTransform property of a SoundChannel object.

All you would need to add on to that is a Boolean flag to indicate which side should have no volume and then to modify the sound transform appropriately.

Upvotes: 1

Related Questions