Skip to content
On this page

Class: Audio

音频播放
目前已支持mp3格式,理论上也支持其它html支持的格式
声音播放一般是统一控制,包括背景,音效,声音大小,应该播放的声音(可能会多个声音同时播放)
浏览器中用户首次打开页面可能会出现声音无法正常播放,这是浏览器出于用户体验考虑在没有任何交互的情况下禁止了。
此组件提供音频播放能力

ts
// 创建节点
const node = new Transform();
// 添加音频组件
const audio = node.addComponent(Audio);
// 设置播放音量
audio.volume = 0.5;
// 设置播放速度为2倍
audio.playbackRate = 2.0;
// 加载素材
Resource.load('horse.mp3').then((res: IRes) => {
     // 设置音频素材
     audio.source = res.data;
     // 播放音频
     audio.play();
});

Hierarchy

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new Audio()

组件不能单独实例化,必须通过Transform.addComponent创建

ts
const node = new Transform();
const componet = node.addComponent(Component);

Inherited from

Component.constructor

Defined in

src/core/component/Component.ts:37

Properties

emitter

emitter: EventEmitter<string | symbol, any>

事件处理器

Inherited from

Component.emitter

Defined in

src/core/component/Component.ts:27


id

id: number

用于表示组件的唯一标识

Inherited from

Component.id

Defined in

src/core/component/Component.ts:12


loop

loop: number = 0

循环次数,0:不循环,-1:一直循环

Defined in

src/core/component/Audio.ts:118


name

name: string = ''

组件名

Inherited from

Component.name

Defined in

src/core/component/Component.ts:17


node

node: Transform<Container>

组件挂载节点

Inherited from

Component.node

Defined in

src/core/component/Component.ts:22


paused

paused: boolean = true

指示媒体元素是否被暂停

Defined in

src/core/component/Audio.ts:151


playCount

playCount: number = 0

已经播放了的次数
如果要播放指定次数,需要将此参数重置

ts

// 创建节点
const node = new Transform();
// 添加音频组件
const audio = node.addComponent(Audio);
// 设置循环播放两次
audio.loop = 2;
// 监听播放完成事件
audio.emitter.on(Audio.Event.ENDED, () => {
     // 重置已播放次数
     audio.playCount = 0;
     // 再次播放
     audio.play();
}, audio);
// 加载素材
Resource.load('horse.mp3').then((res: IRes) => {
     // 设置音频素材
     audio.source = res.data;
     // 播放音频
     audio.play();
});

Defined in

src/core/component/Audio.ts:146


Event

Static Event: Object

事件

Type declaration

NameTypeDescription
ENDED"ended"音频播放完成

Defined in

src/core/component/Audio.ts:32

Accessors

currentTime

get currentTime(): number

音频当前播放的时间

Returns

number

Defined in

src/core/component/Audio.ts:65

set currentTime(val): void

Parameters

NameType
valnumber

Returns

void

Defined in

src/core/component/Audio.ts:68


duration

get duration(): number

获取获取音频总时长

Returns

number

Defined in

src/core/component/Audio.ts:57


playbackRate

get playbackRate(): number

音频播放速度0.25-5.0,不在此范围将被静音,默认值 1.0

Returns

number

Defined in

src/core/component/Audio.ts:79

set playbackRate(val): void

Parameters

NameType
valnumber

Returns

void

Defined in

src/core/component/Audio.ts:82


source

get source(): HTMLAudioElement

Returns

HTMLAudioElement

Defined in

src/core/component/Audio.ts:111

set source(val): void

音频资源

Parameters

NameType
valHTMLAudioElement

Returns

void

Defined in

src/core/component/Audio.ts:95


volume

get volume(): number

表示音频的音量。值从 0.0(静音)到 1.0(最大音量)。

Returns

number

Defined in

src/core/component/Audio.ts:44

set volume(val): void

Parameters

NameType
valnumber

Returns

void

Defined in

src/core/component/Audio.ts:47

Methods

destroy

destroy(): void

消毁

Returns

void

Inherited from

Component.destroy

Defined in

src/core/component/Component.ts:55


onDestroy

Optional onDestroy(): void

组件被销毁时触发

Returns

void

Inherited from

Component.onDestroy

Defined in

src/core/component/Component.ts:62


pause

pause(): void

暂停音频

Returns

void

Defined in

src/core/component/Audio.ts:164


play

play(): void

播放音频

Returns

void

Defined in

src/core/component/Audio.ts:156


start

Optional start(): void

组件被挂载到节点时触发

Returns

void

Inherited from

Component.start

Defined in

src/core/component/Component.ts:44


stop

stop(): void

停止播放
会将播放时间重置到0

Returns

void

Defined in

src/core/component/Audio.ts:173


update

Optional update(time): void

每帧调用

Parameters

NameTypeDescription
timenumber和上一帧的间隔时间 ms

Returns

void

Inherited from

Component.update

Defined in

src/core/component/Component.ts:50