MPRemoteCommandCenter SomeTimes不显示

时间:2018-06-10 23:51:50

标签: ios swift superpowered mpremotecommandcenter mpmedialibrary

我有一个使用Superpowered作为音频播放器的音乐播放器。我按照这个article来实现播放器小部件。但是,窗口小部件有时会显示,而且很多次都没有显示。 我希望玩家小工具在播放音频时显示。

RemoteCommandManager.swift :(来自文章)

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.beginReceivingRemoteControlEvents()
        // Override point for customization after application launch.

        // Initializer the `RemoteCommandManager`.
        remoteCommandManager = RemoteCommandManager()

        // Always enable playback commands in MPRemoteCommandCenter.
        remoteCommandManager.activatePlaybackCommands(true)

        // Setup AVAudioSession to indicate to the system you how intend to play audio.
        let audioSession = AVAudioSession.sharedInstance()

        do {
            try audioSession.setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault)
        }
        catch {
            print("An error occured setting the audio session category: \(error)")
        }


        return true
}

PlayerManager.swift

func play() {
    let _ = try? AVAudioSession.sharedInstance().setActive(true)
    superpowered.play()
}

更新

我注意到以下情况:

如果正在播放iTunes曲目(即播放器小部件存在),那么我使用我的应用播放音频文件,播放器小部件会更改,以便显示我的应用的信息。

我的猜测是,当我激活音频会话时,播放器小部件从iTunes切换到我的应用程序。但是,当播放器窗口小部件不存在时,激活音频会话将无法显示它。

1 个答案:

答案 0 :(得分:0)

在返回函数的上方,尝试将audioSession设置为在AppDelegate中处于活动状态, func application(_ application:UIApplication,didFinishLaunchingWithOptions ....

// Set the AVAudioSession as active.  This is required so that your application becomes the "Now Playing" app.
    do {
        try audioSession.setActive(true, with: [])
    }
    catch {
        print("An Error occured activating the audio session: \(error)")
    }

并检查是否有帮助。