Skip to content

Open-source music player for YouTube Music's catalog, with genres, full albums and playlists. Windows, macOS, Linux, Android, iPhone and Docker. No account needed.

TypeScript

iOS: hold the seek position and keep more audio buffered

Vahid Etemovic3e118d32f05a
diff --git a/mobile/ios/App/App/MusikeAudio.swift b/mobile/ios/App/App/MusikeAudio.swift
index 54dab7c..6953c98 100644
--- a/mobile/ios/App/App/MusikeAudio.swift
+++ b/mobile/ios/App/App/MusikeAudio.swift
@@ -60,6 +60,8 @@ final class MusikeAudio: NSObject, WKScriptMessageHandler {
],
])
let newItem = AVPlayerItem(asset: asset)
+ // Keep a visible amount ahead of the playhead, so the buffered part of the progress bar means something.
+ newItem.preferredForwardBufferDuration = 30
let newPlayer = AVPlayer(playerItem: newItem)
newPlayer.automaticallyWaitsToMinimizeStalling = true
newPlayer.volume = player?.volume ?? 1
@@ -104,7 +106,16 @@ final class MusikeAudio: NSObject, WKScriptMessageHandler {
}
private func seek(_ to: Double) {
- player?.seek(to: CMTime(seconds: max(0, to), preferredTimescale: 600), toleranceBefore: .zero, toleranceAfter: .zero)
+ guard let player else { return }
+ player.seek(
+ to: CMTime(seconds: max(0, to), preferredTimescale: 600),
+ toleranceBefore: .zero,
+ toleranceAfter: .zero
+ ) { [weak self] finished in
+ guard finished else { return }
+ // AVPlayer reports the old position until the seek lands; the web side waits for this.
+ self?.emit("seeked", ["position": to])
+ }
}
private func bufferedEnd() -> Double {
diff --git a/web/src/lib/audio.ts b/web/src/lib/audio.ts
index f574c92..36adc28 100644
--- a/web/src/lib/audio.ts
+++ b/web/src/lib/audio.ts
@@ -43,6 +43,8 @@ class NativeAudio extends EventTarget {
private _paused = true;
private _ended = false;
private _bufferedEnd = 0;
+ /** Set while a seek is in flight: the native player keeps reporting the old position until it lands. */
+ private _seekTarget: number | null = null;
readyState = 0;
networkState = 0;
@@ -52,8 +54,13 @@ class NativeAudio extends EventTarget {
constructor(private bridge: Bridge) {
super();
- const host = globalThis as { __musikeAudioEvent?: (event: string, data: NativePayload) => void };
+ const host = globalThis as {
+ __musikeAudioEvent?: (event: string, data: NativePayload) => void;
+ __musikeAudioEl?: NativeAudio;
+ };
host.__musikeAudioEvent = (event, data) => this.fromNative(event, data ?? {});
+ // Handy when something needs to poke the player from the console.
+ host.__musikeAudioEl = this;
}
get src() {
@@ -74,6 +81,7 @@ class NativeAudio extends EventTarget {
set currentTime(value: number) {
this._position = value;
+ this._seekTarget = value;
this.bridge.postMessage({ cmd: "seek", to: value });
}
@@ -150,13 +158,24 @@ class NativeAudio extends EventTarget {
this.dispatchEvent(new Event("loadedmetadata"));
this.dispatchEvent(new Event("durationchange"));
break;
- case "time":
- this._position = data.position ?? this._position;
+ case "time": {
+ const position = data.position ?? this._position;
if (data.duration && Number.isFinite(data.duration)) this._duration = data.duration;
this._bufferedEnd = data.buffered ?? this._bufferedEnd;
+ // Ignore positions from before a pending seek; otherwise the player snaps back to where it was.
+ if (this._seekTarget !== null && Math.abs(position - this._seekTarget) > 1.5) break;
+ this._seekTarget = null;
+ this._position = position;
this.dispatchEvent(new Event("timeupdate"));
this.dispatchEvent(new Event("progress"));
break;
+ }
+ case "seeked":
+ this._seekTarget = null;
+ this._position = data.position ?? this._position;
+ this.dispatchEvent(new Event("seeked"));
+ this.dispatchEvent(new Event("timeupdate"));
+ break;
case "playing":
this._paused = false;
this.readyState = 4;