效果如下
代码
@Entry
@Component
struct PageVideo {
@State videoSrc: Resource = $rawfile('AndroidVideo.mp4')
@State previewUri: Resource = $rawfile('6_20231218171028A068.jpg')
@State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X
@State isAutoPlay: boolean = false
@State showControls: boolean = true
controller: VideoController = new VideoController()
build() {
Column() {
Video({
src: this.videoSrc,
previewUri: this.previewUri,
currentProgressRate: this.curRate,
controller: this.controller
}).width('100%').height(600)
.autoPlay(this.isAutoPlay)
.controls(this.showControls)
.onStart(() => {
console.info('onStart')
})
.onPause(() => {
console.info('onPause')
})
.onFinish(() => {
console.info('onFinish')
})
.onError(() => {
console.info('onError')
})
.onPrepared((e) => {
console.info('onPrepared is ' + e.duration)
})
.onSeeking((e) => {
console.info('onSeeking is ' + e.time)
})
.onSeeked((e) => {
console.info('onSeeked is ' + e.time)
})
.onUpdate((e) => {
console.info('onUpdate is ' + e.time)
})
Row() {
Button('切换来源').onClick(() => {
if(this.videoSrc != $rawfile('3773192759.mp3')) {
this.previewUri = $rawfile('cover1.png')
this.videoSrc = $rawfile('3773192759.mp3')
} else{
this.previewUri = $rawfile('6_20231218171028A068.jpg')
this.videoSrc = $rawfile('AndroidVideo.mp4')
}
}).margin(5)
Button('显示导航').onClick(() => {
this.showControls = !this.showControls
}).margin(5)
}
Row() {
Button('开始').onClick(() => {
this.controller.start()
}).margin(5)
Button('暂停').onClick(() => {
this.controller.pause()
}).margin(5)
Button('停止').onClick(() => {
this.controller.stop()
}).margin(5)
Button('回到10秒播放').onClick(() => {
this.controller.setCurrentTime(10, SeekMode.Accurate)
}).margin(5)
}
Row() {
Button('播放速度 0.75').onClick(() => {
this.curRate = PlaybackSpeed.Speed_Forward_0_75_X
}).margin(5)
Button('播放速度 1').onClick(() => {
this.curRate = PlaybackSpeed.Speed_Forward_1_00_X
}).margin(5)
Button('播放速度 2').onClick(() => {
this.curRate = PlaybackSpeed.Speed_Forward_2_00_X
}).margin(5)
}
}
}
}