Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • ActionScript3实现一个简单mp3播放器

    post by onelong / 2010-4-14 23:30 Wednesday [MXML/ActionScript3.0]

    使用ActionScript3做一个播放器是一个相当容易的事情!看看adobe帮助的入门例子;播放音乐使用了Sound类,控制声音使用SoundChannel类,控制声音大小声道等使用SoundTransform类
    下面看看例子啦!
    package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;

    public class Test extends Sprite {
    private var snd:Sound = new Sound();
    private var channel:SoundChannel = new SoundChannel();
    private var button:TextField = new TextField();
    private var stopbtn:TextField = new TextField();
    private var sprite:Sprite=new Sprite();
    private var pausePosition:Number=0;

    public function Test() {
    init();
    }
    private function init():void{
    var req:URLRequest = new URLRequest("22.mp3");//声音文件的地址....
    snd.load(req);

    button.x = 10;
    button.y = 10;
    button.text = "PLAY";
    button.border = true;
    button.background = true;
    button.selectable = false;
    button.autoSize = TextFieldAutoSize.CENTER;
    button.addEventListener(MouseEvent.CLICK, clickHandler);

    this.addChild(button);

    stopbtn.x = 60;
    stopbtn.y = 10;
    stopbtn.text = "STOP";
    stopbtn.border = true;
    stopbtn.background = true;
    stopbtn.selectable = false;
    stopbtn.autoSize = TextFieldAutoSize.CENTER;
    stopbtn.addEventListener(MouseEvent.CLICK, stophandle);

    this.addChild(stopbtn);

    sprite.graphics.beginFill(0);
    sprite.graphics.drawRect(10,30,100,4);
    sprite.graphics.endFill();
    sprite.scaleX=0;
    this.addChild(sprite);

    this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    }
    private function clickHandler(e:MouseEvent):void {
    if(button.text == "PLAY") {
    channel = snd.play(pausePosition);
    pausePosition = channel.position;
    channel.addEventListener(Event.SOUND_COMPLETE,soundCompele);
    this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
    button.text = "PAUSE";
    }
    else {
    pausePosition = channel.position;
    channel.stop();
    button.text = "PLAY";
    this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
    }
    }
    private function stophandle(e:MouseEvent):void {
    channel.stop();
    pausePosition = 0;
    button.text = "PLAY";
    this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
    sprite.scaleX=0;
    }
    private function soundCompele(e:Event):void {
    channel.stop();
    pausePosition = 0;
    button.text = "PLAY";
    this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
    sprite.scaleX=0;
    }
    private function onEnterFrame(e:Event):void {
    sprite.scaleX=channel.position/snd.length;
    }
    }
    }

     

    引用地址:
     

    我要评论