AVPlayer not muting with silent switch state - ios

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.

Related

AVAudioPlayer sound too low at starting, after some sound get louder

In my situation, when I pressed play button sound comes low at starting and after some time it comes louder for speaker. In my project too many audio files are available. When I pressed first then it starts with low sound and when I pressed second and third it starts and all played in loud sound from speaker. Every time when I pressed play button, it executed below code.
- (SWYAudioBoardPlayer*) initWithMediaKey:(NSString *) mediaKey{
self.session = [AVAudioSession sharedInstance]; // Object declare above
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:#"audioUrl" error:&error];
[audioPlayer setDelegate:self];
isPause = false;
audioPlayer.volume = 1.0;
defaultAVAudioSessionCategory = [self.session category]; // Object declare above
[self loudSpeakerOn:true];
return [self init]; }
- (void) loudSpeakerOn:(BOOL)isLoudSpeaker{
[self.session setActive:NO error:nil];
BOOL success;
NSError* error;
success = [self.session overrideOutputAudioPort:isLoudSpeaker?AVAudioSessionPortOverrideSpeaker:AVAudioSessionPortOverrideNone error:&error];
if (!success) NSLog(#"AVAudioSession error setting category:%#",error);
[self.session setActive:YES error:nil];}

Resume background music after using AVCaptureSession which addOutput with AVCaptureMovieFileOutPut

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];

iOS app sound only coming through headphones or when ringer not muted

I am using in my
viewDidLoad:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];
if (!success) { /**handle the error condition */ }
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /**handle the error condition */ }
NSURL *SoundURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"music1" ofType:#"caf"]];
and have initialized in the interface
AVAudioPlayer *playSoundURL1;
and called in my method
[playSoundURL1 play];
Sound plays perfectly fine when I'm using headphones when ringer is silent or not silent. However, without any headphones I can only hear the sound when the ringer is not silenced.

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?

iOS How to play alert sound in speaker when recording

I want to manually trigger to play some alert sound during recording audio, and I want the alert sound be recorded. So I want the alert play loudly in speaker instead of Receiver during recording audio.
I use AVAudioRecorder to record audio and use AVAudioPlayer to play the alert sound.
Code for recorder
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryRecord error:&sessionError];
if(session == nil)
NSLog(#"Error creating session: %#", [sessionError description]);
else
[session setActive:YES error:nil];
recorder = [[AVAudioRecorder alloc] initWithURL:_recordedFile settings:recordSettings error:nil];
[recorder prepareToRecord];
[recorder record];
Code for player
AVAudioSession *session = [AVAudioSession sharedInstance];
// session.outputDataSources
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
// use the louder speaker
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (audioRouteOverride),&audioRouteOverride);
if(session == nil)
NSLog(#"Error creating session: %#", [sessionError description]);
else
[session setActive:YES error:nil];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.filePath error:&error];
player.delegate =self;
player.volume = 1.0;
player.numberOfLoops = 0;
self.timeDuration = player.duration;
If I stop the recording, and manually trigger the play alert, it plays in speaker.
But during recording, when I manually trigger the play alert, nothing happens.
Is there anything wrong here? Or Is it possible to play sound in speaker when recording?
Just change category as below thats all
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

Resources