文章目录
- 视频播放
- 一、基础使用(在线、离线)
-
- 二、自定义video 视频播放
-
视频播放
一、基础使用(在线、离线)
代码示例:
@Entry
@Component
struct PayIndex {
build() {
Column() {
Tabs() {
TabContent() {
Video({
src: "http://video19.ifeng.com/video09/2024/05/23/p7199260608686989961-0-122707.mp4"
})
.width("90%")
.height("60%")
.borderRadius(15)
.autoPlay(true)
}.tabBar("在线视频")
TabContent() {
Video({
src: $rawfile("harmonyos_next_video.mp4")
})
.width("90%")
.height("60%")
.borderRadius(15)
.autoPlay(true)
}.tabBar("离线视频")
}
}
.width("100%")
.height("100%")
.justifyContent(FlexAlign.Center)
}
}
二、自定义video 视频播放
代码示例:
@Entry
@Component
struct PayIndex {
controllerVideo: VideoController = new VideoController()
@State isFullScreen: boolean = false
@State isMuted: boolean = false
@State SelectedSpeed: ResourceStr = "1"
@State currentIndex: number = 0
@State playTime: number = 0
@State duration: number = 0
build() {
Column({ space: 10}) {
Video({
src: $rawfile("harmonyos_next_video.mp4"),
controller: this.controllerVideo,
currentProgressRate: this.SelectedSpeed.toString(),
previewUri: $r("app.media.banner_pic2"),
})
.borderRadius(15)
.width("100%")
.height(500)
.objectFit(ImageFit.Contain)
.autoPlay(false)
.loop(true)
.controls(this.isFullScreen)
.muted(this.isMuted)
.onFullscreenChange((screen) => {
this.isFullScreen = screen.fullscreen
})
.onUpdate((time) => {
this.playTime = time.time
})
.onPrepared((totalTime) => {
this.duration = totalTime.duration
})
Row() {
Button("播放")
.onClick(() => {
this.controllerVideo.start()
})
Button("暂停")
.onClick(() => {
this.controllerVideo.pause()
})
Button("停止")
.onClick(() => {
this.controllerVideo.stop()
})
Button("全屏")
.onClick(() => {
this.controllerVideo.requestFullscreen(true)
})
Button("静音")
.onClick(() => {
this.isMuted = ! this.isMuted
})
Select([{value: "1"}, {value: "1.25"}, {value: "1.5"}, {value: "1.75"}, {value: "2"}])
.selected(this.currentIndex)
.value($$this.SelectedSpeed)
.onSelect((index) => {
this.currentIndex = index
})
}
.width("100%")
.justifyContent(FlexAlign.SpaceEvenly)
Row({ space: 15 }) {
Text("进度")
Slider({
value: this.playTime,
min: 0,
max: this.duration
})
.layoutWeight(1)
.onChange((value) => {
this.controllerVideo.setCurrentTime(value, SeekMode.Accurate)
})
Text(){
Span(this.playTime + "/" + this.duration)
}
}
.width("100%")
}
.width("100%")
.height("100%")
.justifyContent(FlexAlign.Center)
.padding(10)
}
}