react-native-video mutes the microphone - ios

When a video is played during a webRTC call it causes the local stream to go mute without muting the track (audio track still has the enabled prop set to true). It does not happen when the 'muted' prop of the Video component is set to true or when the video stats playing before the call is started, but after muting or unmuting the video during the call, the microphone still disconnects. The Video component must be accessing the microphone. Do you know how to fix it? Happens on IOS 14, Xcode 12.2

I fixed it thanks to this post. It was a problem with AVAudioSession in ios/Video/RTCVideo.m. It turns out that the default settings in AVFoundation allow for the use of either Microphone or the Speaker (one at a time), so mounting Video component, or performing any actions that establish AVAudioSession interferes with other components using the microphone.
I changed this part of the code:
- (void)setPaused:(BOOL)paused
{
if (paused) {
[_player pause];
[_player setRate:0.0];
} else {
if([_ignoreSilentSwitch isEqualToString:#"ignore"]) {
// [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; OLD
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil]; //NEW
} else if([_ignoreSilentSwitch isEqualToString:#"obey"]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
}
[_player play];
[_player setRate:_rate];
}
_paused = paused;
}
This fix will work only if the ignoreSilentSwitch in Video component is set to 'ignore'

Related

In iOS created application to force audio output via speaker, but doesn't work when application is in background

I want to implement, when i plugged in headset/headphone in iPhone and play any music, music should play from speaker instead of headphone.
So using this below code that working fine when my application is in foreground. But when my application goes in background that time this code is not working.
I want to implement this functionality in 3 case
When application is in foreground.
When application is in background.
When application remove from backgound.
Please give me solution for that.
I have used this below code,
+ (void) initAudioSessionRouting{
// Called once to route all audio through speakers, even if something's plugged into the headphone jack
static BOOL audioSessionSetup = NO;
if (audioSessionSetup == NO) {
// set category to accept properties assigned below
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: &sessionError];
// Doubly force audio to come out of speaker
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
// fix issue with audio interrupting video recording - allow audio to mix on top of other media
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
// set active
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
// add listener for audio input changes
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, onAudioRouteChange, nil );
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioInputAvailable, onAudioRouteChange, nil );
}
// Force audio to come out of speaker
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
// set flag
audioSessionSetup = YES;}
Thanks in advance

Play MPMoviePlayerController audio stream in background

So I have this app where I play videos and when you exit the app I would like the user to continue to listen to the audio of the video. I use MPMoviePlayerController to play the video and it works in the app perfectly fine. I also setup AVAudioSession up before I play the video and I get no error.
NSError *audioSessionError;
NSError *activationError;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&audioSessionError];
[audioSession setActive:YES error:&activationError];
I also set in plist background modes audio. But the video with the audio both stop playing when you close the app. I've also imported the AVFoundation framework.
Simply set Application does not run in background to NO in .plsit file
You need to make couple of changes in plist file.i.e.
1) Set Required background mode to App plays audio
2) set Application does not run in background to YES.
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
Then, you need to write these much code in AppDelegate
Now, you can easily run audio while phone screen locks or goes in background.
This code worked for me, first you must to give your app permissions to keep playing music in the background (In your .plis), after that go to the wished class and implement this code, first the imports and the the method to play the music.
#import <MediaPlayer/MPNowPlayingInfoCenter.h>
#import <MediaPlayer/MPMediaItem.h>
#import <AVFoundation/AVFoundation.h>
---- o ----
-(void) playMusic{
[[AVAudioSession sharedInstance] setDelegate: self];
NSError *myErr;
// Initialize the AVAudioSession here.
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&myErr]) {
// Handle the error here.
NSLog(#"Audio Session error %#, %#", myErr, [myErr userInfo]);
}else{
// Since there were no errors initializing the session, we'll allow begin receiving remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
//initialize our audio player
audioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://www.cocoanetics.com/files/Cocoanetics_031.mp3"]];
[audioPlayer setShouldAutoplay:NO];
[audioPlayer setControlStyle: MPMovieControlStyleEmbedded];
audioPlayer.view.hidden = YES;
[audioPlayer prepareToPlay];
[audioPlayer play];
}//end playmusic
You need to turn on Background Modes in capabilities.

AVAudioSession record while playing weird issue

For part of an app that I'm making I need to record audio from the user. I have chosen to use AVAudioRecorder to do this.
The problem is that when I start to record audio, all audio playing on the device is paused. I then looked in the Apple docs for AVAudioSession and set the recording options to be: AVAudioSessionCategoryOptionMixWithOthers.
The problem is that whenever I start recording, any audio playing on the device switches from the main external speaker to the small phone (call) speaker.
My goal is to record audio while still allowing all other audio from the device to be outputted through the main speaker.
How can I achieve this?
Here's my code currently:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
[audioSession setActive:YES error:&error];
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if(recorder != nil){
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:#selector(levelTimerCallback:) userInfo:nil repeats:YES];
}else{
NSLog(#"Error: %#",[error description]);
}//end if
Thanks!
Maybe you can try using AVAudioSessionCategoryOptionDefaultToSpeaker. According to the AVAudioSession reference:
When using this option and no other audio route (such as a headset) is available, session audio will play through the device’s built-in speaker. When not using this option, and no other audio output is available or selected, audio will play through the receiver (a speaker intended to be held to the ear).

Audio feed back in foreground and background

I have to give audio feedback when my app is in foreground and also in background. But when the app enters background audio feedback is not heard. In info.plist I have set background mode to App plays audio or streams audio/video using AirPlay and used the following but the audioPlayerDidFinishPlaying delegate is not called when app enters background and audio is not heard.
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
sound.delegate = self;
///Fixed the issue No audible feedback when main audio is silent
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
// This is necessary if you want to play a sequence of songs, otherwise your app will be
// killed after the first one finishes.
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[soundQueue addObject:sound];
You need to make couple of changes in plist file.
1) Set Required background mode to App plays audio
2) set Application does not run in background to YES.
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
Then, you need to write these much code in AppDelegate
Now, you can easily run audio while phone screen locks or in background.
It works fine for me. :)
for more please refer to Playing Audio in background mode and Audio Session Programming Guide

Return volume control to iOS device ringer after playback ends

I have an audio app that uses the Media Playback audio session category to allow background audio. Once my audio session is initialized, the hardware volume buttons on the iOS device control the volume of my audio session.
Once audio playback stops, I'd like to return control of the phone ringer back to the hardware volume buttons but my attempt to do this by deactivating the audio session doesn't do the trick.
Here is how I'm initializing and activating my audio session:
AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),
&sessionCategory);
Here is how I'm attempting to deactivate the audio session and return control of the iOS device's ringer back to the hardware volume controls:
AudioSessionSetActive(false);
There is at least one app that I know of that behaves this way (Audible.com's iOS app). Does anyone have any idea what I may be doing wrong?
In apples documentation I think you are going to have to actually remove the listener.
Look up:
AudioSessionRemovePropertyListenerWithUserData
http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html
I just ran into this problem, but I'm using AVAudioPlayer. If I tried to deactivate my session right after calling play, it didn't work. But waiting for audioPlayerDidFinishPlaying:successfully: and then doing this worked:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error;
BOOL wasSuccessful = [audioSession setActive:NO error:&error];
NSLog(#"wasSuccessful: %#", wasSuccessful ? #"Yes" : #"No");
}
I'm using the default audio session, BTW.

Resources