根据问题定位可知,问题现象的根因在于没有手动为视频设置预览图片,因此可以参考以下代码进行修复:
代码示例如下:
Video({
src: $r('app.media.videoTest'), // $r('app.media.videoTest')仅作展示,使用时请开发者自行替换
previewUri: $r('app.media.example'), // $r('app.media.example')仅作展示,使用时请开发者自行替换
controller: this.controller
})
.height('50%'); 在视频播放前显示预览图片,建议将视频的第一帧作为预览图片。
部分场景,如上下滑动刷新不同视频,且要求视频自动播放的时候,可以考虑使用这个方案。
代码示例如下:
Video({
src: $r('app.media.videoTest'), // $r('app.media.videoTest')仅作展示,使用时请开发者自行替换
controller: this.controller
})
.visibility(this.isVisible)
.autoPlay(true) // 设置自动播放
.loop(true)
.controls(true)
.width('100%')
.height('50%')
.onStart(() => {
setTimeout(() => { // 使用setTimeout设置延迟跳过黑屏阶段
this.controller.setCurrentTime(1, SeekMode.PreviousKeyframe);
this.isVisible = Visibility.Visible;
}, 150);
}); 完整示例代码参考如下:
@Entry
@Component
struct video {
controller: VideoController = new VideoController();
@State isVisible: Visibility = Visibility.None;
build() {
Column() {
Video({
src: $r('app.media.videoTest'), // $r('app.media.videoTest')仅作展示,使用时请开发者自行替换
previewUri: $r('app.media.example'), // $r('app.media.example')仅作展示,使用时请开发者自行替换
controller: this.controller
})
.height('50%');
Video({
src: $r('app.media.videoTest'), // $r('app.media.videoTest')仅作展示,使用时请开发者自行替换
controller: this.controller
})
.visibility(this.isVisible)
.autoPlay(true) // 设置自动播放
.loop(true)
.controls(true)
.width('100%')
.height('50%')
.onStart(() => {
setTimeout(() => { // 使用setTimeout设置延迟跳过黑屏阶段
this.controller.setCurrentTime(1, SeekMode.PreviousKeyframe);
this.isVisible = Visibility.Visible;
}, 150);
});
};
}
}