Video(value: {src?: string | Resource, currentProgressRate?: number | string |PlaybackSpeed, previewUri?: string |PixelMap | Resource, controller?: VideoController})
@Component
export struct VideoPlayer {
private source: string | Resource;
private previewUris: Resource = $r('app.media.preview');
build() {
Column() {
Video({
src: this.source,
previewUri: this.previewUris
})
.width('100%')
.height('100%')
.objectFit(ImageFit.Contain)
}
}
}
注意:
- 使用网络播放路径时,需要注意的是需要在module.json5文件中申请网络权限
- 本地视频地址可以使用媒体库管理模块medialibrary来查询公共媒体库中的视频文件
VideoPlayer
import { Event } from '@ohos.worker';
import Prompt from '@system.prompt';
import { ALL_PERCENT, COMMON_NUM_DOUBLE, COMMON_NUM_MINUTE, SPLIT, STRING_PERCENT, ZERO_STR } from './Constants';
import { changeSliderTime } from './VideoControll';
import { VideoSlide } from './VideoSlide';
@Component
export struct VideoPlayer {
private source: string | Resource;
private previewUris: Resource = $r('app.media.preview');
private controller: VideoController
isPlay: boolean = false
@Provide durationTime: number = 0
@Provide durationStringTime: string = '00:00'
@Provide currentTime: number = 0
@Provide currentStringTime: string = '00:00'
build() {
Column() {
Video({
src: this.source,
previewUri: this.previewUris,
controller: this.controller
})
.width(ALL_PERCENT)
.height(STRING_PERCENT.NINETY_PERCENT)
.objectFit(ImageFit.Contain)
.controls(false)
.onPrepared((event) => {
this.prepared.call(this, event?.duration)
})
.onUpdate((event) => {
if (event) {
this.currentTime = event.time
this.currentStringTime = changeSliderTime(this.currentTime)
}
})
.onFinish(() => {
//视频播放完成回调
this.finish.call(this)
})
.onError(() => {
//视频播放出错回调
Prompt.showToast({ duration: 5000, message: '错误提示' })
})
//自定义视频播放控制器组件
VideoSlide({ controller: this.controller })
}
}
prepared(duration: number) {
this.durationTime = duration;
let second: number = duration % COMMON_NUM_MINUTE;
let min: number = Number.parseInt((duration / COMMON_NUM_MINUTE).toString());
let head = min < COMMON_NUM_DOUBLE ? `${ZERO_STR}${min}` : min;
let end = second < COMMON_NUM_DOUBLE ? `${ZERO_STR}${second}` : second;
this.durationStringTime = `${head}${SPLIT}${end}`;
}
finish() {
this.isPlay = false
}
}
ViewSlider
import { ALL_PERCENT } from './Constants'
@Component
export struct VideoSlide {
private controller: VideoController
private isPlay: boolean = false
@Consume durationTime: number
@Consume durationStringTime: string
@Consume currentTime: number
@Consume currentStringTime: string
build() {
Row({ space: 12 }) {
//视频播放/暂停控制
Image(this.isPlay
? $r('app.media.ic_pause')
: $r('app.media.ic_play'))
.onClick(() => {
this.iconOnclick()
})
.width(50)
.height(50)
//显示当前播放时间
Text(this.currentStringTime)
.fontColor(Color.White)
//控制和播放进度
Slider({ value: this.currentTime, min: 0, max: this.durationTime, step: 1 })
.width(180)
.onChange((value: number, mode: SliderChangeMode) => {
this.slideOnChange.call(this, value, mode)
})
//显示视频时长
Text(this.durationStringTime)
.fontColor(Color.White)
}.backgroundColor(Color.Green)
.width(ALL_PERCENT)
}
iconOnclick() {
if (this.isPlay) {
this.controller.pause()
this.isPlay = false
} else {
this.controller.start()
this.isPlay = true
}
}
slideOnChange(value: number, mode: SliderChangeMode) {
this.currentTime = parseInt(value.toString())
this.controller.setCurrentTime(this.currentTime, SeekMode.Accurate)
}
}