feat #14740: play back calls
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
<script lang="ts">
|
||||
import { convertFileSrc } from '@tauri-apps/api/core'
|
||||
import { onDestroy } from 'svelte'
|
||||
import { errorText, preparePlayback, type CallId } from './api'
|
||||
import { formatOffset } from './format'
|
||||
|
||||
let {
|
||||
callId,
|
||||
estimate,
|
||||
ontime,
|
||||
onplaying,
|
||||
}: {
|
||||
callId: CallId
|
||||
/** The call's length from its times, shown until the mix is ready. */
|
||||
estimate: number
|
||||
/** The playback position, once the mix is loaded (also while paused). */
|
||||
ontime?: (secs: number) => void
|
||||
onplaying?: (playing: boolean) => void
|
||||
} = $props()
|
||||
|
||||
let audio: HTMLAudioElement
|
||||
/** Resolves once the mix is loaded; `false` if preparing it failed. */
|
||||
let loading: Promise<boolean> | null = null
|
||||
let preparing = $state(false)
|
||||
let playing = $state(false)
|
||||
// svelte-ignore state_referenced_locally
|
||||
let duration = $state(estimate)
|
||||
let position = $state(0)
|
||||
/** While the slider is dragged it shows the drag, not the audio. */
|
||||
let dragging = $state(false)
|
||||
let error = $state('')
|
||||
/** The mix held in memory where the webview cannot stream it (Linux). */
|
||||
let blobUrl: string | null = null
|
||||
|
||||
/** Mixes the call (first use only) and loads the mix into the player. */
|
||||
function load(): Promise<boolean> {
|
||||
loading ??= (async () => {
|
||||
preparing = true
|
||||
error = ''
|
||||
try {
|
||||
const info = await preparePlayback(callId)
|
||||
duration = info.duration_secs
|
||||
const url = convertFileSrc(info.path)
|
||||
try {
|
||||
await loadSource(url)
|
||||
} catch (err) {
|
||||
if (!(err instanceof MediaError) || err.code !== MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED) {
|
||||
throw err
|
||||
}
|
||||
// WebKitGTK (Linux) plays no media from a custom scheme such as
|
||||
// asset:// (WebView2 gets http://asset.localhost instead), but
|
||||
// fetches it fine: there the mix is played from memory.
|
||||
const response = await fetch(url)
|
||||
if (!response.ok) throw new Error(`Audio konnte nicht geladen werden (${response.status}).`)
|
||||
blobUrl = URL.createObjectURL(await response.blob())
|
||||
await loadSource(blobUrl)
|
||||
}
|
||||
return true
|
||||
} catch (err) {
|
||||
error = err instanceof MediaError ? 'Audio konnte nicht geladen werden.' : errorText(err)
|
||||
loading = null // a later click tries again
|
||||
return false
|
||||
} finally {
|
||||
preparing = false
|
||||
}
|
||||
})()
|
||||
return loading
|
||||
}
|
||||
|
||||
/** Loads `src` into the player; rejects with its `MediaError`. */
|
||||
function loadSource(src: string): Promise<void> {
|
||||
const ready = new Promise<void>((resolve, reject) => {
|
||||
const ok = () => {
|
||||
audio.removeEventListener('error', fail)
|
||||
resolve()
|
||||
}
|
||||
const fail = () => {
|
||||
audio.removeEventListener('loadedmetadata', ok)
|
||||
reject(audio.error)
|
||||
}
|
||||
audio.addEventListener('loadedmetadata', ok, { once: true })
|
||||
audio.addEventListener('error', fail, { once: true })
|
||||
})
|
||||
audio.src = src
|
||||
return ready
|
||||
}
|
||||
|
||||
async function start() {
|
||||
try {
|
||||
await audio.play()
|
||||
} catch (err) {
|
||||
error = `Wiedergabe nicht möglich: ${errorText(err)}`
|
||||
}
|
||||
}
|
||||
|
||||
async function toggle() {
|
||||
if (playing) {
|
||||
audio.pause()
|
||||
} else if (await load()) {
|
||||
await start()
|
||||
}
|
||||
}
|
||||
|
||||
/** Jumps to `secs` and plays from there (a click on a transcript line). */
|
||||
export async function playFrom(secs: number) {
|
||||
if (!(await load())) return
|
||||
audio.currentTime = Math.min(secs, duration)
|
||||
position = audio.currentTime
|
||||
if (audio.paused) await start()
|
||||
}
|
||||
|
||||
async function seekTo(secs: number) {
|
||||
dragging = false
|
||||
position = secs
|
||||
if (!(await load())) return
|
||||
audio.currentTime = secs
|
||||
}
|
||||
|
||||
function ontimeupdate() {
|
||||
if (!dragging) position = audio.currentTime
|
||||
ontime?.(audio.currentTime)
|
||||
}
|
||||
|
||||
function setPlaying(value: boolean) {
|
||||
playing = value
|
||||
onplaying?.(value)
|
||||
}
|
||||
|
||||
onDestroy(() => {
|
||||
// Lets go of the file, so the call can be deleted right away.
|
||||
audio?.pause()
|
||||
audio?.removeAttribute('src')
|
||||
audio?.load()
|
||||
if (blobUrl) URL.revokeObjectURL(blobUrl)
|
||||
onplaying?.(false)
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="player">
|
||||
<audio
|
||||
bind:this={audio}
|
||||
preload="metadata"
|
||||
{ontimeupdate}
|
||||
onplay={() => setPlaying(true)}
|
||||
onpause={() => setPlaying(false)}
|
||||
onended={() => setPlaying(false)}
|
||||
></audio>
|
||||
<button
|
||||
class="play"
|
||||
disabled={preparing}
|
||||
title={playing ? 'Pause' : 'Abspielen'}
|
||||
aria-label={playing ? 'Pause' : 'Abspielen'}
|
||||
onclick={toggle}
|
||||
>
|
||||
{#if preparing}…{:else if playing}❚❚{:else}▶{/if}
|
||||
</button>
|
||||
<input
|
||||
class="timeline"
|
||||
type="range"
|
||||
min="0"
|
||||
max={duration || 1}
|
||||
step="0.1"
|
||||
value={position}
|
||||
disabled={preparing}
|
||||
aria-label="Position"
|
||||
oninput={(e) => {
|
||||
dragging = true
|
||||
position = Number(e.currentTarget.value)
|
||||
}}
|
||||
onchange={(e) => seekTo(Number(e.currentTarget.value))}
|
||||
/>
|
||||
<span class="time">{formatOffset(position)} / {formatOffset(duration)}</span>
|
||||
{#if preparing}
|
||||
<span class="muted">Audio wird vorbereitet…</span>
|
||||
{:else if error}
|
||||
<span class="error">{error}</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.player {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px 10px;
|
||||
min-width: 0;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.play {
|
||||
flex-shrink: 0;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
border-radius: 50%;
|
||||
border: none;
|
||||
background: var(--primary);
|
||||
color: #fff;
|
||||
font-size: 11px;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.play:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.timeline {
|
||||
flex: 1 1 160px;
|
||||
min-width: 120px;
|
||||
accent-color: var(--primary);
|
||||
}
|
||||
|
||||
.time {
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
font-variant-numeric: tabular-nums;
|
||||
white-space: nowrap;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user