首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPRemoteCommandCenter SomeTimes没有出现

MPRemoteCommandCenter SomeTimes没有出现
EN

Stack Overflow用户
提问于 2018-06-10 23:51:50
回答 1查看 598关注 0票数 0

我有一个音乐播放器,它使用超动力作为音频播放器。我遵循这个文章来实现一个播放器小部件。但是,小部件有时会出现,但很多时候没有。,我希望播放器小部件在有音频播放时出现。

RemoteCommandManager.swift:(从文章中)

AppDelegate.swift

代码语言:javascript
复制
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

代码语言:javascript
复制
func play() {
    let _ = try? AVAudioSession.sharedInstance().setActive(true)
    superpowered.play()
}

更新:

我注意到以下情况:

如果一个iTunes曲目正在播放(即播放器小部件存在),那么我使用我的应用程序播放一个音频文件,player小部件会发生变化,从而显示我的应用程序的信息。

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

EN

回答 1

Stack Overflow用户

发布于 2018-06-19 14:48:26

尝试将audioSession设置为AppDelegate中的active,就在返回函数func应用程序(_ application: UIApplication,didFinishLaunchingWithOptions.

代码语言:javascript
复制
// 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)")
    }

看看这是否有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50789034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档