HarmonyOS鸿蒙Next中SurfaceProvider+Player视频播放开发过程分享
HarmonyOS鸿蒙Next中SurfaceProvider+Player视频播放开发过程分享
- 视频播放的功能
显示效果:
部分代码如下:
package com.example.jltflianxi.slice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.Source;
import ohos.media.player.Player;
import java.io.*;
import java.text.Format;
import java.util.jar.Manifest;
public class MainAbilitySlice extends AbilitySlice {
...
}
vedioabilitysilce中
package com.example.jltflianxi.slice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.global.resource.RawFileDescriptor;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.Source;
import ohos.media.player.Player;
public class VideoAbilitySlice extends AbilitySlice {
...
}
配置文件config.json中
{
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home",
"action.vedio_layout"
]
}
],
"orientation": "landscape",
"visible": true,
"name": "com.example.jltflianxi.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "jltflianxi",
"type": "page",
"launchType": "standard"
}
]
}
我们用的是SurfaceProvider
+ Player
完成的播放
暂停播放。
获取当前播放位置。
获取媒体文件总时长。
设置播放速度。
获取播放速度。
总结:播放本地的视频功能开发已经完成,但是一些小插件没有完成,比如进度条,暂停,播放时长与分布式特征的各项实现等。
在这里面找找,有视频切换的案例的。
更多关于HarmonyOS鸿蒙Next中SurfaceProvider+Player视频播放开发过程分享的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
视频的切换我们后面会有案例
视频切换案例在哪儿能看?
当前player切换视频这么做?
在HarmonyOS鸿蒙Next中,使用SurfaceProvider
和Player
进行视频播放开发的过程如下:
-
环境准备:确保开发环境已安装HarmonyOS SDK,并配置好开发工具。
-
创建项目:在DevEco Studio中创建一个新的HarmonyOS项目,选择适当的模板。
-
添加依赖:在
build.gradle
文件中添加media
模块的依赖。 -
布局文件:在
ability_main.xml
中添加SurfaceProvider
组件,用于显示视频。 -
初始化Player:在Java代码中初始化
Player
对象,并设置数据源。 -
关联SurfaceProvider:将
Player
与SurfaceProvider
关联,设置显示表面。 -
控制播放:实现播放、暂停、停止等控制逻辑。
-
生命周期管理:在
onStart
、onStop
等生命周期方法中管理Player
的资源和状态。 -
调试与测试:运行应用,检查视频播放功能是否正常。
通过以上步骤,可以在HarmonyOS鸿蒙Next中实现SurfaceProvider
和Player
的视频播放功能。
在HarmonyOS鸿蒙Next中,使用SurfaceProvider
与Player
实现视频播放的开发过程如下:
-
创建SurfaceProvider:在布局文件中添加
SurfaceProvider
组件,用于承载视频画面。 -
初始化Player:在代码中创建
Player
实例,并设置数据源(如本地文件或网络URL)。 -
绑定Surface:将
Player
的Surface
与SurfaceProvider
绑定,通过Player.setVideoSurface()
方法实现。 -
播放控制:调用
Player.prepare()
准备视频,随后Player.start()
开始播放。支持暂停、停止、快进等操作。 -
生命周期管理:在
onPause()
、onResume()
等生命周期方法中处理Player
的状态,确保资源释放与恢复。
通过以上步骤,即可在鸿蒙Next中实现视频播放功能。