我想播放音乐背景,为此我在 info.plist 中进行了更改。我将应用程序不在后台运行设置为NO,应用程序播放音频所需的后台模式。
我在 AppDelegate 中添加了这个:
- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (void)applicationDidEnterBackgroundUIApplication *)application {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Activating audio session");
if (![audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error]) {
NSLog(@"Unable to set audio session category: %@", error);
}
BOOL result = [audioSession setActive:YES error:&error];
if (!result) {
NSLog(@"Error activating audio session: %@", error);
}
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
但是当我按下主页按钮时,音乐不再播放。请问我哪里错了?
Best Answer-推荐答案 strong>
并在 homeBtnEventMethod 上的 homeBtn 屏幕上播放音频
1.) 在 viewController.h #import
2.) @property (nonatomic, strong) AVAudioPlayer *theAudio;
3.) 在 viewController.m 在 viewDidLoad
NSError *error = nil;
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
4.) - (IBAction)homeBtnTpd {
[self.theAudio play];
}
关于ios - 按下主页按钮时在后台播放音乐,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/32369391/
|