2012-11-08 81 views
1

我正在使用以下代碼播放項目庫中的音頻記錄。在幾個音頻按鈕上添加暫停按鈕的動作腳本

import flash.media.Sound; 
import flash.media.SoundChannel; 
import flash.events.MouseEvent; 

var sound1:Sound = new audio1(); 
var sound2:Sound = new audio2(); 

var mySoundChannel:SoundChannel = new SoundChannel(); 

function stopSound():void 
{ 
    //This stops all sound in the sound channel. 
    //If there is nothing playing, nothing happens. 
    mySoundChannel.stop(); 
} 

//In this function, we create an argument that allows us to tell the function 
//what sound to we want it to play. 
function playSound(soundname:String):void 
{ 
    try 
    { 
     mySoundChannel = this[soundname].play(0, 0); 
    } 
    catch(error:ReferenceError) 
    { 
     trace("playSound: That sound name doesn't exist."); 
     return; 
    } 
} 

//Hook up buttons--> 
button1.buttonMode = true; 
button1.useHandCursor = true; 

button2.buttonMode = true; 
button2.useHandCursor = true; 

button1.addEventListener(MouseEvent.CLICK, button1click); 
button2.addEventListener(MouseEvent.CLICK, button2click); 

function button1click(evt:Event):void 
{ 
    stopSound(); 
    playSound("sound1"); 
} 

function button2click(evt:Event):void 
{ 
    stopSound(); 
    playSound("sound2"); 
} 

當按鈕被點擊時,我需要暫停正在播放的音頻。我該怎麼做呢?

回答

0

你需要做的五件事你當前的代碼,以暫停和恢復當前正在播放的聲音:

  1. 創建存儲當前播放 聲音文件的名稱一個變量,一個變量存儲音頻的暫停位置。
  2. 創建一個暫停功能。
  3. 創建一個恢復功能。
  4. 展開當前playSound和stopSound函數以使用新變量。
  5. 連接按鈕事件偵聽器。

第1步:

var currentSound:String = ""; 
var pausePosition:Number = 0; 

第2步:我們要保存在我們剛剛創建的第二可變音頻的當前位置。我們可以使用mySoundChannel.position屬性獲取音頻的當前播放位置,該屬性返回一個Number值(匹配我們給予pausePosition變量的Number類型)。

function pauseSound():void 
{ 
    //If there's a song to pause... 
    if(currentSound != "") 
    { 
     //Get pause position. 
     pausePosition = mySoundChannel.position; 
     //Stop the sound directly. 
     mySoundChannel.stop(); 
    } 
} 

注意我們沒有調用stopSound()。這有一個很好的理由。我們將很快在該函數中添加一行代碼,我們不想在pauseSound()中使用它。

第3步:現在我們創建恢復音頻的功能。注意這和playSound()不一樣。我們告訴它從pausePosition開始播放,而不是從0開始(聲音片段的開頭)。

function resumeSound():void 
{ 
    //If there's a song to resume... 
    if(currentSound != "") 
    { 
     //Start playing the current audio from the position we left off at. 
     mySoundChannel = this[currentSound].play(pausePosition); 
    } 
} 

第4步:因爲我們現在我們在步驟1中聲明的變量的工作,我們需要調整如何playSound()和stopSound()工作。

在playSound()中,我們將聲音名稱保存爲currentSound,而不是將聲音名稱傳遞給聲道。

function playSound(soundname:String):void 
{ 
    try 
    { 
     currentSound = soundname 
     mySoundChannel = this[currentSound].play(0, 0); 
    } 
    catch(error:ReferenceError) 
    { 
     trace("playSound: That sound name doesn't exist."); 
     return; 
    } 
} 

在stopSound(),我們需要真正清除currentSound和pausePosition變量,當我們停下來,確保resumeSound不啓動音頻後,我們已經完全停止了。

function stopSound():void 
{ 
    //This stops all sound in the sound channel. 
    //If there is nothing playing, nothing happens. 
    mySoundChannel.stop(); 

    //Clear our variables. 
    currentSound = ""; 
    pausePosition = 0; 
} 

GOTCHA警告:通常,可以通過在第二個參數傳遞其他大於0的整數環路音頻(其中我有5)在下面的代碼:

mySoundChannel = this[currentSound].play(0, 5); 

在上面的代碼,聲音從一開始播放,並重復五次。

但是,如果您是從0以外的任何位置開始播放音頻,實際發生的情況是音頻將在您開始的位置循環播放,而不是在開始播放。

也就是說,如果您使用此代碼:

mySoundChannel = this[currentSound].play(1000, 5); 

聲音將循環五次,但每次的聲音在循環開始隨着時間的推移,它會開始從位置1000打,和NOT聲音的開始(0)。

我希望能回答你的問題!