flash.media

# SoundTransform - AS3

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

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

The SoundTransform class contains properties for volume and panning.

View the examples

Related API Elements

flash.display.SimpleButton.soundTransform
flash.display.Sprite.soundTransform
flash.media.Microphone.soundTransform
flash.media.SoundChannel.soundTransform
flash.media.SoundMixer.soundTransform
flash.net.NetStream.soundTransform

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | leftToLeft : Number A value, from 0 (none) to 1 (all), specifying how much of the left input is played in the left speaker. | SoundTransform
|  | leftToRight : Number A value, from 0 (none) to 1 (all), specifying how much of the left input is played in the right speaker. | SoundTransform
|  | pan : Number The left-to-right panning of the sound, ranging from -1 (full pan left) to 1 (full pan right). | SoundTransform
|  | rightToLeft : Number A value, from 0 (none) to 1 (all), specifying how much of the right input is played in the left speaker. | SoundTransform
|  | rightToRight : Number A value, from 0 (none) to 1 (all), specifying how much of the right input is played in the right speaker. | SoundTransform
|  | volume : Number The volume, ranging from 0 (silent) to 1 (full volume). | SoundTransform

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | SoundTransform(vol:Number = 1, panning:Number = 0) Creates a SoundTransform object. | SoundTransform
|  | 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
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Property Detail

### leftToLeft

property

`leftToLeft:Number`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

A value, from 0 (none) to 1 (all), specifying how much of the left input is played in the left speaker.

Implementation
`public function get leftToLeft():Number`
`public function set leftToLeft(value:Number):void`

### leftToRight

property

`leftToRight:Number`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

A value, from 0 (none) to 1 (all), specifying how much of the left input is played in the right speaker.

Implementation
`public function get leftToRight():Number`
`public function set leftToRight(value:Number):void`

### pan

property

`pan:Number`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

The left-to-right panning of the sound, ranging from -1 (full pan left) to 1 (full pan right). A value of 0 represents no panning (balanced center between right and left).

Implementation
`public function get pan():Number`
`public function set pan(value:Number):void`

### rightToLeft

property

`rightToLeft:Number`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

A value, from 0 (none) to 1 (all), specifying how much of the right input is played in the left speaker.

Implementation
`public function get rightToLeft():Number`
`public function set rightToLeft(value:Number):void`

### rightToRight

property

`rightToRight:Number`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

A value, from 0 (none) to 1 (all), specifying how much of the right input is played in the right speaker.

Implementation
`public function get rightToRight():Number`
`public function set rightToRight(value:Number):void`

### volume

property

`volume:Number`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

The volume, ranging from 0 (silent) to 1 (full volume).

Implementation
`public function get volume():Number`
`public function set volume(value:Number):void`
Constructor Detail

### SoundTransform

()
Constructor

`public function SoundTransform(vol:Number = 1, panning:Number = 0)`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

Creates a SoundTransform object.

Parameters

| `vol:Number` (default = `1`)`` — The volume, ranging from 0 (silent) to 1 (full volume).
| `panning:Number` (default = `0`)`` — The left-to-right panning of the sound, ranging from -1 (full pan left) to 1 (full pan right). A value of 0 represents no panning (center).

Example ( How to use this example )
In the following example, the sound plays only from the right channel, and the volume is set to 50 percent.

In the constructor, the sound is loaded and is assigned to a sound channel (`channel`). A SoundTranform object (`transform`) is also created. Its first argument sets the volume at 50 percent (the range is 0.0 to 1.0). Its second argument sets the panning. In this example, panning is set to 1.0, which means the sound comes from the right speaker only. In order for these settings to take effect, the `transform` SoundTranform object is assigned to the sound channel's `souundTransform` property.

Note: There is limited error handling written for this example.

Copy

    package {
        import flash.display.Sprite;
        import flash.net.URLRequest;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundTransform;
        import flash.events.IOErrorEvent;

        public class SoundTransform_constructorExample extends Sprite
        {
            public function SoundTransform_constructorExample() {
                var mySound:Sound = new Sound();
                var url:URLRequest = new URLRequest("mySound.mp3");
                var channel:SoundChannel;
                var transform:SoundTransform = new SoundTransform(0.5, 1.0);

                mySound.load(url);
                channel = mySound.play();
                channel.soundTransform = transform;

                mySound.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            }

            private function errorHandler(errorEvent:IOErrorEvent):void {
                trace("The sound could not be loaded: " + errorEvent.text);
            }
        }
    }

Examples How to use this example
SoundTransformExample.as
The following example loads and plays an MP3 file. As the MP3 file plays, move the mouse or other user input device; the volume and panning change as you move the user input device over the Stage. To run this example, place a file named MySound.mp3 in the same directory as your SWF file.
Copy

    package {
        import flash.display.Sprite;
        import flash.display.StageAlign;
        import flash.display.StageScaleMode;
        import flash.events.*;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.media.SoundTransform;
        import flash.net.URLRequest;
        import flash.utils.Timer;

        public class SoundTransformExample extends Sprite {
            private var url:String = "MySound.mp3";
            private var soundFactory:Sound;
            private var channel:SoundChannel;
            private var positionTimer:Timer;

            public function SoundTransformExample() {
                stage.align = StageAlign.TOP_LEFT;
                stage.scaleMode = StageScaleMode.NO_SCALE;

                var request:URLRequest = new URLRequest(url);
                soundFactory = new Sound();
                soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                soundFactory.load(request);
                channel = soundFactory.play();
                stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            }

            private function ioErrorHandler(event:Event):void {
                trace("ioErrorHandler: " + event);
            }

            private function setPan(pan:Number):void {
                trace("setPan: " + pan.toFixed(2));
                var transform:SoundTransform = channel.soundTransform;
                transform.pan = pan;
                channel.soundTransform = transform;
            }

            private function setVolume(volume:Number):void {
                trace("setVolume: " + volume.toFixed(2));
                var transform:SoundTransform = channel.soundTransform;
                transform.volume = volume;
                channel.soundTransform = transform;
            }

            private function mouseMoveHandler(event:MouseEvent):void {
                var halfStage:uint = Math.floor(stage.stageWidth / 2);
                var xPos:uint = event.stageX;
                var yPos:uint = event.stageY;
                var value:Number;
                var pan:Number;

                if (xPos > halfStage) {
                    value = xPos / halfStage;
                    pan = value - 1;
                } else if (xPos < halfStage) {
                    value = (xPos - halfStage) / halfStage;
                    pan = value;
                } else {
                    pan = 0;
                }

                var volume:Number = 1 - (yPos / stage.stageHeight);

                setVolume(volume);
                setPan(pan);

            }
        }
    }

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00