• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/*
3 * Copyright (c) 2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18
19import audio from '@ohos.multimedia.audio'
20import mediaLibrary from '@ohos.multimedia.mediaLibrary'
21import { myMedia } from '../model/myMedia'
22import { getTimeString } from '../model/TimeTools'
23import { isInformationDialog } from '../common/IsInformationDialog'
24
25let audioManager = audio.getAudioManager()
26
27@Component
28export struct BroadcastControl {
29  @Link isLoop: boolean
30  @Link myMedia: myMedia
31  @Link isOpacity: number
32  @Link isSurface: boolean
33  @Link isPlaying: boolean
34  @Link currentTime: number
35  @Link btnEnabled: boolean
36  @Link isXComponent: boolean
37  @Link itemData: mediaLibrary.FileAsset
38  @Link @Watch('onChangeIsShowMenu') isShowMenu: boolean
39  @State isVolume: boolean = false
40  @State mediaVolumeValue: number = -1
41  @State isInformation: boolean = false
42  @State isInformationObj: mediaLibrary.FileAsset = undefined
43  private timer: number = undefined
44  private timerOut: number = undefined
45  private scroller: Scroller = new Scroller()
46  private mXComponentController: XComponentController
47  private dialogController: CustomDialogController = null
48  private context: any
49  @State selectText: string = ''
50  clearVideoPlayer() {
51    this.currentTime = 0
52    clearInterval(this.timer)
53    this.myMedia.stop()
54    this.myMedia.release()
55  }
56
57  onChangeIsShowMenu() {
58    if (!this.isShowMenu) {
59      this.isInformation = false
60      this.isVolume = false
61    }
62  }
63
64  async aboutToAppear() {
65    this.mediaVolumeValue = await audioManager.getVolume(audio.AudioVolumeType.MEDIA)
66    this.selectText = await this.convertResourceToString($r('app.string.phoneMain_setSpeed'))
67  }
68  async convertResourceToString(resource: Resource){
69    return await this.context.resourceManager.getString(resource)
70  }
71  build() {
72    Stack({ alignContent: Alignment.Center }) {
73      if (this.isShowMenu) {
74        Column() {
75          Row() {
76            Text(this.myMedia.fileName ? this.myMedia.fileName : $r('app.string.phoneMain_title'))
77              .fontColor(Color.White)
78              .fontSize(28)
79              .margin({ left: 12, top: 8 })
80              .fontFamily('HarmonyHeiTi-Medium')
81          }
82          .width('100%')
83          .height('8%')
84          .constraintSize({ minHeight: 70 })
85          .padding({ left: 15 })
86
87          Blank()
88          Row() {
89            Text(getTimeString(this.currentTime))
90              .fontSize(16)
91              .fontColor('#FFFFFF')
92              .fontWeight(500)
93              .opacity(0.6)
94            Slider({
95              value: this.myMedia ? Math.round(this.currentTime / this.myMedia.totalDuration * 100) : 0
96            })
97              .selectedColor(Color.White)
98              .width('90%')
99              .trackColor(Color.Gray)
100              .onChange((value: number) => {
101                this.currentTime = this.myMedia.totalDuration * value / 100
102                this.myMedia.seek(this.currentTime)
103              })
104            Text(this.myMedia ? getTimeString(this.myMedia.totalDuration) : '00:00')
105              .fontSize(16)
106              .fontColor('#FFFFFF')
107              .fontWeight(500)
108              .opacity(0.6)
109          }
110          .width('98%').height(25)
111
112          Flex({
113            justifyContent: FlexAlign.SpaceBetween,
114            alignItems: ItemAlign.Center
115          }) {
116            Row() {
117              Image($r('app.media.icon_Previous'))
118                .width(24)
119                .height(24)
120                .enabled(this.btnEnabled)
121                .opacity(this.isOpacity)
122                .onClick(() => {
123                  this.isPlaying = true
124                  this.isSurface = !this.isSurface
125                  this.clearVideoPlayer()
126                  this.timer = setInterval(() => {
127                    this.currentTime = this.myMedia.getCurrentTime()
128                  }, 1000)
129                  this.timerOut = setTimeout(() => {
130                    let surfaceId = this.mXComponentController.getXComponentSurfaceId()
131                    this.myMedia.prev(surfaceId)
132                  }, 100)
133                })
134              Image(this.isPlaying ? $r('app.media.icon_play') : $r('app.media.icon_pause'))
135                .width(24)
136                .height(24)
137                .opacity(this.isOpacity)
138                .onClick(() => {
139                  this.isPlaying = !this.isPlaying
140                  clearInterval(this.timer)
141                  this.timer = setInterval(() => {
142                    this.currentTime = this.myMedia.getCurrentTime()
143                  }, 1000)
144                })
145              Image($r('app.media.icon_next'))
146                .width(24)
147                .height(24)
148                .enabled(this.btnEnabled)
149                .opacity(this.isOpacity)
150                .onClick(() => {
151                  this.isPlaying = true
152                  this.clearVideoPlayer()
153                  this.timer = setInterval(() => {
154                    this.currentTime = this.myMedia.getCurrentTime()
155                  }, 1000)
156                  this.isSurface = !this.isSurface
157                  this.timerOut = setTimeout(() => {
158                    let surfaceId = this.mXComponentController.getXComponentSurfaceId()
159                    this.myMedia.next(surfaceId)
160                  }, 100)
161                })
162              Image(this.isLoop ? $r('app.media.icon_single') : $r('app.media.icon_repeatplay'))
163                .width(24)
164                .height(24)
165                .opacity(this.isOpacity)
166                .onClick(() => {
167                  this.isLoop = !this.isLoop
168                  this.myMedia.getPlayMode(this.isLoop)
169                })
170              Select([
171                { value: '0.75' },
172                { value: '1.00' },
173                { value: '1.25' },
174                { value: '1.75' },
175                { value: '2.00' },
176              ])
177                .value(this.selectText)
178                .font({ size: 18 })
179                .fontColor(Color.White)
180                .selectedOptionFont({ size: 18 })
181                .optionFontColor(Color.Black)
182                .opacity(this.itemData ? (this.itemData.mediaType == mediaLibrary.MediaType.AUDIO ? 0 : 1) : 0.4)
183                .enabled(this.itemData ? (this.itemData.mediaType == mediaLibrary.MediaType.AUDIO ? false : true) : false)
184                .optionFont({ size: 18 })
185                .onSelect((index: number) => {
186                  this.myMedia.setSpeed(index)
187                })
188
189            }.width('40%').justifyContent(FlexAlign.SpaceBetween)
190
191            Row() {
192              Image($r('app.media.icon_info'))
193                .width(24)
194                .height(24)
195                .opacity(this.isOpacity)
196                .onClick(() => {
197                  this.isInformation = !this.isInformation
198                  if (this.isInformation) {
199                    if (this.myMedia.getResourceAddress()) {
200                      this.dialogController = new CustomDialogController({
201                        builder: isInformationDialog({ myMedia: $myMedia , context: this.context }),
202                        autoCancel: true,
203                        cancel: () => {
204                          this.myMedia.getPlay()
205                          this.isPlaying = true
206                        },
207                        customStyle: true,
208                        offset: {
209                          dx: 195,
210                          dy: -7
211                        }
212                      })
213                      this.dialogController.open()
214                    }
215                  }
216                })
217
218              Image(this.isVolume ? $r('app.media.icon_isvolume') : $r('app.media.icon_volume'))
219                .width(24)
220                .height(24)
221                .onClick(() => {
222                  this.isVolume = !this.isVolume
223                })
224
225            }
226            .width('10%')
227            .justifyContent(FlexAlign.SpaceBetween)
228            .margin({ right: 10 })
229          }
230          .margin({ top: 10, bottom: 10 })
231          .height(40)
232          .width('98%')
233        }
234        .height('100%')
235        .width('98%')
236        .zIndex(2)
237      }
238      if (this.isXComponent) {
239        if (this.isSurface) {
240          Row() {
241            XComponent({
242              id: '',
243              type: 'surface',
244              controller: this.mXComponentController
245            })
246              .width('100%')
247          }
248          .height('85%')
249          .width('100%')
250          .justifyContent(FlexAlign.Center)
251          .margin({ top: -45 })
252        } else {
253          Row() {
254            XComponent({
255              id: '',
256              type: 'surface',
257              controller: this.mXComponentController
258            })
259              .width('100%')
260          }
261          .height('85%')
262          .width('100%')
263          .justifyContent(FlexAlign.Center)
264          .margin({ top: -45 })
265        }
266      }
267      if (this.isVolume) {
268        Flex({
269          justifyContent: FlexAlign.SpaceEvenly,
270          alignItems: ItemAlign.Center,
271          direction: FlexDirection.Column
272        }) {
273          Slider({
274            value: this.mediaVolumeValue,
275            min: 0,
276            max: 15,
277            step: 1,
278            direction: Axis.Vertical,
279            reverse: true
280          })
281            .selectedColor('#5291ff')
282            .height('94%')
283            .opacity(1)
284            .trackColor(Color.Gray)
285            .onChange((value: number) => {
286              this.mediaVolumeValue = value
287              audioManager.setVolume(audio.AudioVolumeType.MEDIA, value, (err) => {
288              })
289            })
290        }
291        .position({ x: '93%', y: '43%' })
292        .width('5%')
293        .height('50%')
294        .backgroundColor('rgba(0,0,0,0.6)')
295        .borderRadius(25)
296        .zIndex(5)
297        .onClick(() => {
298          this.isVolume = !this.isVolume
299        })
300      }
301    }
302    .width('77%')
303    .height('100%')
304    .backgroundColor(Color.Black)
305    .onClick(() => {
306      this.isShowMenu = !this.isShowMenu
307    })
308  }
309}