Skip to content

Commit

Permalink
Do not emit paused state when changing tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Oct 25, 2021
1 parent da3af0e commit 300b34a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion SwiftAudioEx/Classes/AVPlayerWrapper/AVPlayerWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol {
*/
fileprivate var _playWhenReady: Bool = true
fileprivate var _initialTime: TimeInterval?

/// True when the track was paused for the purpose of switching tracks
fileprivate var _pausedForLoad: Bool = false

fileprivate var _state: AVPlayerWrapperState = AVPlayerWrapperState.idle {
didSet {
Expand Down Expand Up @@ -100,7 +103,7 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol {
return seconds
}
else if let seconds = currentItem?.loadedTimeRanges.first?.timeRangeValue.duration.seconds,
!seconds.isNaN {
!seconds.isNaN {
return seconds
}
return 0.0
Expand Down Expand Up @@ -236,7 +239,10 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol {

func load(from url: URL, playWhenReady: Bool, initialTime: TimeInterval? = nil, options: [String : Any]? = nil) {
_initialTime = initialTime

_pausedForLoad = true
self.pause()

self.load(from: url, playWhenReady: playWhenReady, options: options)
}

Expand Down Expand Up @@ -277,6 +283,7 @@ extension AVPlayerWrapper: AVPlayerObserverDelegate {
if currentItem == nil {
_state = .idle
}
else if _pausedForLoad == true {}
else {
self._state = .paused
}
Expand All @@ -293,6 +300,7 @@ extension AVPlayerWrapper: AVPlayerObserverDelegate {
switch status {
case .readyToPlay:
self._state = .ready
self._pausedForLoad = false
if _playWhenReady && (_initialTime ?? 0) == 0 {
self.play()
}
Expand Down

0 comments on commit 300b34a

Please sign in to comment.