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