Skip to content

Commit 3ff0824

Browse files
committed
Incorporate experimental streaming duration fixes
Changes from PR tanhakabir#155 tanhakabir/SwiftAudioPlayer@master...HKdAlex:SwiftAudioPlayer:master Also update lines 123-128 of AudioStreamEngine to avoid duration being overridden by predictedStreamDuration once stream has completed
1 parent 0ae8e2d commit 3ff0824

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Source/Engine/AudioStreamEngine.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ class AudioStreamEngine: AudioEngine {
121121
let s = predictedStreamDurationDebounceHelper
122122
if d/DEBOUNCING_BUFFER_TIME != s/DEBOUNCING_BUFFER_TIME {
123123
predictedStreamDurationDebounceHelper = predictedStreamDuration
124-
duration = predictedStreamDuration
124+
if AudioDataManager.shared.currentStreamFinished {
125+
duration = AudioDataManager.shared.currentStreamFinishedWithDuration
126+
} else {
127+
duration = predictedStreamDuration
128+
}
125129
}
126130
}
127131
}
@@ -245,7 +249,12 @@ class AudioStreamEngine: AudioEngine {
245249
let range = converter.pollNetworkAudioAvailabilityRange()
246250
isPlayable = (numberOfBuffersScheduledInTotal >= MIN_BUFFERS_TO_BE_PLAYABLE && range.1 > 0) && predictedStreamDuration > 0
247251
Log.debug("loaded \(range), numberOfBuffersScheduledInTotal: \(numberOfBuffersScheduledInTotal), isPlayable: \(isPlayable)")
248-
bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, predictedDurationToLoad: predictedStreamDuration, isPlayable: isPlayable)
252+
if AudioDataManager.shared.currentStreamFinished {
253+
AudioDataManager.shared.updateDuration(d: range.1);
254+
bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, predictedDurationToLoad: range.1, isPlayable: isPlayable)
255+
} else {
256+
bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, predictedDurationToLoad: predictedStreamDuration, isPlayable: isPlayable)
257+
}
249258
}
250259

251260
private func updateNeedle() {
@@ -268,8 +277,13 @@ class AudioStreamEngine: AudioEngine {
268277

269278
private func updateDuration() {
270279
if let d = converter.pollPredictedDuration() {
271-
self.predictedStreamDuration = d
272-
}
280+
self.predictedStreamDuration = d
281+
if AudioDataManager.shared.currentStreamFinished {
282+
self.predictedStreamDuration = AudioDataManager.shared.currentStreamFinishedWithDuration
283+
} else {
284+
self.predictedStreamDuration = d
285+
}
286+
}
273287
}
274288

275289

Source/Model/AudioDataManager.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import Foundation
2727

2828
protocol AudioDataManagable {
29+
var currentStreamFinished: Bool { get }
30+
var currentStreamFinishedWithDuration: Duration { get }
2931
var numberOfQueued: Int { get }
3032
var numberOfActive: Int { get }
3133

@@ -38,6 +40,7 @@ protocol AudioDataManagable {
3840
func setDownloadDirectory(_ dir: FileManager.SearchPathDirectory)
3941

4042
func clear()
43+
func updateDuration(d: Duration)
4144

4245
//Director pattern
4346
func attach(callback: @escaping (_ id: ID, _ progress: Double)->())
@@ -55,8 +58,13 @@ protocol AudioDataManagable {
5558
}
5659

5760
class AudioDataManager: AudioDataManagable {
61+
var currentStreamFinishedWithDuration: Duration = 0
62+
5863
var allowCellular: Bool = true
5964
var downloadDirectory: FileManager.SearchPathDirectory = .documentDirectory
65+
66+
public var currentStreamFinished = false
67+
public var totalStreamedDuration = 0
6068

6169
static let shared: AudioDataManagable = AudioDataManager()
6270

@@ -95,6 +103,10 @@ class AudioDataManager: AudioDataManagable {
95103
progressCallback: streamProgressListener,
96104
doneCallback: streamDoneListener)
97105
}
106+
107+
func updateDuration(d: Duration) {
108+
currentStreamFinishedWithDuration = d
109+
}
98110

99111
func clear() {
100112
streamingCallbacks = []
@@ -125,6 +137,7 @@ class AudioDataManager: AudioDataManagable {
125137
// MARK:- Streaming
126138
extension AudioDataManager {
127139
func startStream(withRemoteURL url: AudioURL, callback: @escaping (StreamProgressPTO) -> ()) {
140+
currentStreamFinished = false
128141
if let data = FileStorage.Audio.read(url.key) {
129142
let dto = StreamProgressDTO.init(progress: 1.0, data: data, totalBytesExpected: Int64(data.count))
130143
callback(StreamProgressPTO(dto: dto))
@@ -154,10 +167,12 @@ extension AudioDataManager {
154167
streamWorker.resume(withId: url.key)
155168
}
156169
func seekStream(withRemoteURL url: AudioURL, toByteOffset offset: UInt64) {
170+
currentStreamFinished = false
157171
streamWorker.seek(withId: url.key, withByteOffset: offset)
158172
}
159173

160174
func deleteStream(withRemoteURL url: AudioURL) {
175+
currentStreamFinished = false
161176
streamWorker.stop(withId: url.key)
162177
streamingCallbacks.removeAll { (cb: (ID, (StreamProgressPTO) -> ())) -> Bool in
163178
return cb.0 == url.key
@@ -230,7 +245,7 @@ extension AudioDataManager {
230245
if error != nil {
231246
return false
232247
}
233-
248+
currentStreamFinished = true
234249
downloadWorker.resumeAllActive()
235250
return false
236251
}

0 commit comments

Comments
 (0)