锁定屏幕音频控制ios
我是iOS新手,我正在尝试使用objective-c构建一个应用程序。我遇到的问题是,当屏幕锁定时,我无法显示音频控件 我正在用AVPlayer播放音频 在viewDidLoad中,我调用此方法锁定屏幕音频控制ios,ios,objective-c,xcode,ios-simulator,Ios,Objective C,Xcode,Ios Simulator,我是iOS新手,我正在尝试使用objective-c构建一个应用程序。我遇到的问题是,当屏幕锁定时,我无法显示音频控件 我正在用AVPlayer播放音频 在viewDidLoad中,我调用此方法 - (void)screenLocked{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) name:UIApplicationDidEnterBac
- (void)screenLocked{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
这个方法调用这个
- (void)appDidEnterBackground:(NSNotification *)notification {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
MPRemoteCommandCenter *remoteCommandCenter = [MPRemoteCommandCenter
sharedCommandCenter];
[[remoteCommandCenter playCommand] addTarget:self
action:@selector(test)];
[[remoteCommandCenter skipForwardCommand] addTarget:self
action:@selector(test)];
[[remoteCommandCenter skipBackwardCommand] addTarget:self
action:@selector(test)];
[[remoteCommandCenter togglePlayPauseCommand] addTarget:self
action:@selector(test)];
[[remoteCommandCenter pauseCommand] addTarget:self
action:@selector(test)];
}
- (void)test{
NSLog(@"Logging");
}
当我在模拟器中运行应用程序并锁定屏幕时,没有任何控件出现。任何帮助都将不胜感激 因此,问题在于我的应用程序没有配置为在后台播放音频。我没有意识到这一点,因为在模拟器中,我的应用程序在后台播放音乐的效果很好。然而,当我在我的实际手机上运行我的应用程序时,当屏幕被锁定时,它不会播放 因此,我必须在项目的“功能”选项卡下启用背景音频,并在viewDidLoad中添加此代码
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
此外,我的模拟器上似乎根本没有音频控件。从Xcode 9b3开始,应暂停模拟器中的后台进程,暂停音频播放。