HarmonyOS 应用开发,HarmonyOS应用开发-视频播放

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 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演示参考使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值