Results 1 to 2 of 2

Thread: Autoplay MP3 Player

  1. #1

    Autoplay MP3 Player

    Can someone please help me get autoplay for an mp3player...below is the code I've used...

    stop();
    // numbers needed for the visuals, you can play around with altering them
    const PLOT_HEIGHT:int = 17;
    const CHANNEL_LENGTH:int = 95;
    // Byte array also needed for the visuals
    var bytes:ByteArray = new ByteArray();
    // Assign The mp3 to play
    var req:URLRequest = new URLRequest("greatiswhatiam.mp3");
    // Boolean value for button functions, to switch in the conditionals
    var isPlaying:Boolean = false;
    // Create the sound object
    var snd:Sound = new Sound(req);
    // Assign a var name for the sound channel
    var channel:SoundChannel;

    // Play Button Listener ///////////////////////////////////////////////////////////////////////
    play_btn.addEventListener(MouseEvent.CLICK, playPause);

    /////////////////////////// Start Play button function //////////////////////////////////////////////////////
    function playPause(event:MouseEvent):void {

    // This conditional makes the magic of our play/pause functionality
    if (isPlaying == false) {
    channel = snd.play(); // Start playing
    isPlaying = true;
    }

    // Stop button listener and function
    stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

    function stopSound(event:MouseEvent):void {
    channel.stop();
    isPlaying = false;
    }

    // On playback complete listener and function, needed to reset some things
    channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

    function onPlaybackComplete(event:Event) {

    isPlaying = false;
    //trace("The sound has finished playing.");
    }

    } /////////////////////////// End Play button function //////////////////////////////////////////////////////



    // Add listener to trigger [onEnterFrame] function below, needed for our amplitude animation
    addEventListener(Event.ENTER_FRAME, onEnterFrame);

    function onEnterFrame(event:Event):void {

    SoundMixer.computeSpectrum(bytes, false, 0);
    var g:Graphics = this.graphics;
    g.clear();
    g.lineStyle(0, 0xffd200);
    g.beginFill(0xffd200, 0.5);
    g.moveTo(0, PLOT_HEIGHT);

    var n:Number = 0;

    // left channel
    for (var i:int = 0; i < CHANNEL_LENGTH; i++)
    {
    n = (bytes.readFloat() * PLOT_HEIGHT);
    g.lineTo(i * 2, PLOT_HEIGHT - n);
    }
    g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
    g.endFill();

    // right channel
    g.lineStyle(0, 0x24d011);
    g.beginFill(0x24d011, 0.5);
    g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

    for (i = CHANNEL_LENGTH; i > 0; i--)
    {
    n = (bytes.readFloat() * PLOT_HEIGHT);
    g.lineTo(i * 2, PLOT_HEIGHT - n);
    }
    g.lineTo(0, PLOT_HEIGHT);
    g.endFill();
    } // END onEnterFrame Function

  2. #2
    OK so after much trial and error I finally got it all by myself....So for anyone out there who may need a solution here is the new code


    stop();
    // numbers needed for the visuals, you can play around with altering them
    const PLOT_HEIGHT:int = 17;
    const CHANNEL_LENGTH:int = 95;
    // Byte array also needed for the visuals
    var bytes:ByteArray = new ByteArray();
    // Assign The mp3 to play
    var req:URLRequest = new URLRequest("greatiswhatiam.mp3");
    // Boolean value for button functions, to switch in the conditionals
    var isPlaying:Boolean = false;
    // Create the sound object
    var snd:Sound = new Sound(req);
    // Assign a var name for the sound channel
    var channel:SoundChannel;
    // Create the play channel using snd
    channel = snd.play(); // Start playing
    // Set "isPalying" to true initially
    isPlaying = true;


    // Stop button listener and function
    stop_btn.addEventListener(MouseEvent.CLICK, stopSound);

    function stopSound(event:MouseEvent):void {
    channel.stop();
    isPlaying = false;
    }

    // Play Button Listener ///////////////////////////////////////////////////////////////////////
    play_btn.addEventListener(MouseEvent.CLICK, playPause);

    /////////////////////////// Start Play button function //////////////////////////////////////////////////////
    function playPause(event:MouseEvent):void {

    // This conditional makes the magic of our play/pause functionality
    if (isPlaying == false) {
    channel = snd.play(); // Start playing
    isPlaying = true;
    }



    // On playback complete listener and function, needed to reset some things
    channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

    function onPlaybackComplete(event:Event) {

    isPlaying = false;
    //trace("The sound has finished playing.");
    }

    } /////////////////////////// End Play button function //////////////////////////////////////////////////////



    // Add listener to trigger [onEnterFrame] function below, needed for our amplitude animation
    addEventListener(Event.ENTER_FRAME, onEnterFrame);

    function onEnterFrame(event:Event):void {

    SoundMixer.computeSpectrum(bytes, false, 0);
    var g:Graphics = this.graphics;
    g.clear();
    g.lineStyle(0, 0xffd200);
    g.beginFill(0xffd200, 0.5);
    g.moveTo(0, PLOT_HEIGHT);

    var n:Number = 0;

    // left channel
    for (var i:int = 0; i < CHANNEL_LENGTH; i++)
    {
    n = (bytes.readFloat() * PLOT_HEIGHT);
    g.lineTo(i * 2, PLOT_HEIGHT - n);
    }
    g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
    g.endFill();

    // right channel
    g.lineStyle(0, 0x24d011);
    g.beginFill(0x24d011, 0.5);
    g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

    for (i = CHANNEL_LENGTH; i > 0; i--)
    {
    n = (bytes.readFloat() * PLOT_HEIGHT);
    g.lineTo(i * 2, PLOT_HEIGHT - n);
    }
    g.lineTo(0, PLOT_HEIGHT);
    g.endFill();
    } // END onEnterFrame Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •