首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重叠声音Actionscript 3/AIR

重叠声音Actionscript 3/AIR
EN

Stack Overflow用户
提问于 2017-02-14 13:23:08
回答 1查看 121关注 0票数 0

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

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-02-14 13:55:12

这是通过在帧而不是类中编码而得到的。

解决方案A:在第1帧脚本中创建一个类,以便只执行一次(创建主时间线时)。

解决方案B:创建副本前进行检查:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42218475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档