即使通过按主页按钮关闭应用程序,也希望此应用程序选项在后台模式下播放音频。
使用下面的代码只能在按下主页按钮两次时处理远程控制事件。但是当应用程序关闭时无法在后台播放应用程序音频。那么,当按下主页按钮关闭应用程序时,如何在后台模式下播放应用程序的 mp3 类型音频,这是我的应用程序中的可听内容。
在 Info.plist 文件中,我添加了选项
所需的背景模式 应用播放音频
- (void) setupAudioSession {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
// Specify that this object is the delegate of the audio session, so that this object's endInterruption method will be invoked when needed.
[audioSession setDelegate: self];
// Assign the Playback category to the audio session.
NSError *audioSessionError = nil;
//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (@"Error setting audio session category.");
return;
}
// Activate the audio session
[audioSession setActive: YES error: &audioSessionError];
if (audioSessionError != nil) {
NSLog (@"Error activating audio session during initial setup.");
return;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
//[player play];
[self playAction];
// } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
// [player pause];
} else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) {
[self rewButtonPressed];
} else if (event.subtype == UIEventSubtypeRemoteControlNextTrack)
[self ffwButtonPressed:nil];
}}
当按下主页按钮时,我在代码中缺少的应用程序的音频没有在后台播放。
感谢任何帮助。
谢谢