1. 介绍
本篇CodeLab将实现的内容
HarmonyOS是面向全场景多终端的分布式操作系统,使得应用程序的开发打破了智能终端互通的性能和数据壁垒,业务逻辑原子化开发,适配多端。通过一个简单应用开发,体验HarmonyOS的视频播放能力
您将建立什么
在这个CodeLab中,你将创建Demo Project,并将Demo编译成HAP,此示例应用程序展示了如何播放视频。
您将会学到什么
- 如何创建一个HarmonyOS Demo Project
- 如何构建一个HAP并且将其部署到智慧屏真机
- 通过此示例应用体验如何播放本地视频
2. 您需要什么
硬件要求
- 操作系统:Windows10 64位
- 内存:8G及以上
- 硬盘:100G及以上
- 分辨率:1280*800及以上
软件要求
- 安装DevEco Studio和Node.js,详情请参考下载和安装软件
- 设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境
1.如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作
2.如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境
需要的知识点
- Java基础开发能力。
3. 能力接入准备
实现HarmonyOS应用开发,需要完成以下准备工作:
- 创建TV的工程
- 准备密钥和证书请求文件
- 申请调试证书
- 应用开发
具体操作,请按照《DevEco Studio使用指南》中详细说明来完成。
4. 代码编写
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");
}
5. 编译运行
通过hdc连接大屏设备
先查看智慧屏IP:大屏设置->"网络与连接"->"网络"->"无线网络"
在cmd或者IDE的Terminal输入命令:
hdc tconn 192.168.xxx.xxx:5555
运行
6. 恭喜您
您已经成功完成了HarmonyOS应用开发体验,学到了:
- 如何创建一个HarmonyOS Demo Project
- 如何构建一个HAP并且将其部署到真机
- 在HarmonyOS上如何使用视频播放的能力