Skip to content

播放状态

提示

你可以通过函数控制音频播放器的当前播放状态.

playAudio

播放给定的音频; 如果该音频没在播放列表中, 则会加入到播放列表.

ts
function playAudio(type: 'bgm' | 'ambient', audio: AudioWithOptionalTitle): void;
ts
type AudioWithOptionalTitle = {
  /** 标题 */
  title?: string;
  /** 音频的网络链接 */
  url: string;
};

参数

type

  • 类型: 'bgm' | 'ambient'
  • 描述: 背景音乐 ('bgm') 或音效 ('ambient')

audio

  • 类型: AudioWithOptionalTitle
  • 描述: 要播放的音频; 如果音频没有设置标题 (title), 则会从链接 (url) 提取文件名作为标题

示例

ts
playAudio('bgm', { url: 'http://commondatastorage.googleapis.com/codeskulptor-demos/DDR_assets/Kangaroo_MusiQue_-_The_Neverwritten_Role_Playing_Game.mp3' });
ts
playAudio('bgm', { title: 'Kangaroo Music', url: 'http://commondatastorage.googleapis.com/codeskulptor-demos/DDR_assets/Kangaroo_MusiQue_-_The_Neverwritten_Role_Playing_Game.mp3' });

pauseAudio

暂停音乐

ts
function pauseAudio(type: 'bgm' | 'ambient'): void;

参数

type

  • 类型: 'bgm' | 'ambient'
  • 描述: 背景音乐 ('bgm') 或音效 ('ambient')

getCurrentAudio

获取当前播放的音频信息: 链接、标题、是否在播、进度.

ts
function getCurrentAudio(type: 'bgm' | 'ambient'): CurrentAudio;
ts
type CurrentAudio = {
  /** 当前选中/正在播放的音频链接, 未选中曲目时为空字符串 */
  src: string;
  /** 当前选中音频的标题, 未匹配到时为空字符串 */
  title: string;
  /** 是否正在播放 */
  playing: boolean;
  /** 播放进度 (0-100) */
  progress: number;
};

参数

type

  • 类型: 'bgm' | 'ambient'
  • 描述: 背景音乐 ('bgm') 或音效 ('ambient')

返回值

示例

ts
// 获取当前正在播放的背景音乐信息
const { src, title, playing, progress } = getCurrentAudio('bgm');

作者:KAKAA, 青空莉想做舞台少女的狗