從其他文章衍生出來,我成功地爲音樂播放器提供了一切。設置MPNowPlayingInfoCenter時,應用程序丟失遠程控制輸入
最後一步將設置MPNowPlayingInfoCenter
值。
不幸的是,只要我這樣做,應用程序會失去接收remoteControlEvents的能力,我應該在任何其他View Controller中設置屬性。這同樣適用於把應用程序在後臺從任何其他VC比「音樂VC」(這是一個標籤式應用程序和音樂只是其中的一個選項卡)
我的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
}
}
}
}
顯示另一個VC時FirstResponder是否自動更改?感謝提示,以解決這個問題。
我有這個問題。我可以使用這個工具來處理除WKWebView以外的多個播放源。我已經resignedFirstResponder之前和之後源開始播放和更新nowPlayingInfo沒有運氣。有任何想法嗎? – pir800