Am working in Messaging based iPhone app. I have added Beep sound to receive message from someone else. I am playing beep sound using AVAudioPlayer. My problem is when the user hearing song from someother applications in background if they receive message from my application the beep sound will be play and the background song/audio stopped not resuming.
I want to play my beep sound by pausing the background song/audio and again i have to resume that audio/song from my application. It is possible in iOS developing? Can anyone please help me?
EDITED:
This is my sample code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Music";
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"basicsound" ofType:@"wav"];
NSURL *audioURL = [[NSURL alloc] initWithString:urlString];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:YES error:&error];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
[audioPlayer play];
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag == YES)
{
NSLog(@"Audio Played successfully");
NSError *error;
[audioSession setActive:NO error:&error];
}
}
You cannot do this for all applications. For applications that use the iPod player in their applications:
Assuming your application is in foreground. You can do this:
This needs the
MediaPlayer.frameworkand also#import <MediaPlayer/MediaPlayer.h>.Read more at MPMusicPlayerController Reference.
For doing it through Audio Player Sessions you can use the property
kAudioSessionCategory_SoloAmbientSound. Reference here.This is similar to
AVAudioSessionCategorySoloAmbientdefined in AVAudioSession class.Quoting from the documentation: