Search Unity

Music stops playing when returning from video (Unity 3.0 iPhone Advanced)

Discussion in 'iOS and tvOS' started by Shawn[QS], Oct 1, 2010.

  1. Shawn[QS]

    Shawn[QS]

    Joined:
    Dec 8, 2009
    Posts:
    46
    I am getting the following error when returning from using the iPhoneUtils.PlayMovie() method. The background music that had been playing before beginning the movie does not resume.

    Error Initializing AudioSession(errNO:1768843636)
     
  2. Shawn[QS]

    Shawn[QS]

    Joined:
    Dec 8, 2009
    Posts:
    46
    I have noticed that it seems to be a much broader issue. I also get the same error when resuming on an iOS 4+ device, with the OpenFeint scripts when it attempts to resume the audio session, and pretty much any other situation where the audio session is paused.

    Is this an issue with a change in implementation for music between older version of Unity iPhone and 3.0?
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Unity 3.0 uses fmod, no longer openal, the audio session stuff works differently, non hw based compressed audio is supported, ...

    basically, the audiosession of unity must not be activated while something else tries to init / deinit its audio or it might missdetect the situation.
     
  4. Shawn[QS]

    Shawn[QS]

    Joined:
    Dec 8, 2009
    Posts:
    46
    So what is the most agile way to handle background music and playing movies? Its easy enough for me to pause the music when the user clicks the play button, but as far as I know there are no events to catch when the movie returns to Unity. I could start a Coroutine when they click play that sleeps a few frames, and since Unity will be paused while it plays the movie it will not finish the Coroutine until it resumes, but this seems like a serious hack.

    Could you please indicate the proper way to handle this situation?

    EDIT:

    A side note to this is why resuming on iOS4 would cause them same error. Yes it is going to resume the AudioSource which is playing the music when the task was paused but since there is still only a single AudioSource playing there should not be an issue with multiple objects accessing the uncompressed audio channel.

    I would love some further clarification on this subject and some pointers on how to work around the current functionality.

    The music file which is being played is fairly long so it is not really an option to use an un-compressed file for this.
     
    Last edited: Oct 5, 2010
  5. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,269
    I posted about this also and added a bug report (which has been closed with no comment ??!!)
    Is it an error which means nothing?
     
  6. Shawn[QS]

    Shawn[QS]

    Joined:
    Dec 8, 2009
    Posts:
    46
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Was your case number 378074? It's unusual for a case to be closed without a report being sent to the originator. I'll investigate what has happened but it is possible the bug has been fixed yet a report was not sent for some reason.
     
  8. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,269
    yes andeeee - (hopefully is fixed in next version - might be related to another issue...)
     
  9. Shawn[QS]

    Shawn[QS]

    Joined:
    Dec 8, 2009
    Posts:
    46
    I have noticed another strange bug when playing non-compressed audio as well.

    During normal game play there can be up to 4 non-compressed audio files playing plus 1 compressed background sound track which plays the duration of game play. I have noticed that on occasion Unity will skip playing one of the non-compressed sounds.

    The non-compressed sounds are played using the PlayOneShot method on an AudioEmitter, and it is usually the same clip played multiple times for a gun shot. It seems that sometimes it will play 4-5 straight with no issue, other times it will skip playing one. I have been able to work around most of the other bugs with hacks but this is one that I really have no control over.

    Also, note these sounds are not playing in very rapid of succession, usually they are spaced out by 1+ seconds.

    Does anyone have information on why this would be happening?
     
  10. j-robichaud

    j-robichaud

    Joined:
    Aug 26, 2010
    Posts:
    40
    I have the same error on iPhone 4 when I press on the home button. The music will not resume when resuming the application.

    I made a workaround, I do not know if it can be of any help for you.
    I add this component on my audio source and the music will restart. I call Play and Stop on this monobehavior instead of the AudioSource. When the application pauses it stores the information on the music.

    You could do something similar at calling the OnApplicationPause manually when your video plays.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(AudioSource))]
    5.  
    6. public class Music : MonoBehaviour {
    7.    
    8.     bool isPlaying = false;
    9.    
    10.     float time = 0;
    11.    
    12.     public void OnApplicationPause( bool pause )
    13.     {
    14.         if ( isPlaying )
    15.         {
    16.             if ( pause )
    17.             {
    18.                 time = audio.time;
    19.                 audio.Stop();
    20.             }
    21.             else
    22.             {
    23.                 audio.time = time;
    24.                 audio.Play();
    25.             }      
    26.         }
    27.     }
    28.    
    29.     public void Play()
    30.     {
    31.         isPlaying = true;
    32.         audio.Play();
    33.     }
    34.    
    35.     public void Stop()
    36.     {
    37.         isPlaying = false;
    38.         audio.Stop();
    39.     }
    40.    
    41. }
    42.  
     
    Last edited: Oct 30, 2010
  11. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Nice tip.

    But I'm seeing more Audio errors I can't figure out what is going on.

    Using Unity 3.1 Basic versus iOS SDK 4.1 xcode 3.2.4

    I reimported and changed all the music .mp3 to be not 3D sound, and adjusted the non-compressed to fit my need on the new editor.

    But when I launch the OpenFeint (latest 2.7.5) dashboard from the menu, I got a crash which seems somehow related to Audio.

    Here the console log:

     
    Last edited: Nov 21, 2010
  12. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Yes!

    I solved the OF dashboard launch issue.

    Apparently a check I performed on main menu audio channel was giving the issue!
    Now I only have the no audio music coming back after dashboard closing issue left.

    This is what the console puts out:

     
    Last edited: Nov 21, 2010
  13. christopherbrown

    christopherbrown

    Joined:
    Oct 19, 2009
    Posts:
    83
    I'm running into a similar issue. In Scene A, I am playing an mandated intro m4v file (nothing else), then loading Scene B. I have several buttons in Scene B that each fire off audio. Any button with audio enabled pressed in Scene B crashes the application. If I disable the button press audio in Scene B, I am able to navigate about my menu and even load the game in Scene C without any trouble.

    Here's a snippet from my stack trace where the error occurs:
    I hope we can get to the bottom of this. Perhaps is just a gross misunderstanding about how audio works in Unity 3.0?
     
  14. christopherbrown

    christopherbrown

    Joined:
    Oct 19, 2009
    Posts:
    83
    So...I've found a (partial?) solution to my problem. I am building to iOS. My original audio clips were imported from mp3. Originally, I had 3D Sound, Decompress On Load, and Hardware Decoding toggle ON. When I went back to the audio clip for my button sound, turned Decompress On Load OFF, I still generate the AudioSession error code, but my application does not crash.

    Worked for me for the time being; hope it works for you. Until then, I'll chalk this up to ignorance of Unity's audio handling.
     
  15. FiveFingers

    FiveFingers

    Joined:
    Oct 15, 2009
    Posts:
    541
    Yes. This is right.

    But even if is now not crashing, we are still experiencing the bug that will prevent Audio Channel of background music (mp3) not being restored when the App looses its focus. And not only with OpenFeint !

    If the battery runs low and the "20% left" message comes up on iOS 4.x (not yet tested on 4.2) when I click "OK" the App looses the audio background !

    This has something to do with a well-known FMOD bug and the fact that when a Unity App looses the focus, the Audio is being paused!

    Hopefully will be fixed in the next releases. Meantime, I Keep on 1.7
     
    Last edited: Nov 29, 2010
  16. ReJ

    ReJ

    Unity Technologies

    Joined:
    Nov 1, 2008
    Posts:
    378
    Easy fix for running OpenFeint on Unity3.0 would be to open AppController+OpenFeint.mm (in your Assets/Editor/OpenFeint/Xcode/ directory), find all UnitySetAudioSessionActive function calls and just remove them all.

    It looks like current AppController+OpenFeint.mm was written with Unity1.7 in mind, not Unity3.0.
     
  17. francksitbon

    francksitbon

    Joined:
    Jan 22, 2010
    Posts:
    268
    Same problem with background music, when clicking on Home, the music won't restart.

    Strange if i put:

    function OnApplicationPause()
    {
    music.audio.volume = 1;
    music.audio.Play();
    }

    Then if i click on home button, then launch the app from multitask, the music plays. should not this happen in a something "OnApplicationPlay()" ?