Avg. Rating 5.0

Problem

Need to control sound with a play button, stop button, volume up and volume down.

Solution

Use SoundChannel and SoundTransform.

Detailed explanation

var req:URLRequest=new URLRequest("sound.mp3");
var mySound:Sound=new Sound;
var myController:SoundChannel;
var myVolumeControl:SoundTransform;

mySound.load(req);

mySound.addEventListener(Event.COMPLETE, mySoundLoaded);

function mySoundLoaded(event:Event):void {
    myController=mySound.play();
    myController.stop();
    myVolumeControl=myController.soundTransform;

    play_btn.addEventListener(MouseEvent.CLICK, playMySound);
    stop_btn.addEventListener(MouseEvent.CLICK, stopMySound);
    upVolume_btn.addEventListener(MouseEvent.CLICK, volumeUp);
    downVolume_btn.addEventListener(MouseEvent.CLICK, volumeDown);
}

function playMySound(event:MouseEvent):void {
    myController=mySound.play();
}
function stopMySound(event:MouseEvent):void {
    myController.stop();
}

function volumeUp(event:MouseEvent):void {
    myVolumeControl.volume+=.1;
    if (myVolumeControl.volume>1) {
        myVolumeControl.volume=1;
    }
    myController.soundTransform=myVolumeControl;
}
function volumeDown(event:MouseEvent):void {
    myVolumeControl.volume-=.1;
    if (myVolumeControl.volume<0) {
        myVolumeControl.volume=0;
    }
    myController.soundTransform=myVolumeControl;
}

 

 

View source in the attached file.

Thank you for reading.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes