首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift 2.0中的遥控器(MPNowPlayingInfoCenter)

Swift 2.0中的遥控器(MPNowPlayingInfoCenter)
EN

Stack Overflow用户
提问于 2015-11-27 18:39:06
回答 1查看 1.1K关注 0票数 3

我正试着用Swift做一个无线电应用。我对锁定屏幕上的遥控器有问题。根本不起作用--屏幕上什么都没有。ViewController中的代码:

代码语言:javascript
复制
 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    playButton.setTitle("Play", forState: UIControlState.Normal)
    if NSClassFromString("MPNowPlayingInfoCenter") != nil {
        let image:UIImage = UIImage(named: "logo_player_background")!
        let albumArt = MPMediaItemArtwork(image: image)
        var songInfo: NSMutableDictionary = [
            MPMediaItemPropertyTitle: "Radio Brasov",
            MPMediaItemPropertyArtist: "87,8fm",
            MPMediaItemPropertyArtwork: albumArt
        ]
        MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo as [NSObject : AnyObject]
    }
    if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)) {
        println("Receiving remote control events")
        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    } else {
        println("Audio Session error.")
    }
}

即使在修复MPNowPlayingInfoCenter行之后,锁定屏幕上也不会出现任何内容。我哪里做错了?

代码来自tutorial

EN

回答 1

Stack Overflow用户

发布于 2015-11-28 07:46:49

好了,我已经修好了。问题出在部件AVAudioSession:

代码语言:javascript
复制
if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)) {
    println("Receiving remote control events")
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
} else {
    println("Audio Session error.")

我将其替换为:

代码语言:javascript
复制
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
    try! AVAudioSession.sharedInstance().setActive(true)

遥控器工作正常:)

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

https://stackoverflow.com/questions/33955556

复制
相关文章

相似问题

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