名称 | 值 | 说明 |
|---|---|---|
CONNECT | 0 | 设备连接 |
DISCONNECT | 1 | 断开设备连接 |
添加音频设备连接状态发生变化的监听,在监听里完成再次连接后开启播放的逻辑。
import audio from '@ohos.multimedia.audio';
@Entry
@Component
struct AudioConnectPlayExample {
private controller: VideoController = new VideoController();
private videoSrc: string = 'https://xxxxxxxxxx.mp4'; // 具体的视频地址
private audioManager = audio.getAudioManager();
private isPlay = false;
aboutToAppear(): void {
let audioRoutingManager = this.audioManager.getRoutingManager();
// 注册音频输出设备改变状态事件
audioRoutingManager.on('deviceChange', audio.DeviceFlag.OUTPUT_DEVICES_FLAG,
(deviceChanged: audio.DeviceChangeAction) => {
// 设备连接状态变化,0为连接,1为断开连接
if (deviceChanged.type === 1 && this.isPlay) {
this.controller.pause();
} else if (deviceChanged.type === 0 && !this.isPlay) {
this.controller.start();
}
});
}
aboutToDisappear(): void {
let audioRoutingManager = this.audioManager.getRoutingManager();
// 注销音频输出设备改变状态事件
audioRoutingManager.off('deviceChange');
}
build() {
Column() {
Video({
src: this.videoSrc,
controller: this.controller
})
.onStart(() => {
this.isPlay = true;
})
.onPause(() => {
this.isPlay = false;
})
}
}
}