0

I make an alarm feature for my app, at the time I setup for alarm fire I can play a music track with AVAudioPlayer, it worked well on background mode. But if at this time I opening Youtube app and playing a video then the AVAudioPlayer not work.

Please tell me why this happen and how to resolve this issue, how to play both at the same time

This is code I implement:

NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"remind2"
                                          withExtension:@"mp3"];
avSound = [[AVAudioPlayer alloc]
            initWithContentsOfURL:soundURL error:nil];
[avSound setDelegate:self];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[avSound prepareToPlay];
[avSound setNumberOfLoops:10];
[avSound setVolume:1.0];
[avSound play];

Thanks!

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
nghien_rbc
  • 133
  • 1
  • 12

2 Answers2

0

This is happened because of Youtube app also set property "UIBackgroundMode" property in plist. And even if you check any of other Music app like gaana, savan, etc. this will happened in all of the music app.

Monika Patel
  • 2,287
  • 3
  • 20
  • 45
0

Replace this line

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

by this:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

and I resolved my problem

It takes me two days to put the right questions to this problem, I only find the solution when i ask Google why Youtube stop my audio stream and this result search help me

AVAudioPlayer turns off iPod - how to work around?

Community
  • 1
  • 1
nghien_rbc
  • 133
  • 1
  • 12