HarmonyOS 鸿蒙Next视频播放完毕到下一页视频的问题
HarmonyOS 鸿蒙Next视频播放完毕到下一页视频的问题 【设备信息】Mate60
【API版本】Api13
【DevEco Studio版本】5.0.7.200
【问题描述】
用isPlay控制Swiper组件autoPlay,会导致视频不是从头开始播放是什么原因?
3 回复
这边有个demo,通过swiper的index和video的onFinsh控制播完完毕播放下一个视频的问题,可以参考下
//父类文件
import { router } from '@kit.ArkUI';
let videoUrl: Array<VideoArray> = []
@Entry
@Component
struct Index {
@State bottomModuleVisible: boolean = false;
@State columnId: number = 61
@State currentVideoIndex: number = 0;
@State isPlay: number = 0
@State active: number = 0;
private videoValue: Array<string> = ['/resources/rawfile/video/video1.mp4', '/resources/rawfile/video/video2.mp4',
'/resources/rawfile/video/video3.mp4',
'/resources/rawfile/video/video4.mp4']
build() {
Stack() {
Row() {
Image($r('app.media.app_icon'))
.width('8%')
.onClick(() => {
router.back();
})
Column() {
Row({ space: 15 }) {
Column() {
Row({ space: 3 }) {
Row() {}
.width(4)
.height(4)
.backgroundColor('#9f9f9f')
.borderRadius(50)
Row() {}
.width(4)
.height(4)
.backgroundColor('#9f9f9f')
.borderRadius(50)
Row() {}
.width(4)
.height(4)
.borderRadius(50)
.backgroundColor('#9f9f9f')
}
}
.onClick(() => {
this.bottomModuleVisible = !this.bottomModuleVisible;
})
}
}
.margin({
right: 20
})
.layoutWeight(1)
.alignItems(HorizontalAlign.End)
}
.width('100%')
.position({ top: 0, left: 0 })
.zIndex(1);
PlayView({ videoValue: this.videoValue, currentVideoIndex: $currentVideoIndex })
}
.width('100%')
.backgroundColor('#000000')
.height('100%')
.position({ top: '50%', left: '50%' })
.translate({ x: '-50%', y: '-50%' });
}
}
//子类文件
interface VideoArray {
videoSrc: string;
previewUri: string;
}
@Component
export struct PlayView {
@Link currentVideoIndex: number;
@Prop videoValue: Array<string>
@State private playState: number = 2; // 0 表示停止播放--stop,1 表示开始播放--start,2 表示暂停播放--pause
private videoController: VideoController = new VideoController();
@State currentTime: number = 0;
@State duration: number = 0;
@State active: number = 0;
build() {
Swiper() {
ForEach(this.videoValue, (item: string, index) => {
Stack({ alignContent: Alignment.Center | Alignment.End }) {
if (this.active === index) {
Video({
src: item, // 视频播放地址
controller: this.videoController // 视频播放器控制器
})
.onFinish(() => {
// 视频播放完毕后自动切换到下一页
this.currentVideoIndex = this.currentVideoIndex + 1
this.active = this.active + 1 //控制显示的swiper容器中显示的子组件的索引值
})
.controls(true)// .autoPlay(this.playState === 2? true : false) // 首次可见状态自动播放
.autoPlay(true)// 自动播放
.objectFit(ImageFit.Contain)// 视频窗口自适应视频大小
.loop(false)
.onClick(() => {
// 点击播放、再点击暂停播放
if (this.playState === 1) {
this.playState = 2;
this.videoController.pause();
} else if (this.playState === 2) {
this.playState = 1;
this.videoController.start();
}
})
}
}
})
}
.index(this.active)
.indicator(false)
.loop(false)
.vertical(true)
.onChange((index: number) => {
this.currentVideoIndex = index;
this.active = index;
})
}
}
更多关于HarmonyOS 鸿蒙Next视频播放完毕到下一页视频的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
会不会是和Swiper设置的缓存个数有关
在HarmonyOS(鸿蒙Next)中,视频播放完毕并自动跳转到下一页视频的需求,可以通过使用鸿蒙提供的视频播放器和页面管理机制来实现。首先,使用VideoPlayer
组件进行视频播放,并通过onEnded
事件监听视频播放结束的状态。当视频播放结束时,触发页面跳转逻辑。可以使用router
模块的push
方法进行页面导航,跳转到下一个视频页面。具体实现时,确保在页面跳转前处理好当前页面的资源释放和状态保存,以避免内存泄漏或状态不一致的问题。通过这种方式,可以实现视频播放完毕后自动跳转到下一页视频的功能。