跳到主要内容

egret.SoundChannel

   egret

接口    public interface SoundChannel

继承    egret.SoundChannel → egret.IEventDispatcheregret.HashObject

SoundChannel 类控制应用程序中的声音。每个声音均分配给一个声道,而且应用程序可以具有混合在一起的多个声道。SoundChannel 类包含 stop() 方法、用于设置音量和监视播放进度的属性。

公共属性

属性
position : number
当播放声音时,position 属性表示声音文件中当前播放的位置(以秒为单位)
volume : number
音量范围从 0(静音)至 1(最大音量)

公共方法

方法
stop():void
停止在该声道中播放声音

事件

Events
egret.Event.SOUND_COMPLETE
音频最后一次播放完成时抛出

属性详细信息

position

position : number

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

当播放声音时,position 属性表示声音文件中当前播放的位置(以秒为单位)

volume

volume : number

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

音量范围从 0(静音)至 1(最大音量)。

方法详细信息

stop()

public stop():void

停止在该声道中播放声音。

  • 支持版本:Egret 2.4
  • 运行平台:Web,Native

示例

/*
* 以下示例加载一个 MP3 文件,进行播放,并输出播放该 MP3 文件时所发生的声音事件的相关信息。
*/
class SoundExample extends egret.DisplayObjectContainer {
public constructor() {
super();
this.startLoad();
}
private startLoad():void {
//创建 Sound 对象
var sound = new egret.Sound();
var url:string = "resource/assets/sound.mp3";
//添加加载完成侦听
sound.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);
//开始加载
sound.load(url);
}
private onLoadComplete(event:egret.Event):void {
//获取加载到的 Sound 对象
var sound:egret.Sound = <egret.Sound>event.target;
//播放音乐
var channel:egret.SoundChannel = sound.play(0,1);
channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);
}
private onSoundComplete(event:egret.Event):void {
egret.log("onSoundComplete");
}
}