首先需要导包
import { media } from '@kit.MediaKit'
创建avPlayer实例
let avPlayer: media.AVPlayer = await media.createAVPlayer();
AvPlayer提供一套状态机进行视频生命周期的控制
以下给出使用状态机的示例:
private setStateChangeCallback(avPlayer: media.AVPlayer) {
avPlayer.on('stateChange', async (state: string) => {
switch (state) {
case 'idle':
Logger.info(TAG, 'AVPlayer state idle called.' + ` this.curIndex:${this.curIndex}`);
break;
case 'initialized':
Logger.info(TAG, 'AVPlayer state initialized called.' + ` this.curIndex:${this.curIndex}`);
avPlayer.surfaceId = this.surfaceID;
avPlayer.prepare();
break;
case 'prepared':
Logger.info(TAG, `AVPlayer state prepared called. this.curIndex:${this.curIndex}`);
this.isReady = true;
avPlayer.loop = true;//循环播放
this.duration = avPlayer.duration;//获取时常
this.durationTime = Math.floor(this.duration / CommonConstants.SECOND_TO_MS);
this.durationStringTime = secondToTime(this.durationTime);//时常字符串
if (this.curIndex === this.curSource.index) {
this.playVideo();//视频播放方法
}
break;
case 'completed':
Logger.info(TAG,
'AVPlayer state completed called.' + ` this.curIndex:${this.curIndex}`);
this.isPlaying = false;
break;
case 'playing':
this.isPlaying = true;
Logger.info(TAG,
'AVPlayer state playing called.' + ` this.curIndex:${this.curIndex}`);
break;
case 'paused':
Logger.info(TAG,
'AVPlayer state paused called.' + ` this.curIndex:${this.curIndex}`);
this.isPlaying = false;
break;
case 'stopped':
Logger.info(TAG,
'AVPlayer state stopped called.' + ` this.curIndex:${this.curIndex}`);
break;
case 'released':
Logger.info(TAG,
'AVPlayer state released called.' + ` this.curIndex:${this.curIndex}`);
break;
case 'error':
Logger.info(TAG,
'AVPlayer state error called.' + ` this.curIndex:${this.curIndex}`);
break;
default:
Logger.info(TAG,
'AVPlayer state unknown called.' + ` this.curIndex:${this.curIndex}`);
break;
}
})
}
以及播放中断监测:
private setInterruptCallback() {
if (!this.avPlayer) {
return;
}
this.avPlayer.on('audioInterrupt', async (interruptEvent: audio.InterruptEvent) => {//监听音频焦点切换信息
// When an audio interruption event occurs, the `audioRenderer` receives the `interruptEvent` callback. Here,
// handle it based on its content:
// 1. Optional: Read the type of `interruptEvent.forceType` to determine if the system has already enforced
// the corresponding action.
// Note: Under the default focus strategy, `INTERRUPT_HINT_RESUME` is of type `INTERRUPT_SHARE`, while all
// other hint types are of `INTERRUPT_FORCE`.Therefore, checking `forceType` may not be necessary.
// 2. Required: Read the type of `interruptEvent.hintType` and perform the corresponding handling.
if (interruptEvent.forceType === audio.InterruptForceType.INTERRUPT_FORCE) {
// For the INTERRUPT_FORCE type: Audio-related processing has been performed by the system, and the
// application needs to update its own state and make the corresponding adjustments.
switch (interruptEvent.hintType) {
case audio.InterruptHint.INTERRUPT_HINT_PAUSE:
// This branch indicates that the system has paused the audio stream (temporarily lost focus).
// To maintain consistency in state, the application should switch to the audio paused state.
// Temporarily lost focus: After other audio streams release the audio focus, this audio stream
// will receive a corresponding resume audio interruption event, at which point it can resume
// playback on its own.
this.updateIsPlay(false);
this.pauseVideo();
break;
case audio.InterruptHint.INTERRUPT_HINT_STOP:
// This branch indicates that the system has stopped the audio stream (permanently lost focus).
// To maintain consistency in state, the application should switch to the audio paused state.
// Permanently lost focus: No further audio interruption events will be received. To resume
// playback, user intervention is required.
this.updateIsPlay(false);
this.pauseVideo();
break;
case audio.InterruptHint.INTERRUPT_HINT_DUCK:
// This branch indicates that the system has reduced the audio volume (default to 20% of the normal volume).
// To maintain consistency in state, the application should switch to the reduced volume playback state.
// If the application does not accept reduced volume playback, it can choose an alternative handling method
// here, such as pausing playback.
break;
case audio.InterruptHint.INTERRUPT_HINT_UNDUCK:
// This branch indicates that the system has restored the audio volume to normal. To maintain
// consistency in state, the application should switch to the normal volume playback state.
break;
default:
break;
}
} else if (interruptEvent.forceType === audio.InterruptForceType.INTERRUPT_SHARE) {
// For the INTERRUPT_SHARE type: The application can choose to perform related actions or ignore the
// audio interruption event.
switch (interruptEvent.hintType) {
case audio.InterruptHint.INTERRUPT_HINT_RESUME:
// This branch indicates that the audio stream, which was paused due to temporary loss of focus,
// can now resume playing. It is recommended that the application resumes playback and switches
// to the audio playback state.
// If the application does not want to resume playback at this point, it can ignore this audio
// interruption event and take no action.
// Resume playback by explicitly calling `start()` here, and record the result of the `start()`
// call in a flag variable `started`.
this.playVideo();
break;
default:
break;
}
}
})
}