2017-04-19 60 views
1

守則連接旋律和創建實例的形式,一個簡單的自定義搜索條..如何創建在ActionScript音樂播放器3.0

import flash.events.Event; 
var mySound:Sound = new MyFavSong(); 
var myChannel:SoundChannel = new SoundChannel(); 
var lastPosition:Number = 0; 
var myTransform = new SoundTransform(); 
myChannel = mySound.play(); 
myTransform.volume = 1; 
myChannel.soundTransform = myTransform; 
stop(); 

然後播放按鈕,暫停,聲音停止+音量 停止

stop_btn.addEventListener(MouseEvent.CLICK, onClickStop); 
    function onClickStop(e:MouseEvent):void{ 
    myChannel.stop(); 
    lastPosition = 0; 
    stop(); 
    } 

暫停

pause_btn.addEventListener(MouseEvent.CLICK, onClickPause); 
function onClickPause(e:MouseEvent):void{ 
lastPosition = myChannel.position; 
myChannel.stop(); 
stop(); 
} 

播放

play_btn.addEventListener(MouseEvent.CLICK, onClickPlay); 
    function onClickPlay(e:MouseEvent):void{ 
    myChannel = mySound.play(lastPosition); 
    myChannel.soundTransform = myTransform; 
    play(); 
    } 

up_btn.addEventListener(MouseEvent.CLICK, onClickUp); 
function onClickUp(e:MouseEvent):void{ 
myChannel.soundTransform = myTransform; 
myTransform.volume = myTransform.volume + 1; 
} 

請幫我

+0

不要使用play()和stop()方法,因爲它們會影響你的主時間軸(或者你的函數聲明爲影片剪輯),而不是聲音。 – DigitalD

回答

1

沒有爲的SoundChannel沒有專門的進展情況,但可以訪問SoundChannel.position屬性每一幀得到以毫秒爲單位當前的聲音位置。您可以通過將SoundChannel.position的值除以Sound.length值來獲得搜索欄的比例。 見例如:

var mySound: Sound = new MyFavSong(); 
var myChannel: SoundChannel = mySound.play(); 
var lastPosition: Number = 0; 
var myTransform = new SoundTransform(); 
myTransform.volume = 1; 
myChannel.soundTransform = myTransform; 
stop(); 

var totalWidth: Number = stage.stageWidth; 
var barContainer: Sprite = new Sprite(); 
var progressBar: Shape = new Shape(); 
var blankBar: Shape = new Shape(); 
initBlankBar(); 
barContainer.addChild(blankBar); 
barContainer.addChild(progressBar); 
barContainer.addEventListener(MouseEvent.CLICK, seekSound); 
progressBar.addEventListener(Event.ENTER_FRAME, updateSoundProgress); 
addChild(barContainer); 

function initBlankBar(): void { 
    blankBar.graphics.beginFill(0xAAC7DB); 
    blankBar.graphics.drawRect(0, 0, totalWidth, 20); 
    blankBar.graphics.endFill(); 
} 

function updateSoundProgress(e: Event): void { 
    var w: Number = (myChannel.position/mySound.length) * totalWidth; 
    progressBar.graphics.clear(); 
    progressBar.graphics.beginFill(0xFF0000); 
    progressBar.graphics.drawRect(0, 0, w, 20); 
    progressBar.graphics.endFill(); 
} 

function seekSound(e: MouseEvent):void { 
    lastPosition = (e.localX/totalWidth) * mySound.length; 
    onClickPlay(null); 
} 

function onClickPlay(e: MouseEvent): void { 
    myChannel.stop(); 
    myChannel = mySound.play(lastPosition, 0, myTransform); 
} 
+0

是的,這是我需要的 非常感謝! –