08-12 16:32:27.873 1654 22650 I C02B91/av_session/AVSession: [GetAllSessionDescriptors]GetAllSessionDescriptors with size=1, topSession:com.***
建议应用正确创建并接入AVSession,确保设置正确的元数据,且至少注册一条控制命令,否则播控中心不显示当前应用接入的播控中心。参考示例如下:
import { avSession as AVSessionManager } from '@kit.AVSessionKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct avSessionExample {
message: string = 'hello world';
@State currentState: AVSessionManager.AVPlaybackState = {
state: AVSessionManager.PlaybackState.PLAYBACK_STATE_INITIAL, // 播放状态。
position: {
elapsedTime: 0, // 已经播放的位置,以ms为单位。
updateTime: new Date().getTime(), // 应用更新当前位置时的时间戳,以ms为单位。
},
};
@State elapsedTime: number = 0;
@State updateTime: number = 0;
context = this.getUIContext().getHostContext() as Context;
type: AVSessionManager.AVSessionType = 'video';
session: AVSessionManager.AVSession | null = null;
controller: VideoController = new VideoController();
aboutToAppear(): void {
this.initAvsession();
}
aboutToDisappear(): void {
this.destroySession();
}
async initAvsession() {
// 创建并激活媒体会话
this.session = await AVSessionManager.createAVSession(this.context, 'SESSION_NAME', this.type);
this.setAVMetadata();
this.registerSessionListener();
this.session.activate();
}
private registerSessionListener() {
this.session!.on('play', () => {
console.info(`on play , do pause task`);
this.controller.start();
});
this.session!.on('pause', () => {
this.controller.pause();
});
}
// 设置播放状态
private setAVPlaybackState(currentState: AVSessionManager.AVPlaybackState) {
this.session!.setAVPlaybackState(currentState, (err) => {
if (err) {
console.error(`Failed to set AVPlaybackState. Code: ${err.code}, message: ${err.message}`);
} else {
console.info(`SetAVPlaybackState successfully`);
}
});
}
// 设置元数据
private setAVMetadata() {
let metadata: AVSessionManager.AVMetadata = {
assetId: '0',
title: 'TITLE',
mediaImage: 'IMAGE',
duration: 91000,
};
this.session!.setAVMetadata(metadata).then(() => {
console.info(`SetAVMetadata successfully`);
}).catch((err: BusinessError) => {
console.error(`Failed to set AVMetadata. Code: ${err.code}, message: ${err.message}`);
});
}
async destroySession() {
this.session?.off('play');
this.session?.off('pause');
this.session?.destroy();
}
build() {
Column() {
Video({
src: $r('app.media.videoTest'),
controller: this.controller,
})
.width('100%')
.height(300)
.onStart(() => {
console.info('onStart');
this.currentState.state = AVSessionManager.PlaybackState.PLAYBACK_STATE_PLAY;
this.updateTime = new Date().getTime();
this.currentState.position = { elapsedTime: this.elapsedTime, updateTime: this.updateTime };
this.setAVPlaybackState(this.currentState);
})
.onPause(() => {
console.info('onPause');
this.currentState.state = AVSessionManager.PlaybackState.PLAYBACK_STATE_PAUSE;
this.updateTime = new Date().getTime();
this.currentState.position = { elapsedTime: this.elapsedTime, updateTime: this.updateTime };
this.setAVPlaybackState(this.currentState);
})
.onSeeked((playbackInfo: PlaybackInfo) => {
console.info('onSeeked');
this.elapsedTime = playbackInfo.time * 1000;
this.updateTime = new Date().getTime();
this.currentState.position = { elapsedTime: this.elapsedTime, updateTime: this.updateTime };
this.setAVPlaybackState(this.currentState);
})
.onUpdate((playbackInfo: PlaybackInfo) => {
console.info('onUpdate');
this.elapsedTime = playbackInfo.time * 1000;
this.currentState.position = { elapsedTime: this.elapsedTime, updateTime: this.updateTime };
})
}
.width('100%')
.height('100%')
}
} 应用如需长时间后台播放,则还需按需创建后台任务。