news 2026/8/6 22:16:14

HarmonyOS 应用开发《掌上英语》第99篇:使用AVPlayer设置播放URL(ArkTS)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
HarmonyOS 应用开发《掌上英语》第99篇:使用AVPlayer设置播放URL(ArkTS)

使用AVPlayer设置播放URL(ArkTS)

本开发指导将介绍如何使用AVPlayer开发播放功能,在不同的场景下如何设置URL。

当前指导仅介绍播放URL设置方法,其他场景及完整示例代码,请参考视频播放。

当前开发指导将提供以下设置播放URL的方法:

  • 流媒体播放场景下设置URL
  • 本地Raw文件播放场景下设置URL

流媒体播放场景下设置URL

情况一:播放HTTP/HTTPS媒体资源

import{media}from'@kit.MediaKit';// 类成员定义avPlayer。privateavPlayer:media.AVPlayer|null=null;// 在业务函数中(示例工程函数名为avSetupURL):// 创建avPlayer实例对象。this.avPlayer=awaitmedia.createAVPlayer();// 设置对应的播放url。leturl='https://xxx.xxx.xxx.mp4';if(this.avPlayer==null){return;}this.avPlayer.url=url;

情况二:HLS媒体资源播放(点播/直播)

import{media}from'@kit.MediaKit';// 类成员定义avPlayer。privateavPlayer:media.AVPlayer|null=null;// 在业务函数中(示例工程函数名为avSetupURL):// 创建avPlayer实例对象。this.avPlayer=awaitmedia.createAVPlayer();// 设置对应的播放url。leturl='https://xxx.xxx.xxx.xxx:xx/xx/index.m3u8';if(this.avPlayer==null){return;}this.avPlayer.url=url;

情况三:设置HTTP请求头信息播放

当服务器需要校验HTTP请求头信息时,可通过createMediaSourceWithUrl设置HTTP请求头信息。

import{media}from'@kit.MediaKit';// 类成员定义avPlayer。privateavPlayer:media.AVPlayer|null=null;// 在业务函数中(示例工程函数名为avSetupURL):// 创建avPlayer实例对象。this.avPlayer=awaitmedia.createAVPlayer();// 设置对应的播放url。leturl='https://xxx.xxx.xxx.xxx:xx/xx/index.m3u8';// 创建mediaSource实例对象,设置媒体来源,定制HTTP请求,如需要,可以键值对的形式设置User-Agent、Cookie、Referer等字段。letmediaSource:media.MediaSource=media.createMediaSourceWithUrl(url,{"User-Agent":"User-Agent-Value","Cookie":"Cookie-Value","Referer":"Referer-Value"});// 设置播放策略,设置缓冲区数据量为3s。letplaybackStrategy:media.PlaybackStrategy={preferredWidth:1,preferredHeight:2,preferredBufferDuration:3,preferredHdr:false};// 为avPlayer设置媒体来源和播放策略。this.avPlayer.setMediaSource(mediaSource,playbackStrategy);

情况四:通过本地Raw文件中的m3u8文件播放在线流媒体资源

当应用需要通过解析本地Raw文件中的m3u8文件,播放在线流媒体资源时,可以通过resourceManager.getRawFd获取文件描述符,将其拼接成fdUrl,并通过setMimeType设置MIME类型为APPLICATION_M3U8。

import{media}from'@kit.MediaKit';import{common}from'@kit.AbilityKit';// 类成员定义avPlayer和context。privateavPlayer:media.AVPlayer|null=null;privatecontext:common.UIAbilityContext|undefined=undefined;// 在业务函数中(示例工程函数名为avSetupURL):// 创建avPlayer实例对象。this.avPlayer=awaitmedia.createAVPlayer();this.context=this.getUIContext().getHostContext()ascommon.UIAbilityContext;// 通过本地m3u8文件名,获取文件描述符。letfileDescriptor=awaitthis.context.resourceManager.getRawFd('xxx.m3u8');// 用文件描述符构造本地m3u8的URL。letfdUrl:string="fd://"+fileDescriptor.fd+"?offset="+fileDescriptor.offset+"&size="+fileDescriptor.length;// 按需设置HTTP请求头。letheaders:Record<string,string>={"User-Agent":"User-Agent-Value","Cookie":"Cookie-Value"};// 通过本地m3u8的URL和HTTP请求头构造mediaSource媒体来源。letmediaSource:media.MediaSource=media.createMediaSourceWithUrl(fdUrl,headers);// 设置媒体MIME类型为APPLICATION_M3U8。letmimeType:media.AVMimeTypes=media.AVMimeTypes.APPLICATION_M3U8;mediaSource?.setMimeType(mimeType);// 设置播放策略,设置缓冲区数据量为20s。letplaybackStrategy:media.PlaybackStrategy={preferredBufferDuration:20};// 为avPlayer设置媒体来源和播放策略。this.avPlayer.setMediaSource(mediaSource,playbackStrategy);

情况五:通过应用沙箱中的m3u8文件播放在线流媒体资源

当应用需要通过解析应用沙箱中的m3u8文件,播放在线流媒体资源时,可以通过fileIo.openSync获取文件句柄,将其拼接成fdUrl,并通过setMimeType设置MIME类型为APPLICATION_M3U8。

import{media}from'@kit.MediaKit';import{fileIo}from'@kit.CoreFileKit';import{common}from'@kit.AbilityKit';// 类成员定义avPlayer和context。privateavPlayer:media.AVPlayer|null=null;privatecontext:common.UIAbilityContext|undefined=undefined;privatem3u8FileName:string='';// 在业务函数中(示例工程函数名为avSetupURL):// 创建avPlayer实例对象。this.avPlayer=awaitmedia.createAVPlayer();this.context=this.getUIContext().getHostContext()ascommon.UIAbilityContext;// 通过UIAbilityContext获取沙箱地址filesDir,以Stage模型为例。letm3u8FileName='';letfilePath=`${this.context.filesDir}/${m3u8FileName}`;// 通过fileIo.openSync获取文件句柄。letfile=fileIo.openSync(filePath,fileIo.OpenMode.READ_ONLY);letfd:string=file.fd.toString();// 用文件句柄构造本地m3u8的URL。letfdUrl:string="fd://"+fd+"?offset="+"0"+"&size="+"0";// 按需设置HTTP请求头。letheaders:Record<string,string>={"User-Agent":"User-Agent-Value","Cookie":"Cookie-Value"};// 通过本地m3u8的URL和HTTP请求头构造mediaSource媒体来源。letmediaSource:media.MediaSource=media.createMediaSourceWithUrl(fdUrl,headers);// 设置媒体MIME类型为APPLICATION_M3U8。letmimeType:media.AVMimeTypes=media.AVMimeTypes.APPLICATION_M3U8;mediaSource?.setMimeType(mimeType);// 设置播放策略,设置缓冲区数据量为20s。letplaybackStrategy:media.PlaybackStrategy={preferredBufferDuration:20};// 为avPlayer设置媒体来源和播放策略。this.avPlayer.setMediaSource(mediaSource,playbackStrategy);

本地raw文件播放场景下设置URL

情况一:应用沙箱文件播放

import{media}from'@kit.MediaKit';import{fileIo}from'@kit.CoreFileKit';import{common}from'@kit.AbilityKit';// 类成员定义avPlayer,context和fileName。privateavPlayer:media.AVPlayer|null=null;privatecontext:common.UIAbilityContext|undefined=undefined;privatefileName:string='';// 在业务函数中(示例工程函数名为avSetupURL):// 创建avPlayer实例对象。this.avPlayer=awaitmedia.createAVPlayer();this.context=this.getUIContext().getHostContext()ascommon.UIAbilityContext;letfdPath='fd://';letfileName='test.mp4';// test.mp4为应用文件目录下的预置资源,需要开发者根据实际情况进行替换。// 通过UIAbilityContext获取沙箱地址filesDir,以Stage模型为例。letpath=`${this.context?.filesDir}/${this.fileName}`;// 打开相应的资源文件地址获取fd,并为url赋值触发initialized状态机上报。letfile=awaitfileIo.open(path);fdPath=fdPath+''+file.fd;this.avPlayer.url=fdPath;

情况二:本地文件播放

说明:
当使用AVPlayer播放本地资源时,AVPlayer会独占此fd。

import{media}from'@kit.MediaKit';import{common}from'@kit.AbilityKit';// 类成员定义avPlayer,context和fileName。privateavPlayer:media.AVPlayer|null=null;privatefileName:string='';privatecontext:common.UIAbilityContext|undefined=undefined;// 在业务函数中(示例工程函数名为avSetupURL):// 创建avPlayer实例对象。this.avPlayer=awaitmedia.createAVPlayer();this.context=this.getUIContext().getHostContext()ascommon.UIAbilityContext;// 通过UIAbilityContext的resourceManager成员的getRawFd接口获取媒体资源播放地址。// 返回类型为{fd,offset,length},fd为HAP包fd地址,offset为媒体资源偏移量,length为播放长度。letfileName='test.mp4';// test.mp4为应用文件目录下的预置资源,需要开发者根据实际情况进行替换。letfileDescriptor=awaitthis.context?.resourceManager.getRawFd(this.fileName);letavFileDescriptor:media.AVFileDescriptor={fd:fileDescriptor.fd,offset:fileDescriptor.offset,length:fileDescriptor.length};// 为fdSrc赋值触发initialized状态机上报。this.avPlayer.fdSrc=avFileDescriptor;

运行完整示例

  1. 新建工程,下载示例工程(也可直接运行),并将示例工程的以下资源复制到对应目录。

    AVPlayerArkTSURL entry/src/main/ets/ └── pages └── Index.ets (播放界面) entry/src/main/resources/ ├── base │ ├── element │ │ ├── color.json │ │ ├── float.json │ │ └── string.json │ └── media │ ├── ic_video_play.svg (播放键图片资源) │ └── ic_video_pause.svg (暂停键图片资源) └── rawfile ├── test.m3u8 (m3u8资源) └── test_01.mp3 (音频资源)
  2. 在/entry/src/main/module.json5中,申请使用网络的权限(或直接替换为示例工程的module.json5)。

    "requestPermissions":[{"name":"ohos.permission.INTERNET"},{"name":"ohos.permission.GET_WIFI_INFO"}]
  3. 通过注释、解注释/entry/src/main/ets/pages/Index.ets中的上文示例的各种情况,编译并运行。

  4. 在安装应用后,可将示例工程的/entry/src/main/resources/rawfile/test.m3u8通过以下命令加入应用沙箱,从而运行应用沙箱相关示例:(<FILESDIR>为物理路径,以示例工程为例,可通过console.info打印"this.context.filesDir"得到应用沙箱路径,再根据应用沙箱指南的应用沙箱路径和真实物理路径的对应关系表找到物理路径)。

    hdc file send "[目录]\test.m3u8" <FILESDIR> hdc file send "[目录]\test_01.mp3" <FILESDIR>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/6 22:14:20

如何的找网站建设公司-避坑指南与实战攻略,助你找到最靠谱的合作伙伴

在这个互联网渗透率几乎达到100%的今天,如果你还觉得拥有一张名片就足以立足江湖,那你可能已经错过了太多红利。如今,无论你是初创小团队,还是年入千万的传统企业转型者,网站早已不再仅仅是一个“互联网展示橱窗”,它更是你品牌的数字资产、是24小时不间断工作的销售冠军…

作者头像 李华
网站建设 2026/8/6 22:12:00

构建低消耗的产研AI工作流:Workbuddy生态 + 墨刀AI 落地实践

到了2026年&#xff0c;大模型提效在产研团队里基本算标配了&#xff0c;没人再问要不要用&#xff0c;大家默认都有。最近这阵子&#xff0c;腾讯的WorkBuddy和面向开发者的CodeBuddy在技术圈子里讨论度很高&#xff0c;我身边不少研发和产品团队已经把日常的业务梳理、代码辅…

作者头像 李华
网站建设 2026/8/6 22:09:28

WPGraphQL for WooCommerce安全最佳实践:JWT认证与会话管理

WPGraphQL for WooCommerce安全最佳实践&#xff1a;JWT认证与会话管理 【免费下载链接】wp-graphql-woocommerce Add WooCommerce support and functionality to your WPGraphQL server 项目地址: https://gitcode.com/gh_mirrors/wp/wp-graphql-woocommerce WPGraphQL…

作者头像 李华
网站建设 2026/8/6 22:09:23

数字沙盘服务商对比:2026年主流厂商能力拆解与选型指南

数字沙盘行业已形成“硬实力软技术场景理解”的三维竞争格局。2026年行业数据显示&#xff0c;72%的地产项目因使用模板化沙盘导致转化率提升不足14%&#xff0c;选对服务商已成为决定展示效果与营销转化的核心变量。以下从技术路线、标杆案例、客户口碑三个维度&#xff0c;对…

作者头像 李华
网站建设 2026/8/6 22:06:05

如何打造高转化网站专题建设方案:从底层逻辑到视觉落地的全实战指南

在这个信息爆炸的时代,网站早已不仅仅是一个展示企业形象的线上名片,它更是品牌与用户深度沟通的超级战场。很多老板或者市场负责人在提到网站建设时,首先想到的往往是“首页”或者“关于我们”,但在实际的业务转化中,我们发现一个经常被忽视却极具爆发力的板块——网站专…

作者头像 李华