Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

ios - 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];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A little late to the party but for those looking for a solution to the problem of pausing and unpausing background (i.e. 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 (i.e. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...