From 2f9a5481ffeb3c05e33c69638eff928b7ee9dec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Henrichsen?= Date: Fri, 26 Oct 2018 13:09:49 +0200 Subject: [PATCH] Removed warnings --- .../AVPlayerWrapper/AVPlayerWrapper.swift | 7 +++ SwiftAudio/Classes/AudioPlayer.swift | 16 ++--- .../RemoteCommandController.swift | 62 ++++--------------- 3 files changed, 28 insertions(+), 57 deletions(-) diff --git a/SwiftAudio/Classes/AVPlayerWrapper/AVPlayerWrapper.swift b/SwiftAudio/Classes/AVPlayerWrapper/AVPlayerWrapper.swift index d799664..2270ac3 100644 --- a/SwiftAudio/Classes/AVPlayerWrapper/AVPlayerWrapper.swift +++ b/SwiftAudio/Classes/AVPlayerWrapper/AVPlayerWrapper.swift @@ -66,6 +66,13 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol { } var duration: TimeInterval { + + if let timeRange = self.avPlayer.currentItem?.loadedTimeRanges[0].timeRangeValue { + let duration = CMTimeGetSeconds(timeRange.duration) + print(duration) + } + + if let seconds = currentItem?.duration.seconds, !seconds.isNaN { return seconds } diff --git a/SwiftAudio/Classes/AudioPlayer.swift b/SwiftAudio/Classes/AudioPlayer.swift index bebae7a..14bebd3 100644 --- a/SwiftAudio/Classes/AudioPlayer.swift +++ b/SwiftAudio/Classes/AudioPlayer.swift @@ -151,22 +151,22 @@ public class AudioPlayer: AVPlayerWrapperDelegate { /** Toggle playback status. */ - public func togglePlaying() throws { - try self.wrapper.togglePlaying() + public func togglePlaying() { + self.wrapper.togglePlaying() } /** Start playback */ - public func play() throws { - try self.wrapper.play() + public func play() { + self.wrapper.play() } /** Pause playback */ - public func pause() throws { - try self.wrapper.pause() + public func pause() { + self.wrapper.pause() } /** @@ -180,8 +180,8 @@ public class AudioPlayer: AVPlayerWrapperDelegate { /** Seek to a specific time in the item. */ - public func seek(to seconds: TimeInterval) throws { - try self.wrapper.seek(to: seconds) + public func seek(to seconds: TimeInterval) { + self.wrapper.seek(to: seconds) } // MARK: - Remote Command Center diff --git a/SwiftAudio/Classes/RemoteCommandController/RemoteCommandController.swift b/SwiftAudio/Classes/RemoteCommandController/RemoteCommandController.swift index a4b0890..a5d23cb 100644 --- a/SwiftAudio/Classes/RemoteCommandController/RemoteCommandController.swift +++ b/SwiftAudio/Classes/RemoteCommandController/RemoteCommandController.swift @@ -100,26 +100,16 @@ public class RemoteCommandController { private func handlePlayCommandDefault(event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { if let audioPlayer = self.audioPlayer { - do { - try audioPlayer.play() - return MPRemoteCommandHandlerStatus.success - } - catch let error { - return self.getRemoteCommandHandlerStatus(forError: error) - } + audioPlayer.play() + return MPRemoteCommandHandlerStatus.success } return MPRemoteCommandHandlerStatus.commandFailed } private func handlePauseCommandDefault(event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { if let audioPlayer = self.audioPlayer { - do { - try audioPlayer.pause() - return MPRemoteCommandHandlerStatus.success - } - catch let error { - return self.getRemoteCommandHandlerStatus(forError: error) - } + audioPlayer.pause() + return MPRemoteCommandHandlerStatus.success } return MPRemoteCommandHandlerStatus.commandFailed } @@ -134,13 +124,8 @@ public class RemoteCommandController { private func handleTogglePlayPauseCommandDefault(event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { if let audioPlayer = self.audioPlayer { - do { - try audioPlayer.togglePlaying() - return MPRemoteCommandHandlerStatus.success - } - catch let error { - return self.getRemoteCommandHandlerStatus(forError: error) - } + audioPlayer.togglePlaying() + return MPRemoteCommandHandlerStatus.success } return MPRemoteCommandHandlerStatus.commandFailed } @@ -149,13 +134,8 @@ public class RemoteCommandController { if let command = event.command as? MPSkipIntervalCommand, let interval = command.preferredIntervals.first, let audioPlayer = self.audioPlayer { - do { - try audioPlayer.seek(to: audioPlayer.currentTime + Double(truncating: interval)) - return MPRemoteCommandHandlerStatus.success - } - catch let error { - return self.getRemoteCommandHandlerStatus(forError: error) - } + audioPlayer.seek(to: audioPlayer.currentTime + Double(truncating: interval)) + return MPRemoteCommandHandlerStatus.success } return MPRemoteCommandHandlerStatus.commandFailed } @@ -164,13 +144,8 @@ public class RemoteCommandController { if let command = event.command as? MPSkipIntervalCommand, let interval = command.preferredIntervals.first, let audioPlayer = self.audioPlayer { - do { - try audioPlayer.seek(to: audioPlayer.currentTime - Double(truncating: interval)) - return MPRemoteCommandHandlerStatus.success - } - catch let error { - return self.getRemoteCommandHandlerStatus(forError: error) - } + audioPlayer.seek(to: audioPlayer.currentTime - Double(truncating: interval)) + return MPRemoteCommandHandlerStatus.success } return MPRemoteCommandHandlerStatus.commandFailed } @@ -178,13 +153,8 @@ public class RemoteCommandController { private func handleChangePlaybackPositionCommandDefault(event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus { if let event = event as? MPChangePlaybackPositionCommandEvent, let audioPlayer = self.audioPlayer { - do { - try audioPlayer.seek(to: event.positionTime) - return MPRemoteCommandHandlerStatus.success - } - catch let error { - return self.getRemoteCommandHandlerStatus(forError: error) - } + audioPlayer.seek(to: event.positionTime) + return MPRemoteCommandHandlerStatus.success } return MPRemoteCommandHandlerStatus.commandFailed } @@ -216,13 +186,7 @@ public class RemoteCommandController { } private func getRemoteCommandHandlerStatus(forError error: Error) -> MPRemoteCommandHandlerStatus { - if let error = error as? APError.PlaybackError { - switch error { - case .noLoadedItem: - return MPRemoteCommandHandlerStatus.noActionableNowPlayingItem - } - } - else if let error = error as? APError.LoadError { + if let error = error as? APError.LoadError { switch error { case .invalidSourceUrl(_): return MPRemoteCommandHandlerStatus.commandFailed