VideoPlayPage.ets
5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { DateFormatUtil, WDAliPlayerController, WDPlayerController, WDPlayerRenderLiveView } from 'wdPlayer/Index';
import router from '@ohos.router';
import { StringUtils } from 'wdKit/Index';
import { Action } from 'wdBean';
const TAG = 'VideoPlayPage';
@Entry
@Component
export struct VideoPlayPage {
//是否处于播放状态中
@State isPlayStatus: boolean = true
playerController: WDAliPlayerController = new WDAliPlayerController();
//视频地址
@State videoUrl: string = ''
//封面图
@State videoCoverUrl: string = ''
@State currentTime: string = ''
@State totalTime: string = ''
@State progressVal: number = 0;
@Provide topSafeHeight: number = AppStorage.get<number>('topSafeHeight') || 0
@State isPlayEnd: boolean = false
aboutToAppear(): void {
let par: Action = router.getParams() as Action
let params = par?.params
this.videoUrl = params?.videoUrl ? params?.videoUrl : ''
this.videoCoverUrl = params?.videoCoverUrl ? params?.videoCoverUrl : ''
//播放进度监听
this.playerController.onTimeUpdate = (position: number, duration: number) => {
this.currentTime = DateFormatUtil.secondToTime(Math.floor(position / 1000));
this.totalTime = DateFormatUtil.secondToTime(Math.floor(duration / 1000));
this.progressVal = Math.floor(position * 100 / duration);
}
this.playerController.onCanplay = () => {
this.playerController.play()
}
this.playerController.onStatusChange = (status: number) =>{
this.isPlayEnd = status === 5 ? true:false
}
}
build() {
Stack() {
WDPlayerRenderLiveView({
playerController: this.playerController,
onLoad: async () => {
this.playerController.firstPlay(this.videoUrl)
}
})
.height('100%')
.width('100%')
.visibility(StringUtils.isEmpty(this.videoUrl) ? Visibility.None : Visibility.Visible)
Image(this.videoCoverUrl)
.objectFit(ImageFit.Contain)
.visibility(StringUtils.isEmpty(this.videoUrl) ? Visibility.Visible : Visibility.None)
Column() {
this.getTopUIComponent()
Stack()
.layoutWeight(1)
Column(){
Image($r('app.media.icon_live_player_replay')).width(40).height(40)
.onClick(() => {
this.progressVal = 0
this.isPlayEnd = false
this.playerController.firstPlay(this.videoUrl)
})
Text('重播').fontColor(Color.White).fontSize(14)
}
.justifyContent(FlexAlign.Center)
.backgroundColor("#80000000")
.width('100%')
.height('60%')
.visibility(this.isPlayEnd ? Visibility.Visible : Visibility.None)
Stack()
.layoutWeight(1)
this.getBottomUIComponent()
}
}
.width('100%')
}
@Builder
getTopUIComponent() {
Column() {
Row() {
Image($r('app.media.icon_arrow_left_white'))
.width(24)
.height(24)
.aspectRatio(1)
.interpolation(ImageInterpolation.High)
.margin({
right: 10
})
.onClick(() => {
router.back()
})
}
.width('100%')
.alignItems(VerticalAlign.Center)
.margin({ top: `${this.topSafeHeight}px` })
}.width('100%')
.alignItems(HorizontalAlign.Start)
}
@Builder
getBottomUIComponent() {
Column(){
Row() {
this.playOrPauseBtn()
Text(this.currentTime)
.fontColor(Color.White)
.fontWeight(600)
.fontSize('12fp')
.margin({
left: 16
})
this.playProgressView()
Text(this.totalTime)
.fontColor(Color.White)
.fontWeight(600)
.fontSize('12fp')
.margin({
right: 16
})
}
.alignItems(VerticalAlign.Center)
.height(40)
.width('100%')
.visibility(this.isPlayEnd ? Visibility.None:Visibility.Visible)
.padding({
left: 10,
right: 10,
})
}
.align(Alignment.Top)
.height(64)
.width('100%')
}
@Builder
playOrPauseBtn() {
//暂停、播放
Image(this.isPlayStatus ? $r('app.media.icon_live_player_pause') : $r('app.media.player_play_ic'))
.width(24)
.height(24)
.onClick(() => {
if (this.isPlayStatus) {
this.isPlayStatus = false
this.playerController.pause()
} else {
this.isPlayStatus = true
this.playerController.play()
}
})
}
@Builder
playProgressView() {
Slider({
value: this.progressVal,
step: 1,
style: SliderStyle.OutSet
})
.blockSize({
width: 18,
height: 12
})
.blockStyle({
type: SliderBlockType.IMAGE,
image: $r('app.media.ic_player_block')
})
.blockColor(Color.White)
.trackColor('#4DFFFFFF')
.selectedColor('#FFED2800')
.height(14)
.trackThickness(1)
.layoutWeight(1)
.margin({
left: 8,
right: 8
})
.onChange((value: number, mode: SliderChangeMode) => {
this.playerController?.setSeekTime(value, mode);
})
}
onPageHide(): void {
this.playerController?.pause()
this.playerController?.stop()
}
}