From bf2dae9569d77dbb0847fb8455f89ee67172e170 Mon Sep 17 00:00:00 2001 From: Tanha Date: Mon, 25 Nov 2019 23:19:29 -0800 Subject: [PATCH] add documentation for fields of SAPlayer --- Source/LockScreenViewProtocol.swift | 11 +++--- Source/SAPlayer.swift | 61 +++++++++++++++++++++++------ Source/SAPlayerPresenter.swift | 6 +-- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/Source/LockScreenViewProtocol.swift b/Source/LockScreenViewProtocol.swift index 3b63a69..160c407 100644 --- a/Source/LockScreenViewProtocol.swift +++ b/Source/LockScreenViewProtocol.swift @@ -36,9 +36,14 @@ protocol LockScreenViewProtocol { extension LockScreenViewProtocol { @available(iOS 10.0, *) - func setLockScreenInfo(withMediaInfo info: SALockScreenInfo, duration: Duration) { + func setLockScreenInfo(withMediaInfo info: SALockScreenInfo?, duration: Duration) { var nowPlayingInfo:[String : Any] = [:] + guard let info = info else { + MPNowPlayingInfoCenter.default().nowPlayingInfo = [:] + return + } + let title = info.title let artist = info.artist let releaseDate = info.releaseDate @@ -68,10 +73,6 @@ extension LockScreenViewProtocol { } } - - - - MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo } diff --git a/Source/SAPlayer.swift b/Source/SAPlayer.swift index 062d754..0b13389 100644 --- a/Source/SAPlayer.swift +++ b/Source/SAPlayer.swift @@ -31,40 +31,71 @@ public class SAPlayer { private var presenter: SAPlayerPresenter! private var player: AudioEngine? + /** + Corresponding to the skipping forward button on the media player on the lockscreen. Default is set to 30 seconds. + */ public var skipForwardSeconds: Double = 30 + + /** + Corresponding to the skipping backwards button on the media player on the lockscreen. Default is set to 15 seconds. + */ public var skipBackwardSeconds: Double = 15 + /** + List of AVAudioUnit audio modifiers to pass to the engine on initialization. + + - Important: To have the intended effects, the list of modifiers must be finalized before initializing the audio to be played. The modifers are added to the engine in order of the list. + + - Note: The default list given has an AVAudioUnitTimePitch already first in the list. This node is specifically set to change the rate of audio without changing the pitch of the audio (intended for changing the rate of spoken word). Please look at [forums.developer.apple.com/thread/5874](https://forums.developer.apple.com/thread/5874) and [forums.developer.apple.com/thread/6050](https://forums.developer.apple.com/thread/6050) to see the specific componentDescription used. + */ public var audioModifiers: [AVAudioUnit] = [] - public var duration: Double { + /** + Total duration of current audio initialized. Returns nil if no audio is initialized in player. + */ + public var duration: Double? { get { - return presenter.duration ?? 0.0 + return presenter.duration } } - public var prettyDuration: String { + /** + A textual representation of the duration of the current audio initialized. Returns nil if no audio is initialized in player. + */ + public var prettyDuration: String? { get { - return SAPlayer.prettifyTimestamp(duration) + guard let d = duration else { return nil } + return SAPlayer.prettifyTimestamp(d) } } - public var elapsedTime: Double { + /** + Elapsed playback time of the current audio initialized. Returns nil if no audio is initialized in player. + */ + public var elapsedTime: Double? { get { - return presenter.needle ?? 0 + return presenter.needle } } - public var prettyElapsedTime: String { + /** + A textual representation of the elapsed playback time of the current audio initialized. Returns nil if no audio is initialized in player. + */ + public var prettyElapsedTime: String? { get { - return SAPlayer.prettifyTimestamp(elapsedTime) + guard let e = elapsedTime else { return nil } + return SAPlayer.prettifyTimestamp(e) } } + /** + Corresponding to the media info to display on the lockscreen for the current audio. + + - Note: Setting this to nil clears the information displayed on the lockscreen media player. + */ public var mediaInfo: SALockScreenInfo? = nil { didSet { - if let info = mediaInfo { - presenter.handleLockscreenInfo(info: info) - } + presenter.handleLockscreenInfo(info: mediaInfo) } } @@ -86,6 +117,12 @@ public class SAPlayer { audioModifiers.append(AVAudioUnitTimePitch(audioComponentDescription: componentDescription)) } + /** + Formats a textual representation of a given timestamp for display in hh:MM:SS format, that is hours:minutes:seconds. + + - Parameter timestamp: The timestamp to format. + - Returns: A textual representation of the given timestamp + */ public static func prettifyTimestamp(_ timestamp: Double) -> String { let hours = Int(timestamp / 60 / 60) let minutes = Int((timestamp - Double(hours * 60)) / 60) @@ -192,7 +229,7 @@ extension SAPlayer: SAPlayerDelegate { func seekEngine(toNeedle needle: Needle) { var seekToNeedle = needle < 0 ? 0 : needle - seekToNeedle = needle > Needle(duration) ? Needle(duration) : needle + seekToNeedle = needle > Needle(duration ?? 0) ? Needle(duration ?? 0) : needle player?.seek(toNeedle: seekToNeedle) } } diff --git a/Source/SAPlayerPresenter.swift b/Source/SAPlayerPresenter.swift index 20ac910..d27cb6e 100644 --- a/Source/SAPlayerPresenter.swift +++ b/Source/SAPlayerPresenter.swift @@ -88,9 +88,7 @@ class SAPlayerPresenter { self.delegate?.updateLockscreenPlaybackDuration(duration: duration) self.duration = duration - if let info = self.mediaInfo { - self.delegate?.setLockScreenInfo(withMediaInfo: info, duration: duration) - } + self.delegate?.setLockScreenInfo(withMediaInfo: self.mediaInfo, duration: duration) }) needleRef = AudioClockDirector.shared.attachToChangesInNeedle(closure: { [weak self] (key, needle) in @@ -116,7 +114,7 @@ class SAPlayerPresenter { } @available(iOS 10.0, *) - func handleLockscreenInfo(info: SALockScreenInfo) { + func handleLockscreenInfo(info: SALockScreenInfo?) { self.mediaInfo = info } }