?
Solved

Left, Right sound Channels in Flash

Posted on 2008-06-16
4
Medium Priority
?
708 Views
Last Modified: 2013-11-11
I need an ActionScript that would  trigger sounds from different channels, for example, I want sound1 to be triggered from the Left Channel and sound2 to be triggered from the Right channel.
0
Comment
Question by:mjmusa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 21798297
using the SoundTransform class you can control the channels of the sound.  you will need to setup a SoundTransform instance for every sound you want to play and mix
0
 
LVL 43

Expert Comment

by:Rob
ID: 21798306
Here is an example from the Adobe CS3 Reference:
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;
            }
            if(pan > 1) pan = 0;
 
            var volume:Number = 1 - (yPos / stage.stageHeight);
 
            setVolume(volume);
            setPan(pan);
            
        }
    }
}

Open in new window

0
 
LVL 12

Accepted Solution

by:
SamuelRostol earned 1400 total points
ID: 21800608
I've taken the liberty to create a quick-and-dirty example just to show you JUST what's needed to pan a sound. Hopefully you'll get the idea. I've used mostly the same variable-names as tagit to cause as little comfusion as possible.

[code]

I've attached both the code, and an example. Just remove the .txt-extension from both files to be able to run them as usual :)

Good luck!

Kindly,
Samuel
// Have the file "sound.mp3" in same folder as swf
 
// Load the sound
var url:String = "sound.mp3";
var soundFactory:Sound = new Sound();
var request:URLRequest = new URLRequest(url);
soundFactory.load(request);
 
// Play the sound
var channel:SoundChannel = soundFactory.play();
 
// Pan the sound
var sTransform:SoundTransform = channel.soundTransform;
sTransform.pan = -1; // 1 = left speaker, -1 = right speaker, 0 = both
channel.soundTransform = sTransform;

Open in new window

SoundChannel.zip
0
 
LVL 43

Expert Comment

by:Rob
ID: 21808534
Great example Samuel :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question