一、音频

AVPlayer的主要工作是将音频、视频媒体资源(如MP4、MP3、MKV、MPEG-TS等)转码为可供渲染的图像和可听见的音频模拟信号,并通过输出设备进行播放。 AVPlayer提供了功能完善的一体化播放功能,应用程序只需提供流媒体来源,即可实现播放效果,无须负责数据解析和解码。

音频容器规格

规 格 描 述

m4a

音频格式:AAC

aac

音频格式:AAC

mp3

音频格式:MP3

ogg

音频格式:VORBIS

wav

音频格式:PCM

 音频播放

import { media } from '@kit.MediaKit'
import { router } from '@kit.ArkUI'

@Entry
@Component
struct MusicPage {
  build() {
    Column({space: 20}) {
      Button('播放音频')
        .onClick(async () => {
          // 1. 创建Avplayer对象
          const avPlayer = await media.createAVPlayer()

          // 2. 开启状态机
          avPlayer.on('stateChange',(state) => {
            switch (state) {
              case 'initialized':
                avPlayer.prepare()
                break
              case 'prepared':
                avPlayer.play()
                break
            }
          })

          // 3. 设置音乐源
          const url = 'http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/HeimaCloudMusic/3.m4a'
          avPlayer.url = url
        })

      Button('返回')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          router.back();
        })
    }
    .width('100%')
    .height('100%')
  }
}

注意:这里请求的是网络资源,需要在module.json5文件里申请ohos.permission.INTERNET权限。 

二、视频

import { router } from '@kit.ArkUI';

@Entry
@Component
struct VideoPage {
  @State videoSrc: ResourceStr = ''; // 视频资源
  @State nextIndex: number = 0;
  @State lastIndex: number = 0;
  @State currentIndex: number = 0;
  @State array: Array<string> = [];
  private title: string = '视频案例';
  private videoData: Array<string> = [
    '第一次爱的人.mp4',
    '梦的光点.mp4'
  ];
  private controller: VideoController = new VideoController();
  private previewUri: ResourceStr =
    'https://q7.itc.cn/q_70/images03/20240519/624bb137d72949dd820ec4c21f0137c7.jpeg'; // 视频未播放时的图片

  aboutToAppear(): void {
    this.videoSrc = $rawfile(this.videoData[this.currentIndex]);
    this.array.push(this.videoData[this.currentIndex]);
  }

  build() {
    Column({ space: 20 }) {
      Text(this.title)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)

      Video({
        src: this.videoSrc,
        controller: this.controller,
        previewUri: this.previewUri
      })
        .width(300)
        .height(550)
        .muted(false)// 是否静音
        .controls(true)// 是否显示进度条
        .autoPlay(false)// 是否自动播放
        .loop(true)// 是否循环播放
        .objectFit(ImageFit.Contain) // 保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内

      Row({ space: 10 }) {
        Button('上一个')
          .onClick(() => {
            this.lastIndex++;
            this.currentIndex = (this.array.length - 1) - this.lastIndex;
            if (this.currentIndex >= 0) {
              this.videoSrc = $rawfile(this.array[this.currentIndex]);
            } else {
              AlertDialog.show({
                title: '提示',
                message: '已经到顶'
              });
            }
          })
        Button('开始播放')
          .onClick(() => {
            this.controller.start();
          })
        Button('暂停')
          .onClick(() => {
            this.controller.pause();
          })
        Button('下一个')
          .onClick(() => {
            this.nextIndex++;
            this.currentIndex = this.nextIndex;
            this.videoSrc = $rawfile(this.videoData[this.currentIndex]);
            this.array.push(this.videoData[this.nextIndex]);
          })
      }

      Button('返回')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          router.back();
        })
    }
    .width('100%')
    .height('100%')
    .margin({ top: 10 })
  }
}

注意:与音频一样,视频资源也需要在module.json5文件里申请ohos.permission.INTERNET权限,同时,视频文件放在rawfile文件夹里。

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐