A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: when streamed sound starts playing, do something.... i can't get it to work!

  1. #1
    user in A minor
    Join Date
    Jan 2005
    Location
    finland
    Posts
    121

    when streamed sound starts playing, do something.... i can't get it to work!

    hi.
    i've tried and tried, been on kennybellew's site for a loooong time, but i just don't succeed! if anyone can help and give me a nudge forward, i'll be eternally grateful.

    i'm loading sound using loadSound. previously, i have defined a function called playMusicians, which works ok: in another swf i have animation of musicians playing their instruments. so i would like them to start playing when the music can actually be heard. currently they start playing when the song starts loading, and it looks a bit silly for a while until the music plays...

    i've tried numerous alternatives. underneath i show just one example out of many. using this, my musician animation never starts... can anyone help me?

    if you wish to see what i mean, the example of my problem can be seen here

    Code:
    sound1 = new Sound ();
    sound1.loadSound("musics/FirstSong.mp3", true);
    if (sound1.playing==true){
    playMusicians();
    }

  2. #2
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    try using a setInterval or onEnterFrame event to repeatedly check the position property of the sound. Once the position is greater than 0 then start your musician animation. Also there may be pauses in the stream due to buffering. So, you may also want to continue polling the position property comparing it to the previous position to see if the music is stopped due to buffering.

  3. #3
    user in A minor
    Join Date
    Jan 2005
    Location
    finland
    Posts
    121
    hi!
    ok, i tried to do as you suggested and came up with this, but it still doesn't work:
    Code:
    navbar.btns_music.btn1.onRelease=function(){
    	sound1 = new Sound ();
    	sound1.loadSound("musics/FirstSong.mp3", true);
    	this.onEnterFrame = function () {
      	myMusicPosition = sound1.position;
    	if (myMusicPosition>0 && playing==true) {
     		playMusicians();
    		}
    	}
    	sound1.onSoundComplete = function (){
    	stopMusicians();
    	}
    }
    i get the sound all right, but my animated musicians never start playing. i guess i haven't succeeded in addressing the song correctly... or something like that, and my song position is not monitored after all. i've found tutorials close to this problem, but they are using attachSound, with instance names to the songs and so on... whereas i need to have it working with loadSound. is that why it doesn't work, or is it something else i'm doing wrong?

    thanks for the helping hand, i appreciate it!

  4. #4
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    --
    and my song position is not monitored after all.
    --
    if you run a trace inside the onEnterFrame loop and it does not generate any values then it may be that the onEnterFrame is not running due to confusion about the meaning of "this". To Flash, does "this" refer to the button object or the current mc?

    To avoid any ambiguity, have you tried setInterval to poll instead of onEnterFrame?

  5. #5
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    While this is not related to your problem, I think it is good form to define sound object and events outside of the button code. The only sound object code you need inside the button are the lines pertaining to loadSound() and position.

  6. #6
    user in A minor
    Join Date
    Jan 2005
    Location
    finland
    Posts
    121
    ok, right.... if i include 'trace(myMusicPosition);' , i get only 'undefined'. so i tried toggling with "this", writing a full path instead, from _root to current mc. the path should be correct. still, no luck. hmmm... man i feel dumb... should this be easy?

    setInterval? i did'n try that yet because i don't know where to start. i've used seIntervals a couple of times, but how should i use it here...? i'm a noob, limited skills.

    i'll rearrange my code. good to know good practices
    Last edited by mrk13; 10-28-2006 at 12:40 PM.

  7. #7
    user in A minor
    Join Date
    Jan 2005
    Location
    finland
    Posts
    121
    'this', btw, in my code pointed to the current Mc time line, not that of the button, forgot to tell u that
    -----
    adding to the previous: now, having rewritten the code, i get a '0' instead of 'undefined' in the output window. music goes on, number never gets bigger than zero. my bug search continues...
    Last edited by mrk13; 10-28-2006 at 12:51 PM.

  8. #8
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    maybe it is not reading the sound object correctly. Perhaps you can try tracing sound1.getBytesLoaded() inside the loop.

  9. #9
    user in A minor
    Join Date
    Jan 2005
    Location
    finland
    Posts
    121
    hi again. the thing is, i believe it should read the sound position correctly, as elsewhere in the same as frame it does just that: i have a pause button in the swf with these lines:
    Code:
    navbar.btn_pause.onRelease=function(){
    	cue = Math.round(sound1.position/1000);
    	sound1.stop();
    	stopMusicians();
    }
    ...and if i insert "trace(cue);" and press pause, i do get numeric values. works perfectly there. i tried with enterFrame and also setInterval commands, but for some reason, when i try to check if the song is playing i don't get numeric values eny more. this is what i've tried with setInterval: this one gives me a NaN, and so did the similar enterFrame that i tried a bit earlier:

    Code:
    function checkPlay() {                
    		if ((sound1.position/1000) > 0 && playing==true) {
     		playMusicians();
    		}
    }
    myTimer = setInterval(checkPlay, 1000); 
    trace(sound1.position/1000);
    ouput window complains "NaN"... what can this mean?

  10. #10
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    try placing the trace within the code executed by the loop. Also you can use the typeof operator to find out Flash recognizes the sound object as you would expect. It should return "object".

    Code:
    function checkPlay(){
           trace(sound1.position); // should see an increasing value from 0 upwards
           trace(typeof sound1); // remove this line if it prints "object"
    }

  11. #11
    user in A minor
    Join Date
    Jan 2005
    Location
    finland
    Posts
    121
    hi again! yeah, and that's exactly what happens: "object" and growing numbers every second. but what i fail to understand is why my musician animation never starts. they just stand there unmoving... currently the bit of code in total looks like this:
    Code:
    function checkPlay() {
           trace(sound1.position); // an increasing value from 0 upwards
           trace(typeof sound1); // this line prints "object", will be removed
    	if (sound1.position > 0 && playing==true) {
     	playMusicians();
    	}
    }
    myTimer = setInterval(checkPlay, 1000);
    if, like now in the current site, i have a button with commands like onRelease=function(){playMusicians();//+ loadSound command//}, the animation starts just like it should, though of course not in sync with the music. there must be something wrong with my "if (sound1.position)>0 { playMusicians()}" sentence. dunno what... any ideas?

    ---
    one thing i should add: the "playMusicians"-command is just a simple "_root.pathtomusician1.play();" times five, as there are 5 musicians. so the bug is not there, but somewhere else...
    Last edited by mrk13; 10-29-2006 at 06:06 PM.

  12. #12
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    It is a good idea to make liberal use of trace statements when debugging a situation like this. This way you can see where flash is not executing code in a manner you would expect. In awkward debugging situations I may have trace statements all over my code to track the culprit.

    Perhaps your if/then statment is not behaving as you would expect.

    Try putting a trace statement after the if/then clause before any other code to find out if flash gets past the if/then logic.

    Then put a trace inside your playMusicians() function at different places to find out how Flash interprets this code.

  13. #13
    user in A minor
    Join Date
    Jan 2005
    Location
    finland
    Posts
    121
    yes, i've been trying to do something like that... and found out that if i remove from "if (sound1.position > 0 && playing==true)..." the latter part, that is, wipe away "&&playing==true", the musician animation starts. not in sync, but it starts.

    so it must be the part where flash checks if the song is actually playing or not, where things get confused. i tried rewriting it like so:
    Code:
    if (sound1.position > 0 && sound1.playing==true) {
     	playMusicians();}
    	}
    and even i tried with exclamation marks: "...&&sound1.playing!=true" etc

    ...but then it doesn't work: music starts, output window shows growing position numbers but animation doesn't begin. does this bring any thoughts in mind...?

  14. #14
    Junior Member
    Join Date
    Jul 2006
    Posts
    9
    hi

    have you tried:

    sound1.loadSound("musics/FirstSong.mp3", false);
    //the last pram should be "false" since you want to pause, rewind, etc. you can only do this with an event sound
    //true=streaming sound
    //false=event sound

    sound1.onLoad(){
    sound1.start(0,1);
    playMusicians();
    }
    //now that it is a event sound you can trigger statements to run synced when the full sound is loaded in the "onLoad" function.

    hope i could help
    kAM

  15. #15
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    just to clarify, you can pause and rewind "streamed' mp3's. There was a bug in players prior to 6r43 that prevented this but everything after is ok.

    Here is an example mp3 player that has pause and rewind for a progressively loaded mp3.
    http://www.sonify.org/home/feature/r...026_mp3player/

  16. #16
    Junior Member
    Join Date
    Jul 2006
    Posts
    9
    i didn't know that
    thanx for the update and link
    im going to read the article just now

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