【问题标题】:Disable PLAY button after click - Adobe Flash Builder 4.6单击后禁用播放按钮 - Adob​​e Flash Builder 4.6
【发布时间】:2012-10-02 20:27:08
【问题描述】:

我使用以下示例播放 MP3 文件(我不需要/想要“打开”对话框):

<?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>`

点击播放按钮后,MP3文件播放正常,但如果我再次点击播放按钮,歌曲同时从头开始,如果我点击按钮三、四、五次或以上。所以我最终得到了同一首歌的更多同时“会话”。我想在第一次单击后禁用 PLAY 按钮,并在单击 STOP 后再次启用相同的按钮。我该怎么做?

【问题讨论】:

    标签: apache-flex mobile button


    【解决方案1】:

    要回答您的具体问题,您可以使用按钮上的 enabled 属性。在你的 playSound() 方法中,这样做::

    public function playSound():void {
        sndChannel=snd.play();
        playButton.enabled = false;
    }   
    

    一定要为你的 playButton 添加一个 Id:

    <s:Button id="playButton" label="play" click="playSound();"/>
    

    您可能需要考虑在 playSound() 方法中添加一个检查,以便在声音已经播放时不播放它。为此,首先创建一个变量:

    protected var isPlaying : Boolean = false;
    

    然后像这样调整 playButton() 方法:

    public function playSound():void {
       if(!isPlaying){
        sndChannel=snd.play();
        isPlaying = true;
       }
    }   
    

    在上述任何一种情况下,您可能都需要向complete 事件添加一个事件侦听器,以便重新启用按钮或更改 isPlaying 标志。方法是这样的:

    public function playSound():void {
       if(!isPlaying){
        snd.addEventListener(Event.COMPLETE,onSoundComplete);
        sndChannel=snd.play();
        isPlaying = true;
       }
    }   
    
    public function onSoundComplete(event:Event):void{
      isPlaying = false;
      playButton.enabled = true;
      snd.removeEventListener(Event.COMPLETE,onSoundComplete);
    }
    

    您也可以从停止声音方法中调用 onSoundComplete 方法:

        public function stopSound():void {
            sndChannel.stop();
            onSoundComplete(new Event());
        }   
    

    【讨论】:

    • 嗨!执行上述代码后,出现以下错误:“1136:参数数量不正确。预期为 1”在第 34 行...查看图片:link
    • 您可能需要将参数传递给 Event 类构造函数,指定事件的类型。在我提供的代码中,您可以执行 new Event('dummy') 或 new Event(Event.COMPLETE)
    猜你喜欢
    • 2012-09-15
    • 2015-09-23
    • 1970-01-01
    • 2014-07-14
    • 2017-01-27
    • 2017-10-04
    • 2012-04-10
    • 2015-09-09
    • 1970-01-01
    相关资源
    最近更新 更多