viromedia / viro

ViroReact: AR and VR using React Native
MIT License
2.3k stars 481 forks source link

ViroReact resets the sound playback mode on iOS #462

Open slycoder opened 5 years ago

slycoder commented 5 years ago

Environment

  1. Development OS: Mac.
  2. Device OS & Version: iOS 12.1.
  3. Version: ViroReact 2.11.0, RN 0.57.4.
  4. Simulator & iPhone 6s

Description

ViroReact seems to be resetting the sound playback mode set in the app. Somewhere in [VRT3DSceneNavigator didSetProps:] it seems to be calling [AVAudioSession setCategory:] (I determined this via a symbolic breakpoint). This is having some unintended effects on audio playback elsewhere. For example, if the app had previously set the category to Playback with the AVAudioSessionCategoryOptionMixWithOthers option in order to allow background audio from other apps, mounting the Viro scene will cause that background audio to stop, even if no audio is being emitted from the Viro scene.

Reproducible Demo

  1. Initialize the audio session in the app by calling:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error: nil]
  1. Start the app and observe that any audio playing in the background keeps playing.

  2. Mount any Viro3DSceneNavigator. Observe that the background audio is now interrupted.

Note that you can get around this by swizzling AVAudioSession's setCategory.

achuvm commented 5 years ago

Hi @slycoder,

It looks like you have a workaround for now, we'll take this into consideration because what's happening is that the audio library that we use itself changes the category and we change it so that we can record audio during screen recording. However, this might be something that we can do later when the developer actually requests/desires to screen record as opposed to immediately during creation of the view.

Thanks for reporting this to us, we'll post on this issue with any future updates.

kaushil111 commented 2 years ago

Hi @slycoder, can you post a demo of where you structured that and how it should look? Looks like I am missing something and unable to do it.

slycoder commented 2 years ago

It's been many years since I did this, but you can read more about method swizzling here: https://nshipster.com/method-swizzling/ Just apply that to setCategory on AVAudioSession and ignore any calls that are not in the category you care about.