【问题标题】:locked screen audio controls ios锁屏音频控制ios
【发布时间】:2017-12-28 01:30:08
【问题描述】:

我是 iOS 新手,我正在尝试使用 Objective-c 构建一个应用程序。我遇到的问题是屏幕锁定时无法显示音频控件。

我正在使用 AVPlayer 播放音频。

在 viewDidLoad 中我调用了这个方法

- (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");
}

当我在模拟器中运行应用程序并将屏幕锁定到我沮丧时,没有出现任何控件。任何帮助,将不胜感激。

【问题讨论】:

    标签: ios objective-c xcode ios-simulator


    【解决方案1】:

    所以问题是我的应用未配置为在后台播放音频。我没有意识到,因为在模拟器中,我的应用程序在后台播放音乐就好了。但是,当我在我的实际手机上运行我的应用程序时,它在屏幕锁定时没有播放。

    所以我必须在我的项目的功能选项卡下启用背景音频,并将此代码添加到 viewDidLoad

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    

    另外,音频控件似乎根本没有出现在我的模拟器上。

    【讨论】:

    • 从 Xcode 9b3 开始,后台进程应该在模拟器中暂停,这应该会暂停音频播放。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2017-03-17
    • 2012-06-20
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多