本篇CodeLab将实现的内容

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

您将建立什么

在这个CodeLab中,你将创建Demo Project,并将Demo编译成HAP,此示例应用程序展示了如何播放视频。

您将会学到什么

硬件要求

软件要求

需要的知识点

实现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"); }

通过hdc连接大屏设备

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

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

hdc tconn 192.168.xxx.xxx:5555

运行

您已经成功完成了HarmonyOS应用开发体验,学到了:

已复制代码