ActionScript 3.0 Language and Components Reference | All Packages | All Classes | Language Elements | Index | Appendixes | Conventions | Frames No Frames |
SoundMixer

Package | flash.media
Class | public final class SoundMixer
Inheritance | SoundMixer Object

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The SoundMixer class contains static properties and methods for global sound control in the SWF file. The SoundMixer class controls embedded streaming sounds in a SWF; it does not control dynamically created Sound objects (that is, Sound objects created in ActionScript).

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | bufferTime : int [static] The number of seconds to preload an embedded streaming sound into a buffer before it starts to stream. | SoundMixer
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | prototype : Object [static] A reference to the prototype object of a class or function object. | Object
|  | soundTransform : SoundTransform [static] The SoundTransform object that controls global sound properties. | SoundMixer

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | areSoundsInaccessible():Boolean [static] Determines whether any sounds are not accessible due to security restrictions. | SoundMixer
|  | computeSpectrum(outputArray:ByteArray, FFTMode:Boolean = false, stretchFactor:int = 0):void [static] Takes a snapshot of the current sound wave and places it into the specified ByteArray object. | SoundMixer
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | stopAll():void [static] Stops all sounds currently playing. | SoundMixer
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Property Detail

bufferTime | property

`bufferTime:int` [read-write]

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The number of seconds to preload an embedded streaming sound into a buffer before it starts to stream. The data in a loaded sound, including its buffer time, cannot be accessed by a SWF file that is in a different domain unless you implement a URL policy file. For more information about security and sound, see the Sound class description.

The `SoundMixer.bufferTime` property only affects the buffer time for embedded streaming sounds in a SWF and is independent of dynamically created Sound objects (that is, Sound objects created in ActionScript). The value of `SoundMixer.bufferTime` cannot override or set the default of the buffer time specified in the SoundLoaderContext object that is passed to the `Sound.load()` method.

Implementation
`public static function get bufferTime():int`
`public function set bufferTime(value:int):void`

See also

Sound

soundTransform | property |

`soundTransform:SoundTransform` [read-write]

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

The SoundTransform object that controls global sound properties. A SoundTransform object includes properties for setting volume, panning, left speaker assignment, and right speaker assignment. This SoundTransform object only affects sounds embedded within a SWF file. The SoundTransform object used in this property provides final sound settings that are applied to all sounds after any individual sound settings are applied.

Implementation
`public static function get soundTransform():SoundTransform`
`public function set soundTransform(value:SoundTransform):void`

See also

SoundTransform
Method Detail

areSoundsInaccessible | () | method

`public static function areSoundsInaccessible():Boolean`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Determines whether any sounds are not accessible due to security restrictions. For example, a sound loaded from a domain other than that of the SWF file is not accessible if the server for the sound has no URL policy file that grants access to the domain of the SWF file. The sound can still be loaded and played, but low-level operations, such as getting ID3 metadata for the sound, cannot be performed on inaccessible sounds.

Returns

| `Boolean` — The string representation of the boolean.

See also

computeSpectrum()

computeSpectrum | () | method |

`public static function computeSpectrum(outputArray:ByteArray, FFTMode:Boolean = false, stretchFactor:int = 0):void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Takes a snapshot of the current sound wave and places it into the specified ByteArray object. The values are formatted as normalized floating-point values, in the range -1.0 to 1.0. The ByteArray object passed to the `outputArray` parameter is overwritten with the new values. The size of the ByteArray object created is fixed to 512 floating-point values, where the first 256 values represent the left channel, and the second 256 values represent the right channel.

Note: This method is subject to local file security restrictions and restrictions on cross-domain loading. If you are working with local SWF files or sounds loaded from a server in a different domain than the calling SWF, you might need to address sandbox restrictions through a URL policy file. For more information, see the Sound class description.

This method is supported over RTMP in Flash Player 9.0.115.0 and later. You can control access to streams on Flash Media Server in a server-side script. For more information, see the `Client.audioSampleAccess` and `Client.videoSampleAccess` properties in Server-Side ActionScript Language Reference for Adobe Flash Media Server.

Parameters

| `outputArray:ByteArray` — A ByteArray object that holds the values associated with the sound. If any sounds are not available due to security restrictions (`areSoundsInaccessible == true`), the `outputArray` object is left unchanged. If all sounds are stopped, the `outputArray` object is filled with zeros.
| `FFTMode:Boolean` (default = `false`)`` — A Boolean value indicating whether a Fourier transformation is performed on the sound data first. Setting this parameter to `true` causes the method to return a frequency spectrum instead of the raw sound wave. In the frequency spectrum, low frequencies are represented on the left and high frequencies are on the right.
| `stretchFactor:int` (default = `0`)`` — The resolution of the sound samples. If you set the `stretchFactor` value to 0, data is sampled at 44.1 KHz; with a value of 1, data is sampled at 22.05 KHz; with a value of 2, data is sampled 11.025 KHz; and so on.

See also

areSoundsInaccessible()
Sound
SoundLoaderContext.checkPolicyFile
Example
In the following example, the `computeSpectrum()` method is used to produce a graphic representation of the sound wave data.

In the constructor, a sound file is loaded and set to play. (There is no error handling in this example and it is assumed that the sound file is in the same directory as the SWF file.) The example listens for the `Event.ENTER_FRAME` event while the sound plays, repeatedly triggering the `onEnterFrame()` method to draw a graph of the sound data values. When the sound finishes playing the `onPlaybackComplete()` method stops the drawing process by removing the listener for the `Event.ENTER_FRAME` event.

In the `onEnterFrame()` method, the `computeSpectrum()` method stores the raw sound in the `bytes` byte array object. The data is sampled at 44.1 KHz. The byte array containing 512 bytes of data, each of which contains a floating-point value between -1 and 1. The first 256 values represent the left channel, and the second 256 values represent the right channel. The first for loop, reads the first 256 data values (the left stereo channel) and draws a line from each point to the next using the `Graphics.lineTo()` method. (The vector graphic display of the sound wave is written directly on to the class's sprite object.) The sound bytes are read as 32-bit floating-point number from the byte stream and multiplied by the plot height to allow for the vertical range of the graph. The width is set to twice the width of the channel length. The second for loop reads the next set of 256 values (the right stereo channel), and plots the lines in reverse order. The `g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);` and `g.lineTo(0, PLOT_HEIGHT);` methods draw the baseline for the waves. The resulting waveform plot produces a mirror-image effect.

    package {
        import flash.display.Sprite;
        import flash.display.Graphics;
        import flash.events.Event;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundMixer;
        import flash.net.URLRequest;
        import flash.utils.ByteArray;
        import flash.text.TextField;

        public class SoundMixer_computeSpectrumExample extends Sprite {

            public function SoundMixer_computeSpectrumExample() {
                var snd:Sound = new Sound();
                var req:URLRequest = new URLRequest("Song1.mp3");
                snd.load(req);

                var channel:SoundChannel;
                channel = snd.play();
                addEventListener(Event.ENTER_FRAME, onEnterFrame);
                channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
            }

            private function onEnterFrame(event:Event):void {
                var bytes:ByteArray = new ByteArray();
                const PLOT_HEIGHT:int = 200;
                const CHANNEL_LENGTH:int = 256;

                SoundMixer.computeSpectrum(bytes, false, 0);

                var g:Graphics = this.graphics;

                g.clear();

                g.lineStyle(0, 0x6600CC);
                g.beginFill(0x6600CC);
                g.moveTo(0, PLOT_HEIGHT);

                var n:Number = 0;

                for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
                    n = (bytes.readFloat() * PLOT_HEIGHT);
                    g.lineTo(i * 2, PLOT_HEIGHT - n);
                }

                g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
                g.endFill();

                g.lineStyle(0, 0xCC0066);
                g.beginFill(0xCC0066, 0.5);
                g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

                for (i = CHANNEL_LENGTH; i > 0; i--) {
                    n = (bytes.readFloat() * PLOT_HEIGHT);
                    g.lineTo(i * 2, PLOT_HEIGHT - n);
                }

                g.lineTo(0, PLOT_HEIGHT);
                g.endFill();
            }

            private function onPlaybackComplete(event:Event):void {
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
        }
    }

stopAll | () | method |

`public static function stopAll():void`

Language Version : | ActionScript 3.0

Player Version : | Flash Player 9

Stops all sounds currently playing.

This method does not stop the playhead. Sounds set to stream will resume playing as the playhead moves over the frames in which they are located.

When using this property, consider the Flash Player security model:

- By default, calling the `SoundMixer.stopAll()` method stops only sounds in the same security sandbox as the object that is calling the method. Any sounds whose playback was not started from the same sandbox as the calling object are not stopped.
- When you load the sound, using the `load()` method of the Sound class, you can specify a `context` parameter, which is a SoundLoaderContext object. If you set the `checkPolicyFile` property of the SoundLoaderContext object to `true`, Flash Player checks for a URL policy file on the server from which the sound is loaded. If the server has a URL policy file, and the file permits the domain of a SWF file, then the file can stop the loaded sound by using the `SoundMixer.stopAll()` method; otherwise it cannot.

For more information, see the following:

- The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
- The Flash Player Developer Center Topic: Security

Example
In the following example, the `stopAll()` method is used to mute two sounds that are playing at the same time.

In the constructor, two different sound files are loaded and set to play. The first sound is loaded locally and is assigned to a sound channel. (It is assumed that the file is in the same directory as the SWF file.) The second file is loaded and streamed from the Adobe site. In order to use the `SoundMixer.stopAll()` method, all sound must be accessible. (A SoundLoaderContext object can be used to check for the cross-domain policy file.) Each sound also has an event listener that is invoked if an IO error occurred while loading the sound file. A `muteButton` text field is also created. It listens for a mouse click, which will invoke the `muteButtonClickHandler()` method.

In the `muteButtonClickHandler()` method, if the text field content is "MUTE," the `areSoundsInaccessible()` method checks if the sound mixer has access to the files. If the files are accessible, the `stopAll()` method stops the sounds. By clicking on the text field again, the first sound begins playing and the text field's content changes to "MUTE" again. This time, the `stopAll()` method mutes the one sound that is running. Note that sound channel `stop()` method can also be used to stop a specific sound assigned to the channel. (To use the channel functionally, the sound needs to be reassigned to the channel each time the `play()` method is invoked.)

    package {
        import flash.display.Sprite;
        import flash.net.URLRequest;
        import flash.media.Sound;
        import flash.media.SoundLoaderContext;
        import flash.media.SoundChannel;
        import flash.media.SoundMixer;
        import flash.text.TextField;
        import flash.text.TextFieldAutoSize;
        import flash.events.MouseEvent;
        import flash.events.IOErrorEvent;

        public class SoundMixer_stopAllExample extends Sprite  {
            private var firstSound:Sound = new Sound();
            private var secondSound:Sound = new Sound();
            private var muteButton:TextField = new TextField();
            private var channel1:SoundChannel = new SoundChannel();

            public function SoundMixer_stopAllExample() {
                firstSound.load(new URLRequest("mySound.mp3"));
                secondSound.load(new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3"));

                firstSound.addEventListener(IOErrorEvent.IO_ERROR, firstSoundErrorHandler);
                secondSound.addEventListener(IOErrorEvent.IO_ERROR, secondSoundErrorHandler);

                channel1 = firstSound.play();
                secondSound.play();

                muteButton.autoSize = TextFieldAutoSize.LEFT;
                muteButton.border = true;
                muteButton.background = true;
                muteButton.text = "MUTE";

                muteButton.addEventListener(MouseEvent.CLICK, muteButtonClickHandler);

                this.addChild(muteButton);
            }

            private function muteButtonClickHandler(event:MouseEvent):void {

                if(muteButton.text == "MUTE") {

                    if(SoundMixer.areSoundsInaccessible() == false) {
                        SoundMixer.stopAll();
                        muteButton.text = "click to play only one of sound.";
                    }
                    else {
                        muteButton.text = "The sounds are not accessible.";
                    }
                }
               else {
                    firstSound.play();
                    muteButton.text = "MUTE";
               }
            }

            private function firstSoundErrorHandler(errorEvent:IOErrorEvent):void {
                trace(errorEvent.text);
            }

            private function secondSoundErrorHandler(errorEvent:IOErrorEvent):void {
                trace(errorEvent.text);
            }
        }
    }

Submit Feedback on LiveDocs
© 2004-2008 Adobe Systems Incorporated. All rights reserved.
Tue May 20 2008, 04:02 AM -07:00