3

I'm trying to make a radio app with Swift. And I have problem with remote controls on the lock screen. Simply doesn't work - nothing on the screen. The code from the 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.")
    }
}

Even after fixing MPNowPlayingInfoCenter line, nothing appears on the lock screen. What I'm doing wrong?

the code is from tutorial

Domysee
  • 12,718
  • 10
  • 53
  • 84
Szekspir
  • 119
  • 13

1 Answers1

4

Okay, I've fix it. The problem was with part AVAudioSession:

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

I've replaced this with:

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

And remote controls are working :)

Szekspir
  • 119
  • 13