HarmonyOS应用开发-视频播放
头像 Tiechui.Wang 2020-09-14 14:22:00    发布
2586 浏览 12 点赞 13 收藏

一、介绍
本篇Codelab将实现的内容
HarmonyOS是面向全场景多终端的分布式操作系统,使得应用程序的开发打破了智能终端互通的性能和数据壁垒,业务逻辑原子化开发,适配多端。通过一个简单应用开发,体验HarmonyOS的视频播放能力

您将建立什么
在这个Codelab中,你将创建Demo Project,并将Demo编译成Hap,此示例应用程序展示了如何播放视频。

您将会学到什么

1.如何创建一个HarmonyOS Demo Project
2.如何构建一个Hap并且将其部署到智慧屏真机
3.通过此示例应用体验如何播放本地或者在线视频

二、您需要什么
1. 硬件要求
操作系统:Windows10 64位
内存:8G及以上。
硬盘:100G及以上。
分辨率:1280*800及以上

2. 软件要求
需手动下载安装,详细步骤请参考《DevEco Studio使用指南》2.1.2
JDK:DevEco Studio自动安装。
Node.js:请手动下载安装,详细步骤请参考《DevEco Studio使用指南》2.1.3 下载和安装Node.js。
HarmonyOS SDK:待DevEco Studio安装完成后,利用DevEco Studio来加载HarmonyOS SDK。详细步骤请参考《DevEco Studio使用指南》2.1.6 加载HarmonyOS SDK。
Maven库依赖包:如需手动拷贝和配置,详细步骤请参考《DevEco Studio使用指南》2.3 离线方式配置Maven库。

3. 需要的知识点
Java基础开发能力。

三、能力接入准备
实现HarmonyOS应用开发,需要完成以下准备工作:


·应用开发
具体操作,请按照《DevEco Studio使用指南》中详细说明来完成。
提示:需要通过注册成开发者才能完成集成准备中的操作。

四、代码编写

1. 写分布式文件


public void copyVideoFile() {
    FileOutputStream outFile = null;
    FileInputStream inFile = null;
    RawFileDescriptor rfd = null;
    try {
        // 读取视频文件在hap包的起始位置和视频文件大小
        rfd = getResourceManager().getRawFileEntry("resources/rawfile/video_test.mp4").openRawFileDescriptor();
        long start = rfd.getStartPosition();
        inFile = new FileInputStream(rfd.getFileDescriptor());
        inFile.skip(start);

        // 设置分布式视频文件保存的路径
        File distDir = getDistributedDir();
        String filePath = distDir + File.separator + "video_test.mp4";
        outFile = new FileOutputStream(filePath);

        byte[] buffer = new byte[FILE_BUFFER_SIZE];
        int len;
        while ((len = inFile.read(buffer, 0, FILE_BUFFER_SIZE)) > 0) {
            outFile.write(buffer, 0, len);
        }
    } catch (IOException ioException) {
        HiLog.error(TAG, "IOException");
    } finally {
        if (inFile != null) {
            try {
                inFile.close();
            } catch (IOException ioException) {
                HiLog.error(TAG, "IOException");
            }
        }
        if (outFile != null) {
            try {
                outFile.close();
            } catch (IOException ioException) {
                HiLog.error(TAG, "IOException");
            }
        }
        if (rfd != null) {
            try {
                rfd.close();
            } catch (IOException ioException) {
                HiLog.error(TAG, "IOException");
            }
        }
    }
}


2. 跨设备启动远端元能力


Intent intent = new Intent();
intent.setParam("playTime", mVideoContainer.getPlayTime());
Operation operation = new Intent.OperationBuilder()
    .withDeviceId(info.getDeviceId())
    .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
    .withBundleName("com.huawei.codelab")
    .withAbilityName("com.huawei.codelab.MainAbility")
    .build();
intent.setOperation(operation);

try {
    List<AbilityInfo> abilityInfos = getBundleManager().queryAbilityByIntent(intent, 0, 0);
    if (abilityInfos != null && !abilityInfos.isEmpty()) {
        startAbility(intent);
    }
} catch (RemoteException e) {
    HiLog.error(TAG, "RemoteException");
}


3. 通过intent获取参数


if (intent != null) {
    if (intent.getParams() != null
        && intent.getParams().keySet().contains("playTime")) {
        mVideoContainer.setCurrentTime((int) intent.getParams().getParam("playTime"));
        isFromOtherClient = true;
    }
}


4. 播放分布式视频文件


try {
    File distDir = getDistributedDir();
    String filePath = distDir + File.separator + "video_test.mp4";
    File videoFile = new File(filePath);
    if (videoFile.exists()) {
        FileInputStream inputStream = new FileInputStream(filePath);
        mVideoContainer.playAssets(new Source(inputStream.getFD()), true, surfaceOps);
    } else {
        mHandler.sendEvent(MESSAGE_UPDATE_PLAY_VIDEO, DELAY_TIME);
    }
} catch (IOException ioException) {
    HiLog.error(TAG, "IOException");
}


提示:以上代码仅demo演示参考使用。

五、编译运行

通过hdc连接大屏设备
先查看智慧屏IP:大屏设置->"网络与连接"->"网络"->"无线网络"

在cmd或者IDE的Terminal输入命令:


hdc tconn 192.168.xxx.xxx:5555

运行


六、恭喜你
干得好,你已经成功完成了HarmonyOS应用开发E2E体验,学到了:

如何创建一个HarmonyOS Demo Project
如何构建一个Hap并且将其部署到真机
在HarmonyOS上如何使用HarmonyOS的视频播放的能力


©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
头像

Tiechui.Wang

我还没有写个人简介......

1091

帖子

0

提问

893

粉丝

关注
热门推荐
地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 商务内容合作QQ:2291221 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除
京ICP备:2022009079号-2
京公网安备:11010502051901号
ICP证:京B2-20230255