2013-05-06 55 views
0

我正在創建簡單的音樂播放器。它只會播放一首歌曲。我想創建一個非常簡單的seekbar。我希望一個圖形作爲查找組件工作 - 因此它可以移動到右側和左側(x軸上的兩個點之間:圖形現在的起始點,以及當歌曲結束時的結束點)並對應於歌曲的channel.position。所以要總結一下:我想問一個善良的靈魂,讓我知道應該用什麼代碼來使x軸上的圖形移動與歌曲的channel.position相對應。如何在ActionScript 3.0中爲音樂播放器創建自定義搜索欄

這是我到目前爲止的代碼:

movieClip_1.stop(); 
var itsstoped:Boolean; 
var youpausedit:Boolean; 
var counter:Timer = new Timer(100); 
var chan:SoundChannel; 
var song:Sound = new Sound(); 
song.load(new URLRequest("wmc2.mp3")); 
song.addEventListener(Event.COMPLETE, soundloaded); 
function soundloaded(event:Event) 
{ 
trace("Song has been loaded."); 
} 
counter.addEventListener(TimerEvent.TIMER,countcounter); 
itsstoped = true; 
youpausedit = false; 
button_2.addEventListener(MouseEvent.CLICK, presser); 
function presser(event:MouseEvent):void 
{ 
if(itsstoped == true && youpausedit == true) 
{ 
    movieClip_1.gotoAndPlay(1); 
    chan = song.play(chan.position); 
    itsstoped = false; 
} 
else if(itsstoped == false) 
{ 
    movieClip_1.gotoAndStop(1); 
    chan.stop(); 
    itsstoped = true; 
    youpausedit = true; 
} 
else if(itsstoped == true && youpausedit == false) 
{ 
    movieClip_1.gotoAndPlay(1); 
    chan = song.play(); 
    counter.start(); 
    itsstoped = false; 
} 
} 
function countcounter(e:TimerEvent):void 
{ 
trace(chan.position/song.length); 
var percentage:uint = 100 * (chan.position/song.length); 
trace(percentage); 
if(percentage == 100) 
{ movieClip_1.gotoAndStop(1); 
    itsstoped = true; 
    youpausedit = false; 
    } 
} 

忽略movieClip_1因爲它只是運行播放音樂時的動畫。

回答

0

類似的東西:

addEventListener(Event.ENTER_FRAME,onEFrame); 

function onEFrame(e:Event):void 
{ 
    graphic.x = graphicStartX + music.position/music.length*(graphicEndX-graphicStartX); 
} 
+0

謝謝你的時間和答案。我會嘗試一下,但是用戶拖放操作的代碼是否應該將該圖形更改爲按鈕? – aya9 2013-05-06 10:02:14

+0

我已經試過你的代碼,它的工作原理,但給我: – aya9 2013-05-06 22:30:50

+0

我試過你的代碼,它的工作原理,但給我三個輸出窗口中:TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。那是因爲我的時間線只有一幀?任何其他事件,我可以「聽」而不是ENTER_FRAME,不會得到這個錯誤?此外,如果你可以涉及我以前的問題,我會很多apriciate。 @SzRaPnEL – aya9 2013-05-06 22:38:41

0

問題在這行代碼奠定:

var chan:SoundChannel; 

我已經把它改爲:

var chan = new SoundChannel(); 

和錯誤#1009不再展示。

萬歲! :)