2014-03-01 34 views
0

使用Sound.play函數,可以指定以毫秒爲單位的開始時間作爲參數之一,以及循環聲音的次數。我是否錯過了某些東西,還是沒有辦法指定結束時間?例如,如果我想要4秒鐘聲音的循環毫秒5-105,有沒有辦法指定它在105毫秒時開始下一個循環?如何指定聲音的結束時間

如果我沒有遺漏什麼東西,有沒有其他方法可以解決這個問題?

回答

0

Sound類不允許你執行該操作,要做到這一點的最好辦法是使用定時器:

var musicTimer:Timer = new Timer(100, 5); // Loop total time(ms), N Loops (+1) 
musicTimer.addEventListener(TimerEvent.TIMER, musicTimer_tick); 
musicTimer.addEventListener(TimerEvent.TIMER_COMPLETE, musicTimer_complete); 
musicTimer.start(); 

private function musicTimer_complete(e:TimerEvent):void 
{ 
    // Last loop stop all sounds 
    channel.stop(); 
} 

private function musicTimer_tick(e:TimerEvent):void 
{ 
    // At each loop, stop and (re)start the sound 
    channel.stop(); 
    channel = sound.play(5); // 5 is the loop start time 
} 
0

您可以創建所需的循環設置適合另一個Sound對象。然後,您可以撥打Sound:extract()來對付源音,提取您的聲音需要循環的確切值,然後在目標聲音上調用loadPCMFromByteArray(),然後循環播放該聲音。

function loopASound(sound:Sound,startTime:uint,endTime:uint,loops:int):SoundChannel { 
    var ba:ByteArray=new ByteArray(); 
    var startPos:uint=Math.floor(startTime*44.1); 
    var endPos:uint=Math.floor(endTime*44.1); 
    // sound rate is always 44100, and positions are expected in milliseconds, 
    // as with all the functions of Sound class 
    sound.extract(ba,endPos-startPos,startPos); // get raw data 
    var ripped:Sound=new Sound(); 
    ba.position=0; 
    ripped.loadPCMFromByteArray(ba,endPos-startPos); // format, stereo and sample rate at defaults 
    return ripped.play(0,loops); 
} 

警告:該代碼未經測試,只能通過閱讀手冊才能生成。另外,您可能還想在某處存儲ripped聲音,因此如果您計劃將循環播放幾次以上,則不會一直進行翻錄。

相關問題