Android 重叠声音动作脚本3/AIR

Android 重叠声音动作脚本3/AIR,android,actionscript-3,flash,audio,air,Android,Actionscript 3,Flash,Audio,Air,我在第1帧(主页)中有一个播放(恢复)/暂停按钮。但是,当用户浏览应用程序并决定按Home按钮返回主页时,声音会重叠。当用户按下其他按钮时,它开始无休止地重叠。谢谢这是一个Actionscript 3 Flash应用程序,将使用AdobeAIR部署在Android设备中。这是我的密码: import flash.net.URLRequest; import flash.media.Sound; import flash.media.SoundChannel; import flash.ui.Mo

我在第1帧(主页)中有一个播放(恢复)/暂停按钮。但是,当用户浏览应用程序并决定按Home按钮返回主页时,声音会重叠。当用户按下其他按钮时,它开始无休止地重叠。谢谢这是一个Actionscript 3 Flash应用程序,将使用AdobeAIR部署在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);
}

不要在时间线中使用初始化代码。另外,除非
myChannel
中有有效的
SoundChannel
,否则不要启动声音,这需要进行更多检查。另外,
position
pauseSound()
中的本地函数,请移动到全局,否则您将丢失数据,无法恢复声音。@Vesper谢谢!我是Flash的初学者,请耐心听我说。你能告诉我你的修正代码版本吗?非常感谢。谢谢@Organia!我可以问一下,我的play_btn恢复功能放在哪里?只需在第一帧中定义该功能,只需确保订阅一次。@niagrafallsxxx检查。请参阅标题为
停止声音
的部分,然后是
暂停声音
。最好为处理音频暂停和恢复创建单独的功能。使用布尔值切换(暂停/恢复为真/假)..@VC.One,谢谢!我尝试使用代码(代码在第1帧中)。当我在第2帧,并决定回到第1帧,声音重叠。有什么可能解决这个问题?@niagrafallsxxx请看我上面的代码。正是这样。