From 93e84d2ebdde6626c6371fc202d08f10e56339cb Mon Sep 17 00:00:00 2001 From: lin <2320225423@qq.com> Date: Thu, 20 Jun 2024 21:23:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=81=E7=95=85=E5=88=B7=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AC=AC=E4=BA=8C=E4=B8=AA=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E5=BC=82=E5=93=8D=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/view/SampleUnitVideoSecondView.ets | 130 ++++++++++++++++++ entry/src/main/ets/view/SampleUnitView.ets | 3 +- 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 entry/src/main/ets/view/SampleUnitVideoSecondView.ets diff --git a/entry/src/main/ets/view/SampleUnitVideoSecondView.ets b/entry/src/main/ets/view/SampleUnitVideoSecondView.ets new file mode 100644 index 0000000..1b23cb2 --- /dev/null +++ b/entry/src/main/ets/view/SampleUnitVideoSecondView.ets @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { CONSTANT } from '../constant/Constant'; + +// Video height +const VIDEO_HEIGHT = 193; +// Video name +const VIDEO_NAME = 'videoTest.mp4'; +// Set time delay +const DELAY = 1000; +// XComponent width +const X_COMPONENT_WIDTH: string = '70%'; +// XComponent height +const X_COMPONENT_HEIGHT: string = '50%'; +// Animation duration +const DURATION = 30000 +// Slider size +const SLIDER_SIZE = 4 +// Slider max +const SLIDER_MAX = 100 +// Max curtime +const CUR_MAX_TIME = 100 +@Component +export default struct SampleUnitVideoSecondView { + @State currentTime: number = 0; + @State xComponentWidth: string = X_COMPONENT_WIDTH; + @State xComponentHeight: string = X_COMPONENT_HEIGHT; + @State curTime: number = 0; + controller: VideoController = new VideoController(); + durationTime: number = 0; + durationStringTime: string = '00:00'; + currentStringTime: string = '00:00'; + flag: boolean = false; + isLoading: boolean = false; + + build() { + Stack({ alignContent: Alignment.BottomStart }) { + + Video({ + src: $rawfile(VIDEO_NAME), + previewUri: $r('app.media.j'), + controller: this.controller + }) + .width(CONSTANT.FULL_SIZE) + .height(VIDEO_HEIGHT) + .controls(false) + .autoPlay(false) + .objectFit(ImageFit.Contain) + .loop(false) + .onUpdate((event) => { + if (event) { + this.currentTime = event.time; + this.currentStringTime = this.changeSliderTime(this.currentTime); + } + }) + .onPrepared((event) => { + this.prepared(event?.duration); + }) + .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => { + if (isVisible) { + setTimeout(() => { + this.controller.start(); + }, DELAY) + } + if (!isVisible) { + this.controller.stop(); + } + }) + Slider({ value: this.curTime, min: 0, max: SLIDER_MAX }) + .enabled(false) + .height(SLIDER_SIZE) + .width(this.xComponentWidth) + .trackThickness(3) + .blockColor(Color.Red) + .blockSize({ width: SLIDER_SIZE, height: SLIDER_SIZE }) + .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => { + if (isVisible && currentRatio >= 1.0) { + animateTo({ + duration: DURATION, + iterations: -1, + expectedFrameRateRange: { + expected: CONSTANT.HZ_THIRTY, + min: CONSTANT.HZ_ZERO, + max: CONSTANT.HZ_TWENTY, + }, + }, () => { + if (this.curTime >= CUR_MAX_TIME) { + this.curTime = 0; + } + for (let i = 0; i < (CUR_MAX_TIME + 1); i++) { + this.curTime += 1; + } + }) + } + }) + } + } + + prepared(duration: number) { + this.durationTime = duration; + let second: number = duration % 60; + let min: number = Number.parseInt((duration / 60).toString()); + let head = min < 10 ? `'0'${min}` : min; + let end = second < 10 ? `'0'${second}` : second; + this.durationStringTime = `${head}':'${end}`; + this.flag = true; + } + + changeSliderTime(value: number): string { + let second: number = value % 60; + let min: number = Number.parseInt((value / 60).toString()); + let head = min < 10 ? `'0'${min}` : min; + let end = second < 10 ? `'0'${second}` : second; + let nowTime = `${head}':'${end}`; + return nowTime; + } +} \ No newline at end of file diff --git a/entry/src/main/ets/view/SampleUnitView.ets b/entry/src/main/ets/view/SampleUnitView.ets index e13e851..9c04666 100644 --- a/entry/src/main/ets/view/SampleUnitView.ets +++ b/entry/src/main/ets/view/SampleUnitView.ets @@ -22,6 +22,7 @@ import SampleUnitAVPlayView from './SampleUnitAVPlayView'; import SampleUnitPicView from './SampleUnitPicView'; import SampleUnitVideoView from './SampleUnitVideoView'; import { CONSTANT } from '../constant/Constant'; +import SampleUnitVideoSecondView from './SampleUnitVideoSecondView'; // Font family const FONT_FAMILY = 'HarmonyHeiTi'; @@ -63,7 +64,7 @@ export default struct SampleUnitView { .fontFamily(FONT_FAMILY) .margin({ bottom: STATIC_MARGIN }) if (this.type === TYPE_ENUM[0]) { - SampleUnitAVPlayView({ avPlayer: this.avPlayer }); + SampleUnitVideoSecondView({ controller: this.controller }); } else if (this.type === TYPE_ENUM[1]) { SampleUnitPicView({ imageArray: this.imageArray }); } else if (this.type === TYPE_ENUM[2]) { -- Gitee