Author Topic: Audio Help needed...  (Read 1440 times)

wdotcom

  • Guest
Audio Help needed...
« on: February 25, 2014, 12:23:57 pm »
I am playing around with creating a basic app that my nieces have asked me to create for their birthday parties.  Nothing fancy.  Basically it's a game called Pass the Parcel.  You start playing music and a wrapped parcel is passed around a circle.  The music stops.  Whomever is holding the parcel has about 5 seconds to frantically unwrap.  When the music starts again, they continue passing the parcel.  The game continues until the parcel is unwrapped.

Everything is going ok except for the music...which of course is the key to the game. 

I have a button that starts the music.  It works.  I need to do two things.
1) I want a "Pause" button. (Just in case the game needs to be paused for an extended period of time)  I used Mp3Stop() but of course that just resets the music so when I hit Play again, it restarts from the beginning.  I guess this isn't a really big deal but it would be nice if it would resume from the paused location.  I want to be able to hit pause and then either hit the pause or play button to have it begin playing again from the same spot.  I've been looking but can only find a stop, loop and play function for mp3s.  Not sure if this is possible.

2) I also want to be able to embed random 5 second pauses while the music is playing.  So it plays for 8 seconds, pauses for 5, resumes and plays for 6 seconds, pauses for 5, and so on.  I know I'll be using randomize to generate the random numbers.  I suppose once the first issue is addressed this part will be a lot easier.  In fact, I don't even care if the music is just muted or if it's actually paused.

Two more minor questions
1) I know I can use loop but is there a way to tell it "If the first song finishes, move onto a second song?"
2) Pretty sure this is a no but can users select music from their device or do I have to preload all the music into the app?


Thanks in advance for any support.  I haven't played enough with sounds other than a basic "play" and "stop playing" button.

Sandi

expleo

  • Jr. Member
  • **
  • Posts: 57
  • Karma: +0/-0
    • View Profile
Re: Audio Help needed...
« Reply #1 on: February 26, 2014, 02:10:16 am »
Hi, first off, the bad news is that mp3 functions on dragonfire sdk is lacking badly. there is not even a way to detect if a song is completed playing, no pause, no location seek function, no volume mixing etc. So this also answers your other question marked 1) ;)

So in short, you are stuck with mp3play and mp3stop (mp3loop). For a game I worked on, I tried having fixed time and using a counter to measure time, then start next segment when 30 secconds had passed. This seemed to work, but for some reason the game crashed/froze when it was idling too much. I never figured out why,  and the best solution would be a 'song played trough' api call (it should be possible as dragonfire already have a loop function, so detecting so end should not be very impossible).

Anyway. Play and Stop. These are your tools.
Then you need random (and randomize, look up the post 'how to make random more random')

Next you need to use timers. Use a couple of counters that ticks up (30 ticks=1 seccond).

Something like this:

int CounterToCheckIfMusickStops;
int PauseFiveSeccondsCounter;
bool MusicIsPaused;

//Need a onbutton funcion _here_ that sets MusicIsPaused=true when starting the game/music, and starting the music first time.

main(){
   CounterToCheckIfMusickStops=0;
   MusicIsPaused=true;
   PauseFiveSeccondsCounter=0;
}

ontimer(){
   if (MusicIsPaused==false){
      CounterToCheckIfMusickStops++;
   } // only increase counter if we are playing the music

   if (CounterToCheckIfMusickStops==60 && MusicIsPaused==false;){
      CounterToCheckIfMusickStops=0; //resetting the counter every 2 secconds
      if (Random(200-CounterToCheckIfMusickStops)==1){
         MusicIsPaused=true; //to trigger the pause
         Mp3Stop(); //stop the music
      }// random stop the player, increasing the chance to stop as time increases
   }// end if music stops==60 checker

   if(MusicIsPaused){ //pause 5 secconds
      PauseFiveSeccondsCounter++;
      if (PauseFiveSeccondsCounter=>1800){ // 5 secconds is 1800 ticks
         Mp3Play(song ID or perhaps a random song out of an array of songs ??);
         PauseFiveSeccondsCounter=0;
         MusicIsPaused=false;
      }//end if check for 5 secconds
   }

}


EDIT:

as mentioned, you would need a button that starts the music first time, and set the bool value of musicpaused to false. Also perhaps a seccond bool to see if the game is 'on'.

You can preload a few songs, and have the player select a random song to avoid same song playing from start over and over, perhaps also in a predefined order.

also, in the code i wrote (and didnt test :), there is

if (Random(200-CounterToCheckIfMusickStops)==1){

you can of course modify this to suit your needs, have a minimum wait state before you start checking if music is stopping etc, this is just one idea. Every 2 secconds (the 60 mark), the counter is reset, and a check is performed if the music is stopping or not.
« Last Edit: February 26, 2014, 02:19:11 am by expleo »

wdotcom

  • Guest
Re: Audio Help needed...
« Reply #2 on: February 27, 2014, 07:25:22 am »
Thank you!  I will definitely give this a try.