I use the following example for playing an MP3 file (I don’t need/want “Open” dialog):
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
<fx:Script>
<![CDATA[
import flash.media.*;
[Embed(source="assets/marseljeza.mp3")]
[Bindable]
public var sndCls:Class;
public var snd:Sound = new sndCls() as Sound;
public var sndChannel:SoundChannel;
public function playSound():void {
sndChannel=snd.play();
}
public function stopSound():void {
sndChannel.stop();
}
]]>
</fx:Script>
<s:HGroup>
<s:Button label="play" click="playSound();"/>
<s:Button label="stop" click="stopSound();"/>
</s:HGroup>
</s:View>`
After I click on the PLAY-button, the MP3 file plays just fine, but if I click the PLAY-button again, the song starts over from the start simultaneously, and the same if I click the button three, four, five or more times. So I end up with more simultaneously “sessions” of the same song. I want disable the PLAY-button after the first click, and enable the same button again after click on STOP. How can I do this?
To answer your specific question, you can use the enabled property on the button. Inside your playSound() method, do this::
Be sure to add an Id to your playButton:
You may want to consider just adding a check inside your playSound() method to not play the sound if it is already playing. To do this, first create a variable:
Then tweak the playButton() method like this:
In either of the above situations, you’ll probably want to add an event listener to the complete event in order to re-enable to button or change the isPlaying flag. The approach would be something like this:
You can also call the onSoundComplete method from your stop sound method: