Search Unity

Audio Unity audio management / iOS AVAudioSession (initialization, interrupts, etc.)

Discussion in 'Audio & Video' started by OgnjenTodic, Sep 27, 2017.

  1. OgnjenTodic

    OgnjenTodic

    Joined:
    Feb 18, 2017
    Posts:
    14
    I am having problems trying to control AVAudioSession in iOS app created by Unity; I have a native SDK (and a corresponding Unity plugin) that deals with audio; this SDK controls AVAudioSession (sets it active, inactive, etc.) and I am trying to figure out how to make it co-exist with Unity audio management, specifically when interrupts occur (e..g app goes to background).

    I've seen some other posts about UnityPause and UnitySetAudioSessionActive functions, but there is really not much documentation about these functions.

    When the app goes to the background my SDK sets AVAudioSession to inactive. In order to be able to do that, there can't be any audio playing, so in the iOS plugin I call UnitySetAudioSessionActive(0) before unwinding audio stack in the SDK (this is done via callback and works fine)

    When app comes to foreground, if there is an audio source that's attached to the scene, and I try calling UnitySetAudioSessionActive(1) in the plugin, it crashes the app (stack trace below); same thing happens with the UnityPause(0); the crash doesn't happen if there is no audio source attached to the scene (by audio source I mean that there is an audio clip that plays in the loop)

    Questions:
    - is there any documentation on how Unity audio stack behaves on different platforms (at this point the iOS being most relevant)? When and how it gets initialized, what happens and how it can be controlled when app goes to background/foreground, etc.
    - is there any documentation on UnityPause and UnitySetAudioSessionActive
    - any idea why the app crashes when calling UnitySetAudioSessionActive(1) (and only when there is audio related stuff in Unity scene) and what I need to do to prevent that from happening?

    Code (CSharp):
    1. #0    0x000000010038f664 in begin [inlined] at /Users/builduser/buildslave/unity/build/Runtime/Utilities/LinkedList.h:138
    2. #1    0x000000010038f660 in ::UnloadClip() at /Users/builduser/buildslave/unity/build/Runtime/Audio/sound/SoundManager.cpp:1217
    3. #2    0x000000010037779c in ::CloseFMOD() at /Users/builduser/buildslave/unity/build/Runtime/Audio/AudioManager.cpp:1241
    4. #3    0x0000000100378ba0 in ::ShutdownReinitializeAndReload() at /Users/builduser/buildslave/unity/build/Runtime/Audio/AudioManager.cpp:683
    5. #4    0x00000001000ec268 in -[KeenASRBridge setupAppAudioAfterInterrupt] at /Users/ogi/work/Unity-KeenASR/ios/Libraries/Plugins/KeenASR/iOS/KeenASRBridge.m:37
    6. #5    0x00000001008fd208 in -[KIOSRecognizer setupAppAudioAfterAudioInterruptInAudioController] ()
    7. #6    0x00000001008d7edc in -[KIOSAudioController setupAppAudioAfterAudioInterrupt] ()
    8. #7    0x00000001009a5658 in -[AEAudioController applicationWillEnterForeground:] at /Users/ogi/work/KaldiIOS/Pods/TheAmazingAudioEngine/TheAmazingAudioEngine/AEAudioController.m:2361
    9. #8    0x000000018aa5422c in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
    10. #9    0x000000018aa53930 in _CFXRegistrationPost ()
    11. #10    0x000000018aa536ac in ___CFXNotificationPost_block_invoke ()
    12. #11    0x000000018aac2b9c in -[_CFXNotificationRegistrar find:object:observer:enumerator:] ()
    13. #12    0x000000018a995bf4 in _CFXNotificationPost ()
    14. #13    0x000000018b49f62c in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
    15. #14    0x0000000190b764d0 in -[UIApplication _sendWillEnterForegroundCallbacks] ()
    16. #15    0x0000000190bb1aac in -[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:] ()
    17. #16    0x0000000190bb0fdc in -[UIApplication _handleApplicationLifecycleEventWithScene:transitionContext:completion:] ()
    18. #17    0x0000000190b9c5e0 in __70-[UIApplication scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke ()
    19. #18    0x0000000190b9c264 in -[UIApplication scene:didUpdateWithDiff:transitionContext:completion:] ()
    20. #19    0x0000000190ecdba4 in -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] ()
    21. #20    0x000000018c633b8c in __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.376 ()
    22. #21    0x000000018c6618bc in __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ ()
    23. #22    0x000000018c661728 in -[FBSSerialQueue _performNext] ()
    24. #23    0x000000018c661ad0 in -[FBSSerialQueue _performNextFromRunLoopSource] ()
    25. #24    0x000000018aa68278 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
    26. #25    0x000000018aa67bc0 in __CFRunLoopDoSources0 ()
    27. #26    0x000000018aa657c0 in __CFRunLoopRun ()
    28. #27    0x000000018a994048 in CFRunLoopRunSpecific ()
    29. #28    0x000000018c41a198 in GSEventRunModal ()
    30. #29    0x00000001909802fc in -[UIApplication _run] ()
    31. #30    0x000000019097b034 in UIApplicationMain ()
    32.  
     
  2. mcarriere

    mcarriere

    Joined:
    Sep 14, 2012
    Posts:
    106
    Did you make any progress on this? We're experiencing the same stacktrace, and we're having a hard time figuring out the root cause of it. We can get it to consistently happen when we load a specific scene twice, and then call: UnitySetAudioSessionActive(0) and shortly after: UnitySetAudioSessionActive(1).
     
  3. OgnjenTodic

    OgnjenTodic

    Joined:
    Feb 18, 2017
    Posts:
    14
    Unity is aware of it and working on a fix
     
  4. MechEthan

    MechEthan

    Joined:
    Mar 23, 2016
    Posts:
    166
    2017.4.20 -- I'm not getting any crashes when using UnitySetAudioSessionActive, instead UnitySetAudioSessionActive(0) seems to shut down audio just fine, but after calling that, UnitySetAudioSessionActive(1) does nothing, so when I play a Unity AudioClip through a Unity AudioSource, there is no sound.

    (I'll be filing a bug Monday)
     
  5. juliens137

    juliens137

    Joined:
    Oct 2, 2017
    Posts:
    26
    Hey, is there any update on these issues? Also experiencing some interruption issues muting all audio and not resuming on 2018.4.9f