Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sound does not resume after closing.

Discussion in 'iOS and tvOS' started by VPrime, Nov 15, 2010.

  1. VPrime

    VPrime

    Joined:
    Mar 4, 2009
    Posts:
    74
    Having a weird issue with the sound in my project (so far there is only 1 mp3 audio track) not resuming after I relaunch/continue playing my game.
    IF I restart the game (through the in game pause menu) or quit the task with the multi tasking bar the sound returns.

    Does any one have any idea on what could be causing this? We tried changing the audio format to a wav, but that did nothing.
     
  2. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    When your app is put in the background via iOS's fast app switching, your audio is killed. To resume it, just have a function somewhere that starts it again when the app is returned to. Something like this...

    Code (csharp):
    1.  
    2. var myMusic : AudioSource;  // assign this variable to your music's audio source
    3. private var currentMusicTime = 0.0; // this is used to store and return to the proper playback position for the audio, so it doesn't start over
    4.  
    5. function OnApplicationPause (tempBool : boolean) {
    6.      if (tempBool) {
    7.           // if the argument for this function is true, the app has just been paused
    8.           currentMusicTime = myMusic.time;
    9.      }
    10.      else {
    11.           // if the argument for this function is false, then the app has just been resumed
    12.           myMusic.Play();
    13.           myMusic.time = currentMusicTime;
    14.      }
    15. }
     
  3. VPrime

    VPrime

    Joined:
    Mar 4, 2009
    Posts:
    74
    Thank you, that helps quite a bit!
     
  4. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Thanks Mika.... I had another thread open with this very issue, and your script resolve it. Cheers.
     
  5. bpritchard

    bpritchard

    Joined:
    Jan 29, 2009
    Posts:
    444
    I've integrated your function as designed, thanks a TON for putting that up. But it created another odd problem... it basically restarts the audio from the beginning of the file. I'm not sure if its because of how my audio is, but they are all typically very short (1-4 second long) talking audio clips and not long looping music. Would that have an effect on anything?

    Also i've attached it to my main character controller in the scene which has the audio source on it, and then took the audio source and placed it in the script as the variable. That seem right?

    Cheers
    Bryan
     
  6. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Awesome, works like a charm, thanks MikaMobile!
     
  7. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    Sure, no prob.

    In regards to bpritchard's question - the script I posted does attempt to resume the sound where it left off, but its not super accurate when you're dealing with compressed audio. AudioClip.time is not precise enough to put you right back on the exact same syllable of a word for instance.