Audiosessionsetproperty Deprecated in iOS 7.0 So How Set Kaudiosessionproperty_Overridecategorymixwithothers

AudioSessionSetProperty deprecated in iOS 7.0 so how set kAudioSessionProperty_OverrideCategoryMixWithOthers

Use AVAudioSession:

AVAudioSession *session = [AVAudioSession sharedInstance];

NSError *setCategoryError = nil;
if (![session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:&setCategoryError]) {
// handle error
}

AVCaptureSession and background audio iOS 7

You can use the AVAudioSessionCategoryOptionMixWithOthers.
For instance,

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

After that, you can use the AVAudioPlayer simultaneously with AVCaptureSession.

However, the above code leads to very low volume.
If you want normal volume, use the AVAudioSessionCategoryOptionDefaultToSpeaker with AVAudioSessionCategoryOptionMixWithOthers as follows,

[session setCategory:AVAudioSessionCategoryPlayAndRecord  withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

This goes well.

What is the best microphone recording settings for AVAudio?

I personaly prefer PCM encoding, I've had better quality using it.

try:

NSDictionary *recordSettings = 
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
[NSNumber numberWithInt:2], AVNumberOfChannelsKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
nil];

or to make it more readable:

NSDictionary * recordSetting;
recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

Adjust your function like this:

- (IBAction)playAudio:(id)sender {
[_playButton setEnabled:NO];
if (!_audioRecorder.recording)
{
_stopButton.enabled = YES;
_recordButton.enabled = NO;

//force the session to play to come out of speakers!
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

NSError *error;

_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:_audioRecorder.url
error:&error];
_audioPlayer.delegate = self;
[_audioPlayer prepareToPlay];

if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[_audioPlayer play];
}else{
NSLog(@"errormax");
}
}

App with AVPlayer plays mp4 interrupt iPod music after launched

Finally, I figure out what's wrong.

AudioSessionInitialize(NULL, NULL, NULL, NULL);  
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 allowMixWithOthers = true;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixWithOthers), &allowMixWithOthers);
AudioSessionSetActive(true);

AudioSessionSetActive must be called after AudioSessionSetProperty, it works fine now.

Do I have to reset Audio Session Properties after interruption?

If you are correctly using the Audio session interruption listener, then no, you should not have to reset the properties. You just need to make sure that you actually call kAudioSessionBeginInterruption and kAudioSessionEndInterruption. I am not sure what your listener looks like, but if you are doing something like this:

if (inInterruptionState == kAudioSessionBeginInterruption) {
AudioSessionSetActive(NO);
}
if (inInterruptionState == kAudioSessionEndInterruption) {

AudioSessionSetActive(YES);
}

And are following the rules of Audio Session, Then theoretically, you should not have to reset your properties.

I don't know what you are using the Audio Session for, but you could also pause and resume playback by using the:

kAudioSessionInterruptionType_ShouldResume 

and

kAudioSessionInterruptionType_ShouldNotResume.

You can use these as stated in the Docs:

kAudioSessionInterruptionType_ShouldResume

Indicates that the interruption that has just ended was one for
which it is appropriate to immediately resume playback; for example,
an incoming phone call was rejected by the user.

Available in iOS 4.0 and later.

Declared in AudioSession.h.

kAudioSessionInterruptionType_ShouldNotResume

Indicates that the interruption that has just ended was one for which it is not appropriate to resume playback; for example, your app
had been interrupted by iPod playback.

Available in iOS 4.0 and later.

Declared in AudioSession.h.

You should read the docs because there is a lot of info in there about pausing, resuming, and handling interruptions for the AudioSession.

NOTE:

AudioSession has been deprecated since iOS7. Use AVAudioSession methods instead, or set Pause and Resume option by setting the constant AVAudioSessionInterruptionOptions or AVAudioSessionInterruptionType.

(Available since iOS 6)

AVAudioPlayer turns off iPod - how to work around?

This should work for you (taken from my Ambiance app)

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];

From the docs:

kAudioSessionProperty_OverrideCategoryMixWithOthers
Changes the mixing behavior of the
kAudioSessionCategory_MediaPlayback
and
kAudioSessionCategory_PlayAndRecord
audio session categories. Setting this
property to TRUE (any nonzero value)
allows mixing of iPod audio with
application audio. Other aspects of
these categories, such as their
Ring/Silent switch behavior, are not
affected.

This property has value of FALSE (0)
by default. When the audio session
category changes, such as during an
interruption, the value of this
property reverts to FALSE. To regain
mixing behavior you must then re-set
this property.

Always check to see if setting this
property succeeds or fails, and react
appropriately; behavior may change in
future releases of iPhone OS.

Available in iPhone OS 3.0 and later.

Declared in AudioServices.h.

Avfoundation - Play and record video (along with audio and preview) simultaneously

First, set the moviePlayer to use the application audio session:

moviePlayer.useApplicationAudioSession=YES;

Then, before calling [[captureManager session] startRunning], activate an audio session with its category set to "play and record" and override its property to allow it to mix with others.

// Set audio session category to "play and record"
NSError* error = nil;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]) {
NSLog(@"AVAudioSession setCategory failed: %@", [error localizedDescription]);
}

// Set audio session property "allow mixing" to true so audio can be recorded while it is playing
UInt32 allowMixing = true;
OSStatus status = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
if (status != kAudioSessionNoError) {
NSLog(@"AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers) failed: %ld", status);
}

// Activate the audio session
error = nil;
if (![audioSession setActive:YES error:&error]) {
NSLog(@"AVAudioSession setActive:YES failed: %@", [error localizedDescription]);
}


Related Topics



Leave a reply



Submit