我在第1帧(主页)中有一个“播放(恢复)/暂停”按钮。但是,当用户浏览该应用程序并决定通过按下主页按钮返回“主页”时,声音会重叠。当用户按下其他按钮时,它开始无休止地重叠。谢谢!这是一个ActionScript 3 Flash应用程序,将使用Adobe AIR在Android设备中部署。这是我的代码:
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);
}
}
最佳答案
那就是通过框架而不是类进行编码所得到的。
解决方案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);
}
关于android - 重叠的声音Actionscript 3/AIR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42218475/