2017-02-14 42 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