Skip to content

Commit 65ac214

Browse files
committed
fix(audio): keep voice messages playing in the background on mobile
Signed-off-by: Erwan Leboucher <erwanleboucher@gmail.com>
1 parent 0f64589 commit 65ac214

9 files changed

Lines changed: 293 additions & 82 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Voice messages and audio files keep playing on Android and iOS when Sable goes to the background or the screen turns off, with play and pause on the lock screen and in the notification shade.

‎Cargo.lock‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src-tauri/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ tauri-plugin-edge-to-edge = { version = "0.3.3", git = "https://github.com/Sable
9999
# `navigator.share` reaches no webview we ship, so the system share sheet only
100100
# comes from the native side.
101101
tauri-plugin-sharekit = "=0.4.0-rc.8"
102+
# The webviews mute `<audio>` in the background, the system player keeps going.
103+
tauri-plugin-native-audio = { version = "1.0.5", git = "https://github.com/SableClient/tauri-plugin-native-audio.git", rev = "1bbab74ed550b8395fdd287a25542c2b99624597" }
102104

103105
[target."cfg(target_os = \"ios\")".dependencies]
104106
# backtrace 0.3.76 needs dyld bindings that libc 0.2.190 removed on iOS.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "../gen/schemas/mobile-schema.json",
3+
"identifier": "native-audio-capability",
4+
"description": "plays voice messages and audio files through the system player",
5+
"platforms": ["android", "iOS"],
6+
"windows": ["main"],
7+
"permissions": [
8+
"native-audio:default",
9+
{
10+
"identifier": "fs:allow-mkdir",
11+
"allow": [{ "path": "$APPCACHE/native-audio" }]
12+
},
13+
{
14+
"identifier": "fs:allow-write-file",
15+
"allow": [{ "path": "$APPCACHE/native-audio/*" }]
16+
},
17+
{
18+
"identifier": "fs:allow-remove",
19+
"allow": [{ "path": "$APPCACHE/native-audio/*" }]
20+
}
21+
]
22+
}

‎src-tauri/src/lib.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,9 @@ fn with_platform_plugins(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<
771771
#[cfg(feature = "geolocation")]
772772
let builder = builder.plugin(tauri_plugin_geolocation::init());
773773

774-
let builder = builder.plugin(tauri_plugin_sharekit::init());
774+
let builder = builder
775+
.plugin(tauri_plugin_sharekit::init())
776+
.plugin(tauri_plugin_native_audio::init());
775777

776778
#[cfg(target_os = "android")]
777779
let builder = builder.plugin(tauri_plugin_android_fs::init());

‎src/lib/platform/files.ts‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,12 @@ export async function shareFile(
194194
await electron.shareFile({ name, bytes });
195195
return 'saved';
196196
}
197-
const [{ appCacheDir, join }, { mkdir, remove, writeFile }, { shareFile: shareNative }] =
198-
await Promise.all([
199-
import('@tauri-apps/api/path'),
200-
import('@tauri-apps/plugin-fs'),
201-
import('@choochmeque/tauri-plugin-sharekit-api'),
202-
]);
203-
204-
const directory = await join(await appCacheDir(), 'outgoing-share');
205-
await mkdir(directory, { recursive: true });
206-
const path = await join(directory, cacheName(name));
207-
await writeFile(path, bytes);
197+
const [{ remove }, { shareFile: shareNative }] = await Promise.all([
198+
import('@tauri-apps/plugin-fs'),
199+
import('@choochmeque/tauri-plugin-sharekit-api'),
200+
]);
201+
202+
const path = await writeCacheFile('outgoing-share', name, bytes);
208203

209204
try {
210205
await shareNative(path, {
@@ -224,6 +219,23 @@ export async function shareFile(
224219
}
225220
}
226221

222+
/** Native code cannot read a blob URL, so the bytes go to a file in the app cache first. */
223+
export async function writeCacheFile(
224+
folder: string,
225+
name: string,
226+
bytes: Uint8Array
227+
): Promise<string> {
228+
const [{ appCacheDir, join }, { mkdir, writeFile }] = await Promise.all([
229+
import('@tauri-apps/api/path'),
230+
import('@tauri-apps/plugin-fs'),
231+
]);
232+
const directory = await join(await appCacheDir(), folder);
233+
await mkdir(directory, { recursive: true });
234+
const path = await join(directory, cacheName(name));
235+
await writeFile(path, bytes);
236+
return path;
237+
}
238+
227239
function cacheName(name: string): string {
228240
const safe = name.replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^[.-]+/, '');
229241
return safe || 'attachment';
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { addPluginListener, invoke } from '@tauri-apps/api/core';
2+
3+
import { writeCacheFile } from './files.js';
4+
import { isNativeMobile } from './os.js';
5+
6+
const FOLDER = 'native-audio';
7+
8+
interface PluginState {
9+
status: 'idle' | 'loading' | 'playing' | 'ended' | 'error';
10+
currentTime: number;
11+
duration: number;
12+
isPlaying: boolean;
13+
error?: string;
14+
}
15+
16+
export interface NativeTrack {
17+
/** The blob URL the timeline already holds; it also tells tracks apart. */
18+
url: string;
19+
name: string;
20+
}
21+
22+
export interface NativeTrackState {
23+
playing: boolean;
24+
ended: boolean;
25+
currentTime: number;
26+
duration: number | null;
27+
}
28+
29+
let activeUrl = $state<string | null>(null);
30+
let plugin = $state<PluginState | null>(null);
31+
let ready: Promise<void> | null = null;
32+
let activePath: string | null = null;
33+
let loads = 0;
34+
let queue: Promise<unknown> = Promise.resolve();
35+
36+
/**
37+
* The webviews mute or freeze `<audio>` once the app leaves the screen, so the phone apps play
38+
* through the system player, which also brings lock screen and notification controls.
39+
*/
40+
export function playsNatively(): boolean {
41+
return isNativeMobile();
42+
}
43+
44+
function setUp(): Promise<void> {
45+
ready ??= (async () => {
46+
await addPluginListener<PluginState>('native-audio', 'native_audio_state', (next) => {
47+
plugin = next;
48+
});
49+
plugin = await invoke<PluginState>('plugin:native-audio|initialize');
50+
})().catch((error: unknown) => {
51+
ready = null;
52+
throw error;
53+
});
54+
return ready;
55+
}
56+
57+
function serial<T>(task: () => Promise<T>): Promise<T> {
58+
const run = queue.then(task, task);
59+
queue = run.catch(() => undefined);
60+
return run;
61+
}
62+
63+
async function load(track: NativeTrack): Promise<void> {
64+
await setUp();
65+
if (activeUrl === track.url) return;
66+
const bytes = new Uint8Array(await (await fetch(track.url)).arrayBuffer());
67+
loads += 1;
68+
const path = await writeCacheFile(FOLDER, `${String(loads)}-${track.name}`, bytes);
69+
plugin = await invoke<PluginState>('plugin:native-audio|set_source', {
70+
src: path,
71+
title: track.name,
72+
});
73+
const previous = activePath;
74+
activeUrl = track.url;
75+
activePath = path;
76+
if (previous !== null) {
77+
const { remove } = await import('@tauri-apps/plugin-fs');
78+
await remove(previous).catch((error: unknown) => {
79+
console.debug('[sable audio] the previous track was not removed', previous, error);
80+
});
81+
}
82+
}
83+
84+
export const nativeAudio = {
85+
/** Null while another track, or none, holds the player. */
86+
of(url: string): NativeTrackState | null {
87+
if (activeUrl !== url || plugin === null) return null;
88+
return {
89+
playing: plugin.isPlaying,
90+
ended: plugin.status === 'ended',
91+
currentTime: plugin.currentTime,
92+
duration: plugin.duration > 0 ? plugin.duration : null,
93+
};
94+
},
95+
toggle(track: NativeTrack, rate = 1): Promise<void> {
96+
return serial(async () => {
97+
if (activeUrl === track.url && plugin?.isPlaying) {
98+
plugin = await invoke<PluginState>('plugin:native-audio|pause');
99+
return;
100+
}
101+
await load(track);
102+
await invoke('plugin:native-audio|set_rate', { rate });
103+
plugin = await invoke<PluginState>('plugin:native-audio|play');
104+
});
105+
},
106+
seek(track: NativeTrack, position: number): Promise<void> {
107+
return serial(async () => {
108+
await load(track);
109+
plugin = await invoke<PluginState>('plugin:native-audio|seek_to', { position });
110+
});
111+
},
112+
setRate(url: string, rate: number): Promise<void> {
113+
return serial(async () => {
114+
if (activeUrl !== url) return;
115+
plugin = await invoke<PluginState>('plugin:native-audio|set_rate', { rate });
116+
});
117+
},
118+
};

‎src/lib/ui/AudioPlayer.svelte‎

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import SpeakerXIcon from 'phosphor-svelte/lib/SpeakerXIcon';
1919
import { onDestroy, onMount } from 'svelte';
2020
21+
import { nativeAudio, playsNatively } from '#lib/platform/native-audio.svelte.js';
2122
import IconButton from '#lib/ui/primitives/IconButton.svelte';
2223
import ResponsivePopover from '#lib/ui/primitives/ResponsivePopover.svelte';
2324
import Slider from '#lib/ui/primitives/Slider.svelte';
@@ -43,16 +44,24 @@
4344
};
4445
4546
let player: HTMLAudioElement | null = $state(null);
46-
let duration: number | null = $state(null);
47-
let isPlaying = $state(false);
47+
let webDuration: number | null = $state(null);
48+
let webPlaying = $state(false);
4849
let seekPosition = $state(0);
4950
let seekUserIsInteracting = $state(false);
5051
let animationId: number | null;
5152
5253
let { src, label, filename, attachedTop = false }: Props = $props();
5354
55+
const native = playsNatively();
56+
let track = $derived({ url: src, name: filename });
57+
let nativeState = $derived(native ? nativeAudio.of(src) : null);
58+
let isPlaying = $derived(native ? (nativeState?.playing ?? false) : webPlaying);
59+
let duration = $derived(native ? (nativeState?.duration ?? null) : webDuration);
60+
5461
const updateTime = () => {
55-
if (!seekUserIsInteracting && player) {
62+
if (!seekUserIsInteracting && native) {
63+
seekPosition = nativeState && !nativeState.ended ? nativeState.currentTime : 0;
64+
} else if (!seekUserIsInteracting && player) {
5665
seekPosition = player?.currentTime || 0;
5766
}
5867
animationId = requestAnimationFrame(updateTime);
@@ -77,21 +86,23 @@
7786
class="audio-container {attachedTop ? 'audio-container-attached' : 'audio-container-unattached'}"
7887
aria-label={label}
7988
>
80-
<audio
81-
bind:this={player}
82-
{src}
83-
preload="metadata"
84-
aria-hidden="true"
85-
onloadedmetadata={() => {
86-
if (player?.duration) {
87-
duration = player.duration;
88-
}
89-
}}
90-
onplay={() => (isPlaying = true)}
91-
onpause={() => (isPlaying = false)}
92-
onended={() => (isPlaying = false)}
93-
>
94-
</audio>
89+
{#if !native}
90+
<audio
91+
bind:this={player}
92+
{src}
93+
preload="metadata"
94+
aria-hidden="true"
95+
onloadedmetadata={() => {
96+
if (player?.duration) {
97+
webDuration = player.duration;
98+
}
99+
}}
100+
onplay={() => (webPlaying = true)}
101+
onpause={() => (webPlaying = false)}
102+
onended={() => (webPlaying = false)}
103+
>
104+
</audio>
105+
{/if}
95106
{#if !attachedTop}
96107
<div class="audio-header">
97108
<FileAudioIcon size={24} />
@@ -105,7 +116,9 @@
105116
type="button"
106117
aria-label={i18n.t(isPlaying ? 'timeline.pauseAudio' : 'timeline.playAudio', { name: label })}
107118
onclick={() => {
108-
if (!isPlaying) {
119+
if (native) {
120+
void nativeAudio.toggle(track);
121+
} else if (!isPlaying) {
109122
try {
110123
player?.play();
111124
} catch (err) {
@@ -138,13 +151,16 @@
138151
onpointerup={() => (seekUserIsInteracting = false)}
139152
onpointercancel={() => (seekUserIsInteracting = false)}
140153
oninput={(e) => {
141-
if (player) {
154+
if (native) {
155+
seekPosition = +e.currentTarget.value;
156+
void nativeAudio.seek(track, seekPosition);
157+
} else if (player) {
142158
player.currentTime = +e.currentTarget.value;
143159
seekPosition = +e.currentTarget.value;
144160
}
145161
}}
146162
/>
147-
{#if volumeAdjustable}
163+
{#if volumeAdjustable && !native}
148164
<ResponsivePopover
149165
side="top"
150166
align="center"

0 commit comments

Comments
 (0)