Stream 对象
Stream
对象是 Stream 模式下的媒体对象,可以用来在页面上播放媒体。
userId
string
类型 标记这个 Stream
是来自哪个用户发布的
isDestroyed
boolean
类型 用于标记这个流是否调用了 release 方法,即表示这个流已经被释放
isLoop
boolean
类型 如果这个流是通过 外部音频导入 创建的,这个值表示是否循环播放外部导入的媒体源,通过麦克风/摄像头采集或者订阅下来的流不用关注这个值。
enableAudio
boolean
类型 是否开启了音频
enableVideo
boolean
类型 是否开启了视频
muteAudio
boolean
类型 是否静音了音频
muteVideo
boolean
类型 是否黑屏了视频
mute 是指保留传输通道的前提下暂时停止数据输出
audioTrack
MediaStreamTrack | undefined
类型 如果这个流开启了音频的话,这个返回原生的音频媒体对象,一般场景不会用到
videoTrack
MediaStreamTrack | undefined
类型 如果这个流开启了视频的话,这个返回原生的视频媒体对象,一般场景不会用到
play(container, muted)
HTMLElement
container:在传入的 container
下创建相应的 <audio>
和 <video>
元素播放媒体。详细参见播放媒体流
boolean
muted: 是否使用静音模式,如果为 true 就不会播放音频,仅播放视频,默认为 false
如果您在还没有与用户任何交互的情况下调用了play方法会导致音频无法播放,详情参考我们的博文:浏览器的自动播放策略的注意事项
getStats()
StatsReport
返回 获取当前 Stream
的传输状态数据,SDK 内部会每秒更新一次。StatsReport
的详细格式见下:
StatsReport {
videoBitrate: number; // 当前视频实时码率
audioBitrate: number; // 当前音频实时码率
videoBytes: number; // 截止目前视频一共发送/接收的字节数
audioBytes: number; // 截止目前音频一共发送/接收的字节数
videoPackets: number; // 截止目前视频一共发送/接收的包数
audioPackets: number; // 截止目前音频一共发送/接收的包数
videoPacketLoss: number; // 截止目前视频一共的丢包数
audioPacketLoss: number; // 截止目前音频一共的丢包数
videoPacketLossRate: number; // 当前的视频丢包率
audioPacketLossRate: number; // 当前的音频丢包率
timestamp: number; //时间戳
}
该方法仅支持 Chrome 和 Firefox
release()
停止从远端或者采集设备拉取媒体数据,释放这个 Stream
取消订阅会自动调用相应
Stream
的release
,但是取消发布不会调用,如果需要请手动调用
onAudioBuffer(callback, bufferSize?)
(buffer: AudioBuffer) => any
callback: 设置这个方法后,SDK 会收集音频的原始 Buffer 数据,然后回调给传入的 callback
函数中。AudioBuffer 是浏览器原生的音频 Buffer 对象。
number
可选
bufferSize: 是指每次回调 Buffer 的长度,默为 4096 位。
bufferSize
必须是 2 的 n 次方且大于 256 小于 16394
stream.onAudioBuffer((buffer) => {
console.log("get audio buffer!", buffer);
});
setVolume(value)
number
value: 设置音量,目前仅对采集到的 Stream 有效(订阅 Stream 无效)
value 是增益值,0 代表静音,2,代表当前声音的 2 倍
getCurrentTimeDomainData()
Uint8Array
返回获取当前 2048 位的时域数据
通常用于音频可视化的数据,可以配合 requestAnimationFrame 和 canvas 实现绘制声波图
getCurrentFrequencyDomainData()
Uint8Array
返回获取当前 2048 位的频域数据
通常用于音频可视化的数据,可以配合 requestAnimationFrame 和 canvas 实现绘制频谱图
getCurrentVolumeLevel()
音频Track调用才会生效
number
返回 获取当前实时的音量等级,返回值的范围在 0 - 1 之间,可以用于音量的可视化。
getCurrentFrameDataURL()
开启了视频(enableVideo: true)调用才会生效
截帧
DataURL
返回:当视频不存在时返回 "data:,"
setAudioSourceLoop(isLoop)
boolean
isLoop: 只有通过 外部音频导入 创建的流才能调用此方法
设置是否循环播放导入的媒体源
startAudioSource()
只有通过 外部音频导入 创建的流才能调用此方法
开始处理播放输入的媒体源
注意这里并不是真正意义上的播放(播放媒体源的声音并被听见),仅仅是开始处理这些媒体源数据,只有当 play 方法被调用后,这些处理后的媒体数据才会被送进声卡,从而在页面上播放被用户听见。
pauseAudioSource()
只有通过 外部音频导入 创建的流才能调用此方法
暂停处理输入的媒体源,这将会导致音频被暂停
resumeAudioSource()
只有通过 外部音频导入 创建的流才能调用此方法
恢复处理输入的媒体源,如果调用了 pauseAudioSource 方法,可以通过这个方法恢复。
stopAudioSource()
只有通过 外部音频导入 创建的流才能调用此方法
停止处理输入的媒体源,同时释放相应的媒体源资源
getAudioSourceCurrentTime()
number
返回: 只有通过 外部音频导入 创建的流才能调用此方法
获取当前处理的媒体源的时间,简单来说就是当前音乐播放的位置,单位是秒
setAudioSourceCurrentTime(time)
number
time: 只有通过 外部音频导入 创建的流才能调用此方法
跳转至指定播放位置开始处理媒体源,也就是设置音频当前的播放时间,单位是秒
getAudioSourceDuration()
number
返回: 只有通过 外部音频导入 创建的流才能调用此方法
获取当前媒体源的总时长,单位是 秒
事件列表
release
当 Stream
调用 release
方法的时候触发,表示这个 Stream
已经被释放。无论是自己手动调用还是 SDK 自动调用都会触发。
stream.once("release", () => {
console.log("stream", stream, "is released!");
})
audio-ended
当音频 Track 由于任何原因不再向流提供数据时发生此事件,包括到达媒体输入的结束,用户撤消所需的权限,删除源设备或远程结束连接。
常见于 USB 输入设备被拔出时。订阅下来的 Stream 不会触发此事件。
track.once("audio-ended", () => {
console.log("audio track ended!");
})
video-ended
当视频 Track 由于任何原因不再向流提供数据时发生此事件,包括到达媒体输入的结束,用户撤消所需的权限,删除源设备或远程结束连接。
常见于 USB 输入设备被拔出或 Chrome 下在屏幕共享过程中用户自行取消了屏幕共享。订阅下来的 Stream 不会触发此事件。
track.once("video-ended", () => {
console.log("video track ended!");
})
audio-source-state-change
只有通过 外部音频导入 创建的流才会触发此事件
当处理媒体源的状态发生变化时,会触发这个事件,全部的状态介绍见下:
AudioSourceState {
IDLE = "idle", // 表示还没有开始处理播放媒体源,初始状态
LOADING = "loading", // 在处理在线媒体源的时候会触发,表示还在加载
PLAY = "play", // 表示媒体源在正常处理播放
PAUSE = "pause", // 表示媒体源被暂停
END = "end", // 表示媒体源播放结束
}
示例:
stream.on("audio-source-state-change", (currState, lastState) => {
console.log(`audio source state change, ${lastState} -> ${currState}`);
});