Derived from some other posts I was successfully able to get everything right for a music player.
Last step would be to set MPNowPlayingInfoCenter
values.
Unfortunately as soon as I do this, the app loses capability to receive remoteControlEvents should i be in any other View Controller than the one that set up the properties. Same goes for putting the App in Background from any other VC than the "Music VC" (It is a tabbed Application and Music is just one Tab)
My AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//here is other stuff for push-notifications etc.
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
return true
}
MusicVC:
class MusicVC:UIViewController, UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource, UICollectionViewDelegate {
var myPlayer:AVQueuePlayer!
override func viewDidLoad() {
super.viewDidLoad()
//tableView and other setup
self.becomeFirstResponder()
}
//MARK: - InfoCenter Methods
func setMediaCenterInfo() {
let mpic = MPNowPlayingInfoCenter.defaultCenter()
var albumArtWork = MPMediaItemArtwork(image: UIImage(named:"testImage"))
var fullString = self.currentSong.title as String
var splitArray = fullString.componentsSeparatedByString(" - ")
var artistName: String = splitArray[0]
var titleString: String? = splitArray.count > 1 ? splitArray[1] : nil
if titleString != nil {
mpic.nowPlayingInfo =
[
MPMediaItemPropertyArtwork:albumArtWork,
MPMediaItemPropertyTitle:titleString!,
MPMediaItemPropertyArtist:artistName
]
} else {
mpic.nowPlayingInfo =
[
MPMediaItemPropertyArtwork:albumArtWork,
MPMediaItemPropertyTitle:fullString
]
}
//Does get set correctly and shows in Info Center as well as Lock-Screen
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func remoteControlReceivedWithEvent(event: UIEvent) {
if event.type == UIEventType.RemoteControl {
switch event.subtype {
case .RemoteControlTogglePlayPause:
println("TOGGLE PLAY PAUSE")
//self.playToggleTapped(self)
case .RemoteControlPlay:
println("ONLY PLAY BUTTON")
//self.playToggleTapped(self)
case .RemoteControlPause:
println("ONLY PAUSE BUTTON")
//self.playToggleTapped(self)
case .RemoteControlNextTrack:
println("next")
//self.nexButtonTapped(self)
case .RemoteControlPreviousTrack:
//self.previousButtonTapped(self)
println("previous")
default:
break
}
}
}
}
Does the FirstResponder change automatically when showing another VC? Grateful for hints to fix this.