鸿蒙视频开发介绍

首先需要导包

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;
        }
      }
    })
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值