重叠声音Actionscript 3 / AIR

时间:2017-02-14 05:23:08

标签: android actionscript-3 flash audio air

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

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);
    }
}

1 个答案:

答案 0 :(得分:0)

这就是你在帧而不是类中编码所得到的。

解决方案A:从第1帧脚本中创建一个类,这样它只执行一次(当创建主时间轴时)。

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

// pythagorean:
//    Returns string indicating "missing" info for a 
//    right-angled triangle (two regular sides and the
//    hypotenuse). Basic outcome is:
//        Hypotenuse null, sides given, return hypotenuse.
//        Sides null, hypotenuse given, return equal sides.
//        Hypotenuse and only one side given, return other side.
//        Anything else, error given.

function pythagorean (s1, s2, hyp) {
    // At least one value must be null.

    if ((hyp !== null) && (s1 !== null) && (s2 !== null)) {
        return "ERROR: all fields given, don't know what you want";
    }

    // If hypotenuse is null, need both other sides or error.
    // Returns hypotenuse.

    if (hyp === null) {
        if ((s1 === null) || (s2 === null)) {
            return "ERROR: hypotenuse is null, other sides cannot be";
        }
        return "hypotenuse is " + Math.sqrt((s1 * s1) + (s2 * s2));
    }

    // At this point, we KNOW we have hypotenuse, don't check again.
    // If BOTH sides are missing, assume equal.

    if ((s1 === null) && (s2 === null)) {
        return "sides are both " + Math.sqrt((hyp * hyp) / 2);
    }

    // At this point, we have hypotenuse and exactly one side
    // (not the other), so just return the other side.

    if (s1 === null) {
        return "side 1 is " + Math.sqrt((hyp * hyp) - (s2 * s2));
    }
    return "side 2 is " + Math.sqrt((hyp * hyp) - (s1 * s1));
}