|
@@ -108,6 +108,9 @@ class NCPlayerToolBar: UIView {
|
|
|
playButton.isEnabled = false
|
|
|
forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.15", color: .lightGray), for: .normal)
|
|
|
forwardButton.isEnabled = false
|
|
|
+
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: AVAudioSession.interruptionNotification, object: nil)
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange), name: AVAudioSession.routeChangeNotification, object: nil)
|
|
|
}
|
|
|
|
|
|
deinit {
|
|
@@ -116,6 +119,59 @@ class NCPlayerToolBar: UIView {
|
|
|
if self.timeObserver != nil {
|
|
|
appDelegate.player?.removeTimeObserver(self.timeObserver!)
|
|
|
}
|
|
|
+
|
|
|
+ NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: nil)
|
|
|
+ NotificationCenter.default.removeObserver(self, name: AVAudioSession.routeChangeNotification, object: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Handle Notifications
|
|
|
+ @objc func handleRouteChange(notification: Notification) {
|
|
|
+ guard let userInfo = notification.userInfo, let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else { return }
|
|
|
+
|
|
|
+ switch reason {
|
|
|
+ case .newDeviceAvailable:
|
|
|
+ let session = AVAudioSession.sharedInstance()
|
|
|
+ for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.headphones {
|
|
|
+ print("headphones connected")
|
|
|
+ DispatchQueue.main.sync {
|
|
|
+ //self.play()
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ case .oldDeviceUnavailable:
|
|
|
+ if let previousRoute =
|
|
|
+ userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
|
|
|
+ for output in previousRoute.outputs where output.portType == AVAudioSession.Port.headphones {
|
|
|
+ print("headphones disconnected")
|
|
|
+ DispatchQueue.main.sync {
|
|
|
+ //self.pause()
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ default: ()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func handleInterruption(notification: Notification) {
|
|
|
+ guard let userInfo = notification.userInfo, let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { return }
|
|
|
+
|
|
|
+ if type == .began {
|
|
|
+ print("Interruption began")
|
|
|
+ // Interruption began, take appropriate actions
|
|
|
+ } else if type == .ended {
|
|
|
+ if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
|
|
|
+ let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
|
|
|
+ if options.contains(.shouldResume) {
|
|
|
+ // Interruption Ended - playback should resume
|
|
|
+ print("Interruption Ended - playback should resume")
|
|
|
+ //play()
|
|
|
+ } else {
|
|
|
+ // Interruption Ended - playback should NOT resume
|
|
|
+ print("Interruption Ended - playback should NOT resume")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func setBarPlayer(ncplayer: NCPlayer, timeSeek: CMTime, metadata: tableMetadata) {
|
|
@@ -381,6 +437,8 @@ class NCPlayerToolBar: UIView {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//MARK: - Remote Command Center
|
|
|
+
|
|
|
extension NCPlayerToolBar {
|
|
|
|
|
|
func setupRemoteTransportControls() {
|
|
@@ -437,4 +495,45 @@ extension NCPlayerToolBar {
|
|
|
// Set the metadata
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
|
|
}
|
|
|
+
|
|
|
+ // MARK: AVAudioPlayerDelegate
|
|
|
+ func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
|
|
|
+ print("Audio player did finish playing: \(flag)")
|
|
|
+ if (flag) {
|
|
|
+ updateNowPlaying(isPause: true)
|
|
|
+ //playPauseButton.setTitle("Play", for: UIControl.State.normal)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ // MARK: Actions
|
|
|
+ @IBAction func togglePlayPause(_ sender: Any) {
|
|
|
+ if (player.isPlaying) {
|
|
|
+ pause()
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ play()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func play() {
|
|
|
+ player.play()
|
|
|
+ playPauseButton.setTitle("Pause", for: UIControl.State.normal)
|
|
|
+ updateNowPlaying(isPause: false)
|
|
|
+ print("Play - current time: \(player.currentTime) - is playing: \(player.isPlaying)")
|
|
|
+ }
|
|
|
+
|
|
|
+ func pause() {
|
|
|
+ player.pause()
|
|
|
+ playPauseButton.setTitle("Play", for: UIControl.State.normal)
|
|
|
+ updateNowPlaying(isPause: true)
|
|
|
+ print("Pause - current time: \(player.currentTime) - is playing: \(player.isPlaying)")
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func stop(_ sender: Any) {
|
|
|
+ player.stop()
|
|
|
+ player.currentTime = 0
|
|
|
+ playPauseButton.setTitle("Play", for: UIControl.State.normal)
|
|
|
+ }
|
|
|
+ */
|
|
|
}
|