Web加载视频播放出现异常问题时,可按如下步骤进行定位排查:
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct DownloadVideoDemo {
controller: webview.WebviewController = new webview.WebviewController();
delegate: webview.WebDownloadDelegate = new webview.WebDownloadDelegate();
cacheDir: string = this.getUIContext().getHostContext()!.cacheDir;
build() {
Column() {
Web({
src: 'xxx.com/xxx.mp4', // 需要替换为那种打开后自动下载的链接
controller: this.controller
})
.fileAccess(true)
.domStorageAccess(true)
.geolocationAccess(false)
.onControllerAttached(() => {
try {
this.delegate.onBeforeDownload((webDownloadItem: webview.WebDownloadItem) => {
// 传入本地沙箱路径并开始下载
webDownloadItem.start(this.cacheDir + '/' + webDownloadItem.getSuggestedFileName());
});
this.delegate.onDownloadUpdated((webDownloadItem: webview.WebDownloadItem) => {
// 下载任务进度和速度监测处理
console.info(`download update guid: ${webDownloadItem.getGuid()}`);
});
this.delegate.onDownloadFailed((webDownloadItem: webview.WebDownloadItem) => {
// 下载任务失败处理
console.error(`download failed guid: ${webDownloadItem.getGuid()}`);
});
this.delegate.onDownloadFinish((webDownloadItem: webview.WebDownloadItem) => {
// 下载成功通过Web重新加载本地文件打开预览
this.controller.loadUrl(`file://${this.cacheDir}/` + webDownloadItem.getSuggestedFileName());
});
this.controller.setDownloadDelegate(this.delegate);
} catch (error) {
// 异常处理
console.error(
`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
});
};
}
} import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct SandboxVideoDemo {
controller: webview.WebviewController = new webview.WebviewController();
url: string | Resource = this.getUIContext().getHostContext()!.filesDir + '/test.mp4';
build() {
Column() {
Web({
src: this.url,
controller: this.controller
})
.domStorageAccess(true)
.fileAccess(true)
.geolocationAccess(false);
};
}
} import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct LocalH5Demo {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Web({
src: $rawfile('video.html'),
controller: this.controller
})
.geolocationAccess(false)
.fileAccess(true);
};
}
} <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width">
</head>
<body>
<video id="myVideo" src="resource://rawfile/file/video.mp4" autoplay controls muted >
</video>
<script>
</script>
</body>
</html>