Resume background music after using AVCaptureSession which addOutput with AVCaptureMovieFileOutPut - ios

I am using AVCaptureSession method addOutput with AVCaptureMovieFileOutput object. As soon as addOutput gets called, background music gets killed, and what I do is I call removeOutput when video recording is done. But the background music won't be resumed.
I tried the code as blow:
AVAudioSession *audioSession=[AVAudioSession sharedInstance];
if (audioSession) {
[audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}
But it does't work for me.
Furthermore, I tried the MPMusicPlayerController method as blow:
MPMusicPlayerController *mp = [MPMusicPlayerController systemMusicPlayer];
[mp play];
It can resume system music, but it doesn't go well for 3rd part apps.
Is there any other methods can fix this issue?
Thank you in advance.

In target Capabilities background modes enable and check Audio,Airplay
NSURL *soundUrl = [NSURL URLWithString:#"Audiourl"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer play];

Related

Set output speaker for openTok?

I try to make an audio/video call with openTok with the possibility to change de output speaker. I want to be set on ear speaker for default and i use that code.
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
self.audioSpeakerButton.selected =true;
First time i play a ringtone with AVAudioPlayer with code below:
audioPLayer =[[AVAudioPlayer alloc]initWithContentsOfURL:pewPewURL fileTypeHint:#"mp3" error:nil];
audioPLayer.numberOfLoops=-1;
[audioPLayer prepareToPlay];
[audioPLayer play];
The sound comes from ear speaker which is good.
After when the second user is connected i stop the audioPlayer but the sound it is comes from loudspeaker which is bad.
- (void)subscriberDidConnectToStream:(OTSubscriberKit *)subscriber
{
[audioPLayer pause];
[audioPLayer stop];
audioPLayer = nil;
}
I use that button action for change output speaker but nothing is changed.
-(void)audioSpeakerButtonAction{
if (![self.audioSpeakerButton isSelected]) {
NSError *error = nil;
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
self.audioSpeakerButton.selected = YES;
} else {
NSError *error = nil;overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
self.audioSpeakerButton.selected = NO;
}
}
I will appreciate any help. Thanks in advance!

The AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation can not work

The method:
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]
does not work for resuming background music.
My app audio session category is AVAudioSessionCategoryPlayAndRecord.
Who knows what's going on?

interrupted music can not be wake up in safari or other browsers

In my app ,i play my music in this way :
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
AVAudioPlayer *pl = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fileName] error:nil];
[pl play];
Now other music had been interrupt.After my music been played,i resume playback like this:
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
It works perfect for me, when music played by some media player(as:QQ Player, iPhone's music, etc.);
But it was inefficacy, when the first music played by safari,chrome and so on.
How could i resume palyback?

AVPlayer not muting with silent switch state

I have a game which is using AVPlayer to stream remote tracks. This music is background audio in the game, so I need it to respect the silent/mute switch on the device.
Following Apple's documentation, I am doing the following:
NSError *error;
NSError *activationError;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient error:&error];
[audioSession setActive:YES error:&activationError];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:#"http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3"]];
self.player = [[AVPlayer alloc] init];
[self.player replaceCurrentItemWithPlayerItem:item];
[self.player play];
// error = nil, and activationError = nil at this point
I have tried this approach for both remote and local files. The files play and can be heard when the device is silent mode, whereas I'm expecting AVPlayer to mute the playback when the silent switch is on.

AVAudioPlayer doesn't play audio in iPhone 5 when locked

Using AVAudioPlayer I'm trying to play sounds while the iphone player is playing. Also when device is locked.
The thing is, that in iPhone 4s ios 7 - works fine.
But on iPhone 5 with 6 and 7 ios nothing happens.
In the -Info.plist in Required background modes I wrote
App plays audio or streams audio/video using AirPlay
in Player.h implemented:
#property (nonatomic, strong) AVAudioPlayer *notificationPlayer;
<AVAudioPlayerDelegate, AVAudioSessionDelegate> with
#import <AVFoundation/AVFoundation.h> included
Also in Player.m
//this is called on viewDidLoad
-(void) prepareSound{
[[AVAudioSession sharedInstance] setDelegate:self];
}
//overriden function
- (void)playAudio:(NSString *)path{
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryAmbient
withOptions: AVAudioSessionCategoryOptionDuckOthers
error: nil];
NSError *error;
NSURL *audioURL = [NSURL fileURLWithPath:path];
self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&error];
self.notificationPlayer.delegate = self;
[self.notificationPlayer prepareToPlay];
[self.notificationPlayer setVolume:1.0];
[self.notificationPlayer play];
if (error) {
NSLog(#"error %#",[error localizedDescription]);
}
}
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player stop];
[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient
withOptions: 0
error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];
}
//Call from here
-(void) customMethod{
[self playAudio:[[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"]];
}
Regards.
Another Really important thing to note while playing sounds in background is whether or not you have permission to the file. If you are playing a bundle file, you have permission, and presumably some file streamed from the internet, but if you've downloaded media into the documents directory, then you must set permissions appropriately, especially if your application natively locks files.
This tends to match with what many people see as a symptom where the file will continue to play for a couple of seconds, 5-15, then stop. If you're listening for the finished method, you'll see an error flag. Why: AVAudioPlayer won't load the entirety of your audio file into memory when you first play it.
Example:
NSURL url = [NSURL URLWithString:#""];
NSError *error = nil;
[[NSFileManager defaultManager] setAttributes:#{NSFileProtectionKey :
NSFileProtectionCompleteUntilFirstUserAuthentication} ofItemAtPath:[url path] error:&error];
Your options are as follows:
NSFileProtectionComplete;
NSFileProtectionCompleteUnlessOpen;
NSFileProtectionCompleteUntilFirstUserAuthentication;
NSFileProtectionNone;
I should use the following code
- (void)playAudio:(NSString *)path {
NSError *error;
NSURL *audioURL = [NSURL fileURLWithPath:path];
self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&error];
self.notificationPlayer.delegate = self;
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
NSError *errorSession = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil];
[audioSession setActive:NO error:&errorSession];
[self.notificationPlayer prepareToPlay];
[self.notificationPlayer setVolume:1.0];
[self.notificationPlayer play];
if (error) {
NSLog(#"error %#",[error localizedDescription]);
}
NSLog(#"Should play music");
}
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player stop];
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}

Resources