HarmonyOS鸿蒙Next中SurfaceProvider+Player视频播放开发过程分享

HarmonyOS鸿蒙Next中SurfaceProvider+Player视频播放开发过程分享

  1. 视频播放的功能

显示效果:

部分代码如下:

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 完成的播放

暂停播放。
获取当前播放位置。
获取媒体文件总时长。
设置播放速度。
获取播放速度。

总结:播放本地的视频功能开发已经完成,但是一些小插件没有完成,比如进度条,暂停,播放时长与分布式特征的各项实现等。

7 回复

在这里面找找,有视频切换的案例的。

更多关于HarmonyOS鸿蒙Next中SurfaceProvider+Player视频播放开发过程分享的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


视频的切换我们后面会有案例

视频切换案例在哪儿能看?

当前player切换视频这么做?

在HarmonyOS鸿蒙Next中,使用SurfaceProviderPlayer进行视频播放开发的过程如下:

  1. 环境准备:确保开发环境已安装HarmonyOS SDK,并配置好开发工具。

  2. 创建项目:在DevEco Studio中创建一个新的HarmonyOS项目,选择适当的模板。

  3. 添加依赖:在build.gradle文件中添加media模块的依赖。

  4. 布局文件:在ability_main.xml中添加SurfaceProvider组件,用于显示视频。

  5. 初始化Player:在Java代码中初始化Player对象,并设置数据源。

  6. 关联SurfaceProvider:将PlayerSurfaceProvider关联,设置显示表面。

  7. 控制播放:实现播放、暂停、停止等控制逻辑。

  8. 生命周期管理:在onStartonStop等生命周期方法中管理Player的资源和状态。

  9. 调试与测试:运行应用,检查视频播放功能是否正常。

通过以上步骤,可以在HarmonyOS鸿蒙Next中实现SurfaceProviderPlayer的视频播放功能。

在HarmonyOS鸿蒙Next中,使用SurfaceProviderPlayer实现视频播放的开发过程如下:

  1. 创建SurfaceProvider:在布局文件中添加SurfaceProvider组件,用于承载视频画面。

  2. 初始化Player:在代码中创建Player实例,并设置数据源(如本地文件或网络URL)。

  3. 绑定Surface:将PlayerSurfaceSurfaceProvider绑定,通过Player.setVideoSurface()方法实现。

  4. 播放控制:调用Player.prepare()准备视频,随后Player.start()开始播放。支持暂停、停止、快进等操作。

  5. 生命周期管理:在onPause()onResume()等生命周期方法中处理Player的状态,确保资源释放与恢复。

通过以上步骤,即可在鸿蒙Next中实现视频播放功能。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!