暂停的音频开始在applicationWillEnterForeground上自动播放:

Mus*_*afa 6 avaudioplayer ios avaudiosession

applicationDidEnterBackground:触发器,我暂停其使用播放音频AVAudioPlayer:

[self.avPlayer pause];
Run Code Online (Sandbox Code Playgroud)

现在,当applicationWillEnterForeground:触发时,音频开始自动播放!由于我没有启动音频,因此用户界面未更新,并且显示音频仍处于暂停状态.

这是怎么回事?这种情况发生在iOS 6.x,iPad 2上.这个问题不能在运行iOS 5.x的旧iPad上重现.

这是我设置的方式AVAudioSession:

// Setup the audio session
BOOL succeeded = NO;
NSError *sessionError = nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setDelegate:self];

if ([session respondsToSelector:@selector(setPreferredSampleRate:error:)]) {
    succeeded = [session setPreferredSampleRate:128000.0f error:&sessionError];

} else {
    succeeded = [session setPreferredHardwareSampleRate:128000.0f error:&sessionError];
}

succeeded = [session setCategory:AVAudioSessionCategorySoloAmbient error:&sessionError];
succeeded = [session setActive:YES error:&sessionError];
Run Code Online (Sandbox Code Playgroud)

HRM*_*HRM 1

In one of my recent app, I found a similar issue where audio playback pauses automatically and resumes after you are interrupted by a call and GUI is not refreshing in my app. I have fixed the issue by using the following method:

Register this in the class where you handle player code

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Run Code Online (Sandbox Code Playgroud)

And use AVAudioSession's audioPlayerEndInterruption delegate method to get control after the application was resumed. In this function you can resume the playback and update UI accordingly.

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。