【问题标题】:Remote Controls in Swift 2.0 (MPNowPlayingInfoCenter)Swift 2.0 中的远程控制 (MPNowPlayingInfoCenter)
【发布时间】:2016-03-01 13:09:03
【问题描述】:

我正在尝试使用 Swift 制作广播应用。而且我在锁定屏幕上的遥控器有问题。根本不起作用 - 屏幕上什么都没有。来自 ViewController 的代码:

 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

【问题讨论】:

    标签: xcode lockscreen remote-control mpnowplayinginfocenter


    【解决方案1】:

    好的,我已经解决了。问题出在 AVAudioSession 部分:

    if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)) {
        println("Receiving remote control events")
        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    } else {
        println("Audio Session error.")
    

    我已将其替换为:

    try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
        try! AVAudioSession.sharedInstance().setActive(true)
    

    并且遥控器正在工作:)

    【讨论】:

    • 你让我很开心 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2015-08-06
    • 2016-01-07
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多