A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Basic mp3 embedded into swf or stream from server.

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    11

    Basic mp3 embedded into swf or stream from server.

    Hi all,

    I have searched the internet but can't find the answer I'm looking for, i need a basic swf with a play/pause button to ether play an embedded mp3 file or stream if it is easier.

    Or even not flash if anyone knows of an alternative?

    Any help would be great.
    thanks
    Karl

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    What are you using, Actionscript 2.0 or Actionscript 3.0?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    It is a new project so could be ether, I will proberly keep it AS2 but it wouldn't matter if it was AS3 if it were easier.

    Thanks

  4. #4
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    AS2

    Import a sound file into Flash, find it in your Library (CTRL+L), right-click it, choose Properties, expand Advanced, tick/check Export for ActionScript, in the text field labeled Identifier, type in something like, mySound, and press OK. Make a button in your Stage, give it an instance name of (click on the button, open Properties Panel [CTRL+F3], and type in the text field labeled, Instance Name), my_btn - and then click on your Frame, open Actions Panel (F9), and type in this:

    Actionscript Code:
    sound = new Sound();
    sound.attachSound("mySound");
    sound.start();

    soundOn = true;

    my_btn.onPress = function(){
        soundOn = !soundOn;
        if(soundOn){
            sound.start(currentPosition);
        } else {
            currentPosition = sound.position/1000;
            sound.stop();
        }
    }

    This is a play/pause button as a single button. Just tell me if you want it distributed into two buttons.

    And trust me, Sound handling in AS2 is MUCH easier than in AS3
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Awesome thanks mate, if I were to have 2 mp3's and 2 buttons would I just need to double the code but have 2 buttons and 2 "mysound" ? I ask because I have 2 people and need a play button under each.

  6. #6
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    yup, but you'll need to change the names to unique names:

    sound -> sound2
    my_btn -> my_btn2
    soundOn -> soundOn2
    currentPosition -> currentPosition2
    mySound -> mySound2

    or just copy and paste this code after the previous one, and give your second music, a Linkage name of (the Identifier name), mySound2, and give your second button an instance name of, my_btn2:

    Actionscript Code:
    sound2 = new Sound();
    sound2.attachSound("mySound");
    sound2.start();

    soundOn2 = true;

    my_btn2.onPress = function(){
        soundOn2 = !soundOn2;
        if(soundOn2){
            sound2.start(currentPosition2);
        } else {
            currentPosition2 = sound2.position/1000;
            sound2.stop();
        }
    }
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Thanks Mate, I owe you a few

  8. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Ok it was playing both mp3's at once but not now. Now it requires 2 clicks to play?
    Last edited by nwhc; 05-07-2012 at 12:34 AM.

  9. #9
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Sorry about that, new code:

    Actionscript Code:
    sound = new Sound();
    sound.attachSound("mySound");
    sound.start();

    soundOn = true;

    my_btn.onPress = function(){
        trace(currentPosition+" | "+currentPosition2);
        soundOn = !soundOn;
        if(soundOn){
            currentPosition2 = sound2.position/1000;
            sound2.stop();
            soundOn2 = false;
            sound.start(currentPosition);
        } else {
            currentPosition = sound.position/1000;
            sound.stop();
        }
    }

    sound2 = new Sound();
    sound2.attachSound("mySound2");
    currentPosition2 = 0;

    soundOn2 = false;

    my_btn2.onPress = function(){
        soundOn2 = !soundOn2;
        if(soundOn2){
            currentPosition = sound.position/1000;
            sound.stop();
            soundOn = false;
            sound2.start(currentPosition2);
        } else {
            currentPosition2 = sound2.position/1000;
            sound2.stop();
        }
    }
    Last edited by Nig 13; 05-07-2012 at 05:29 AM.
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  10. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    11
    Ok that works but now mySound start automatically?

    cheers

    Just had a thought could it be my preloaded causing issues?
    Last edited by nwhc; 05-07-2012 at 10:54 PM.

  11. #11
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    well, how about telling me that you don't want it automatically started in the beginning ?

    Change these 4 first lines:

    Code:
    sound = new Sound();
    sound.attachSound("mySound");
    sound.start();
    
    soundOn = true;
    to this:

    Actionscript Code:
    sound = new Sound();
    sound.attachSound("mySound");
    currentPosition = 0;

    soundOn = false;
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center