按下主页按钮时在后台播放音乐
Play music in background when home button is press
我想播放音乐背景,为此我在 info.plist 中进行了更改。我将应用程序在后台 运行 设置为 NO,将所需的后台模式设置为 App plays audio.
我在 AppDelegate 中添加了这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (void)applicationDidEnterBackground:(UIApplication *)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];
}
但是当我按下主页按钮时,音乐不再播放。请问我哪里错了?
您应该将 Applications does not run in background
设置为否
并在 homeBtnEventMethod 的 homeBtn 屏幕上播放音频
1.) 在 viewController.h #import <AVFoundation/AVAudioPlayer.h>
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];
}
我想播放音乐背景,为此我在 info.plist 中进行了更改。我将应用程序在后台 运行 设置为 NO,将所需的后台模式设置为 App plays audio.
我在 AppDelegate 中添加了这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (void)applicationDidEnterBackground:(UIApplication *)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];
}
但是当我按下主页按钮时,音乐不再播放。请问我哪里错了?
您应该将 Applications does not run in background
设置为否
并在 homeBtnEventMethod 的 homeBtn 屏幕上播放音频
1.) 在 viewController.h #import <AVFoundation/AVAudioPlayer.h>
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];
}