我有一个场景,我正在开发一个音频播放器,并希望为音轨搜索栏编写代码。
感谢任何帮助。
答案 0 :(得分:0)
假设您有一个Sound
对象和一个SoundChannel
对象来播放Sound
对象,您可以执行以下操作:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var _trackBar:Sprite;
private var _sound:Sound;
private var _soundChannel:SoundChannel;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_trackBar = new Sprite();
_trackBar.graphics.beginFill(0);
_trackBar.graphics.drawRect(-10, 0, 20, 40);
_trackBar.graphics.endFill();
addChild(_trackBar);
graphics.lineStyle(2);
graphics.moveTo(_trackBar.x - 10, _trackBar.y);
graphics.lineTo(_trackBar.x - 10, _trackBar.y +_trackBar.height);
graphics.moveTo(_trackBar.x + 200, _trackBar.y);
graphics.lineTo(_trackBar.x + 200, _trackBar.y + _trackBar.height);
_sound = new Sound();
_sound.addEventListener(Event.COMPLETE, onLoaded);
_sound.load(new URLRequest("[mp3 url goes here].mp3"));
}
private function onLoaded(e:Event):void
{
_soundChannel = _sound.play();
stage.addEventListener(Event.ENTER_FRAME, onTick);
_trackBar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_trackBar.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onTick(e:Event):void
{
if (!_soundChannel)
{
return;
}
_trackBar.x = 200 * (_soundChannel.position / _sound.length);
}
private function onMouseUp(e:MouseEvent):void
{
_trackBar.stopDrag();
_soundChannel = _sound.play(_sound.length * (_trackBar.x / 200));
}
private function onMouseDown(e:MouseEvent):void
{
_soundChannel.stop();
_soundChannel = null;
_trackBar.startDrag(false, new Rectangle(0, 0, 200, 0));
}
}
}
请注意:这非常粗糙,绝对可以优化。这只是为了向您展示如何完成这项任务而付出的一切。