2017-02-14 43 views
0

我有一个播放(恢复)/第1帧中暂停按钮(主页)。但是,当用户导航应用程序并通过按主页按钮决定返回主页时,声音重叠。而当用户按下其他按钮时,它开始无休止地重叠。谢谢!这是一个使用Adobe AIR在Android设备上部署的Actionscript 3 Flash应用程序。这是我的代码:重叠的声音的ActionScript 3/AIR

import flash.net.URLRequest; 
import flash.media.Sound; 
import flash.media.SoundChannel; 
import flash.ui.Mouse; 
import flash.events.MouseEvent; 

var played:Boolean = false; 
var soundFile:URLRequest = new URLRequest("music.mp3"); 
var mySound:Sound = new Sound; 

if(played== false){ 
      played= true; 
mySound.load(soundFile); 
var myChannel:SoundChannel = new SoundChannel; 
myChannel = mySound.play(0,999); 

pause_btn.addEventListener(MouseEvent.CLICK,pauseSound) 
function pauseSound(event:MouseEvent):void 
    { 
     var position = myChannel.position; 
     myChannel.stop(); 
     play_btn.addEventListener(MouseEvent.CLICK,resumeSound); 
     } 

function resumeSound(event:MouseEvent):void 
    { 
     myChannel = mySound.play(myChannel.position); 
    } 
} 
+0

不要在时间轴使用初始化代码。除非'myChannel'中有一个有效的'SoundChannel',否则不要启动声音,这需要更多的检查。另外''位置'是'pauseSound()'中的本地函数,移动到全局位置,否则您将丢失数据并且无法恢复您的声音。 – Vesper

+0

@Vesper谢谢你!我是Flash的初学者,请耐心等待。你能否给我你的版本更正的代码?谢谢! – niagrafallsxxx

回答

0

这就是你通过在帧而不是类编码。解决方案A:从您的第1帧脚本中创建一个类,以便仅执行一次(创建主时间线时)。

解决方案B:创建重复之前进行检查:

var played:Boolean; 

var mySound:Sound; 
var myChannel:SoundChannel 

// That will be executed only once, because the 
// next time this variable will be initialized. 
if (mySound == null) 
{ 
    played = true; 

    var soundFile:URLRequest = new URLRequest("music.mp3"); 

    mySound = new Sound; 
    mySound.load(soundFile); 
    myChannel = new SoundChannel; 
    myChannel = mySound.play(0,999); 

    pause_btn.addEventListener(MouseEvent.CLICK,pauseSound); 
} 
+0

谢谢@Organis!请问,我在哪里可以放置我的play_btn的简历能力? – niagrafallsxxx

+0

只需在第一帧中定义该功能,只能确保只订阅一次。 – Organis

+0

你能给我一个示例代码吗?谢谢! – niagrafallsxxx