Avg. Rating 3.9

Problem

Okay, I have my favorite Killers tune playing in my Flex 2 app, but how can I mute the sound if I get a phone call?

Solution

Look no further than the SoundChannel and the SoundTransform Classes. By combining these, you can add a mute button to your MP3 Player.

Detailed explanation

Demo                Source

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:fx="com.fusiox.ui.*" initialize="initApp()" layout="vertical"
backgroundColor="black">
    <mx:Script>
        <![CDATA[
            import flash.media.Sound;
            import flash.net.URLRequest;
            import flash.net.URLLoader;
            import flash.media.SoundLoaderContext;
            import flash.media.SoundChannel;
            import flash.media.SoundTransform;

            [Embed(source="images/pause.gif")]
            [Bindable] private var imgPause:Class;
            [Embed(source="images/play.gif")]
            [Bindable] private var imgPlay:Class;
            [Bindable] private var sndTransform:SoundTransform =
new SoundTransform();
            [Bindable] private var volume:Number;
            [Bindable] private var request:URLRequest = new
URLRequest("http://webcfmx.no-ip.info/prototypes/mp3s/24
Theme.mp3");
            private var channel:SoundChannel;

            private function initApp():void {
                var loader:URLLoader = new URLLoader(request);
                vis.setStyle('audioLineColor',16737792)
                vis.setStyle('audioFillColor',16776960)
                loader.addEventListener(ProgressEvent.PROGRESS,
updateProgress);
                loader.addEventListener(Event.COMPLETE,
completeProgress);
            }
               
            private function
updateProgress(event:ProgressEvent):void {
                if (event.bytesLoaded < event.bytesTotal) {
                    lblLoading.text = "24 Radio [ Loading " +
event.bytesLoaded + " ]";
                }
            }
               
            private function completeProgress(event:Event):void {
                lblLoading.text = "24 Radio";
                var mySound:Sound = new Sound(request, new
SoundLoaderContext(1000, true));
                channel = mySound.play(0,1);
                channel.addEventListener(Event.SOUND_COMPLETE,
showCompletion);
                vis.visible = true;
                btnMute.enabled = true;
            }
            
            private function muteSound():void {
                // Mute the channel, but only if it not muted
                if (sndTransform.volume!=0) {
                    sndTransform.volume = 0;
                } else {
                    sndTransform.volume = 1;
                }
                volume = sndTransform.volume;
                channel.soundTransform = sndTransform;    
            }

            private function showCompletion(event:Event):void {
                btnMute.enabled = false;
                vis.visible = false;
            }
        ]]>
    </mx:Script>

    <mx:Label id="lblLoading" fontFamily="Verdanna"
fontSize="24" color="white"/>
    <fx:Visualization id="vis" width="60%" height="182"
opaqueBackground="black" />
    <mx:Button id="btnMute" label="{(volume==0)?'Sound
On':'Mute'}" labelPlacement="left"
icon="{(volume==0)?imgPlay:imgPause}" click="muteSound()"
toolTip="{(volume==0)?'Click to turn sound on...':'Click to
mute...'}" enabled="false" />

</mx:Application>
NameThatTune.zip
[Source Code]
Report abuse

Related recipes