简体   繁体   中英

How to stop and resume background audio from iPhone app?

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

A little late to the party but for those looking for a solution to the problem of pausing and unpausing background (ie ipod music) after playing sounds, you should be using the following when deactivating your audio session.

[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

//New method that's not deprecated:

[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

Whenever you do not require the audio session in your application (ie when you're not outputting any sound) you should be deactivating it. Where it makes sense, you should use this method so that any background audio is notified and may resume (applications receiving this notification do not necessarily have to resume).

In addition, you must use the appropriate audio category to allow your sound to play exclusively where required. This can be done with any of the categories other than AVAudioSessionCategoryAmbient . This will automatically pause the background audio for you when your Audio session becomes active. It will not, however, reactivate any background audio, that is done by using the above code. In which case, as stated earlier, background audio then decides what to do with the notification.

Side note, another option to look into is audio 'ducking'. If it's not imperative that your sound plays alone, such as a simple alert sound, try using ducking which will lower the volume of background audio to play your sound and when complete will raise the background audio back to normal when you're sound has finished playing. See Configure your Audio Session for more details on these concepts.

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:

MPMusicPlayerController *mp = [MPMusicPlayerController iPodMusicPlayer];        
[mp stop]; // or [mp pause]

This needs the MediaPlayer.framework and 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 AVAudioSessionCategorySoloAmbient defined in AVAudioSession class .

Quoting from the documentation:

When you use this category, audio from other apps is silenced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM