跳转到正文

播放列表队列

GitHub

使用 8.5 队列 API 构建播客播放器、播放列表和引导式会话。所有这些 API 都是 增量式的: 现有 preload, play, pause,以及 complete 流程保持不变。

示例代码也位于插件仓库中: capacitor-native-audio (docs/PLAYLIST.md)。这些 API 随插件 8.5 版本发布,发布于 capacitor-native-audio PR #353.

configure() 选项因平台而异。在 Web 上,仅支持 progressInterval 。传入 showNotification 或 backgroundPlayback 导致 configure() 被拒绝。

import { NativeAudio } from '@capgo/capacitor-native-audio';
// Web: only progressInterval is supported in configure().
await NativeAudio.configure({ progressInterval: 0.5 });
// iOS and Android: notification and background options.
await NativeAudio.configure({
showNotification: true,
backgroundPlayback: true,
progressInterval: 0.5,
});

backgroundPlayback: true 当应用在 Android 仅适用于 iOS configure() 不会读取此选项,因此请勿依赖它。在 Android 上,它会 不会 启动前台服务。在依赖后台播放之前,您仍需清单权限(FOREGROUND_SERVICE, FOREGROUND_SERVICE_MEDIA_PLAYBACK, WAKE_LOCK)以及您自己的媒体前台服务。请参阅 入门指南 了解混音权衡和通知设置。

在 iOS 上, showNotification: true 切换音频会话,使“正在播放”出现在控制中心和锁屏上。

锁屏和通知中的跳过下一首和上一首功能在以下位置处理: iOS 和 Android 何时 showNotification 已启用。

await NativeAudio.addListener('queueChange', ({ assetId, index, reason }) => {
console.log('queue', assetId, index, reason);
});
await NativeAudio.addListener('progress', ({ assetId, position, duration }) => {
console.log(assetId, position, duration);
});
await NativeAudio.addListener('playbackState', (state) => {
console.log(state.assetId, state.state, state.reason, state.isPlaying, state.currentTime, state.duration);
});
事件平台备注
queueChangeiOS、Android、Web当活动队列项发生变化时触发,包括 setQueue 配合 autoPlay: true 在 Web 和原生平台(原因 setQueue),以及 next, previous, select,以及 remoteNext.
progressiOS、Android、Web来自 configure({ progressInterval })的间隔。默认 0.5 秒。字段: assetId, position, duration.
playbackStateiOS、Android、Web字段: assetId, state (playing, paused, stopped), reason, isPlaying,可选 currentTime 以及 duration。包含来自锁屏和原生通知的传输控制。当插件的传输方法改变播放状态时,在 Web 端触发,包括播放、暂停、恢复、循环和完成。
audioRouteChangeiOS、Android、Web当成功 setSpeaker 或 setEarpiece 调用触发事件时,即使路由未改变也会触发。耳机或蓝牙自行连接时不会触发。在 Web 端, setSpeaker 触发 route: 'speaker' 以及 setEarpiece 触发 route: 'earpiece'.

currentTime 播放期间仍大约每 100ms 触发一次。

setQueue 接受与 preload 每个项目(assetId, assetPath, isUrl, volume, audioChannelNum, notificationMetadata, headers)相同的字段。具有匹配 ID 的预加载资源会被复用。选项还包括 startIndex (默认 0)和 autoPlay (默认 false)。它返回一个 QueueState 配合 assetId, index, count,以及 assetIds.

await NativeAudio.setQueue({
items: [
{
assetId: 'ep-1',
assetPath: 'https://example.com/ep1.mp3',
isUrl: true,
notificationMetadata: {
title: 'Episode 1',
artist: 'Show',
artworkUrl: 'https://example.com/cover.png',
},
},
{
assetId: 'ep-2',
assetPath: 'https://example.com/ep2.mp3',
isUrl: true,
notificationMetadata: {
title: 'Episode 2',
artist: 'Show',
artworkUrl: 'https://example.com/cover.png',
},
},
],
startIndex: 0,
autoPlay: true,
});

queueNext, queuePrevious,以及 queueSelect 停止当前播放项并开始播放所选项目。每个方法都返回更新后的 QueueState.

const state = await NativeAudio.getQueueState();
console.log(state.assetId, state.index, state.count, state.assetIds);
await NativeAudio.queueNext();
await NativeAudio.queuePrevious();
await NativeAudio.queueSelect({ assetId: 'ep-2' });
await NativeAudio.clearQueue();

自动推进由应用驱动,与预加载和播放模型相同。在推进前,请比较事件 assetId 与选中的队列资源。 queueNext() 已开始播放选中项,因此请勿在其后调用 resume() 。在 Web 上,选中索引会在停止当前项之前更新,因此此保护机制在 iOS 和 Android 上表现一致。

await NativeAudio.addListener('complete', async ({ assetId }) => {
const { assetId: selectedId, index, count } = await NativeAudio.getQueueState();
if (assetId !== selectedId) {
return;
}
if (index + 1 < count) {
await NativeAudio.queueNext();
}
});

getPlaybackState() 返回一个 PlaybackStateEvent: assetId, state, reason, isPlaying,以及可选的 currentTime 以及 duration。如果该资源当前正在播放, state 为 playing 从实时元素获取,即使跟踪的快照已过期。否则 state 使用跟踪的快照,它匹配该资源最后一次 playbackState 事件。当没有资源处于活动状态时, assetId 为空。请确保 getRate() 仅在传入真实的 assetId时调用。在 Web 上, play 会重新创建音频元素并重置速率,因此请调用 setRate 之后 play.

const snapshot = await NativeAudio.getPlaybackState();
console.log(snapshot.assetId, snapshot.state, snapshot.reason, snapshot.isPlaying, snapshot.currentTime, snapshot.duration);
if (snapshot.assetId) {
const { rate } = await NativeAudio.getRate({ assetId: snapshot.assetId });
console.log(rate);
}
await NativeAudio.setSpeaker();
await NativeAudio.setEarpiece();
const { route } = await NativeAudio.getAudioRoute();
console.log(route);
await NativeAudio.addListener('audioRouteChange', ({ route }) => {
console.log('route', route);
});

route 为 speaker, earpiece,或 external。成功 setSpeaker 以及 setEarpiece 调用会触发 audioRouteChange,即使路由未发生变化。当耳机或蓝牙设备自行连接时,该事件不会触发。在 Android 上, getAudioRoute 报告 earpiece 仅在通话模式下,且在 setEarpiece之后;否则它报告 speaker 当未连接耳机或蓝牙时。在 Web 上, setSpeaker 以及 setEarpiece 并非空操作: setSpeaker 触发 audioRouteChange 配合 route: 'speaker', setEarpiece 触发 route: 'earpiece',以及 getAudioRoute 返回最后请求的路由(speaker 之后 setSpeaker, earpiece 之后 setEarpiece)。Web 平台无法更改实际输出。