鸿蒙(HarmonyOS)应用开发—— video组件实操

news/2024/7/21 12:18:13 标签: harmonyos, 华为

video 组件

harmonyOS 系统提供了基础的video。下面就直接上代码

原始video

新建项目 customVideo项目

在这里插入图片描述

本地视频

网络地址,就不用说明,只需要把地址换成网络地址即可
在resource 文件夹下的rawfile文件夹下 添加视频
在这里插入图片描述

在index.ets

在这里插入图片描述

  Video({src:$rawfile("videoTest.mp4")})
        .height("30%")

呈现的效果

在这里插入图片描述

新闻类/课程类视频

引入背景图片

在resource 文件下的base——media 中添加进背景图
在这里插入图片描述
添加背景图属性

    Column(){
      Flex() {
        Video({src:$rawfile("videoTest.mp4"),previewUri:$r('app.media.preview')})
          .height("30%")


      }
      Flex(){
        Text("sss")
      }
    }

呈现效果

在这里插入图片描述

自定义播放栏

新建VideoSlider.ets

@Component
export struct VideoSlider {
  @Consume isOpacity: boolean;
  private controller: VideoController = new VideoController();
  @Consume currentStringTime: string;
  @Consume currentTime: number;
  @Consume durationTime: number;
  @Consume durationStringTime: string;
  @Consume isPlay: boolean;
  @Consume flag: boolean;
  @Consume isLoading: boolean;
  @Consume progressVal: number;

  build() {
    Row({ space: MARGIN_FONT_SIZE.FIRST_MARGIN }) {
      Image(this.isPlay ? $r('app.media.ic_pause') : $r('app.media.ic_play'))
        .width(IMAGE_SIZE)
        .height(IMAGE_SIZE)
        .margin({ left: MARGIN_FONT_SIZE.FIRST_MARGIN })
        .onClick(() => {
          this.iconOnclick();
        })
      Text(this.currentStringTime)
        .fontSize(MARGIN_FONT_SIZE.SECOND_MARGIN)
        .fontColor(Color.White)
        .margin({ left: MARGIN_FONT_SIZE.FIRST_MARGIN })
      Slider({
        value: this.currentTime,
        min: 0,
        max: this.durationTime,
        step: 1,
        style: SliderStyle.OutSet
      })
        .blockColor($r('app.color.white'))
        .width(STRING_PERCENT.SLIDER_WITH)
        .trackColor(Color.Gray)
        .selectedColor($r("app.color.white"))
        .showSteps(true)
        .showTips(true)
        .trackThickness(this.isOpacity ? SMALL_TRACK_THICK_NESS : BIG_TRACK_THICK_NESS)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.sliderOnchange(value, mode);
        })
      Text(this.durationStringTime)
        .fontSize(MARGIN_FONT_SIZE.SECOND_MARGIN)
        .margin({ right: MARGIN_FONT_SIZE.FIRST_MARGIN })
        .fontColor(Color.White)
    }
    .opacity(this.isOpacity ? DEFAULT_OPACITY : 1)
    .width(ALL_PERCENT)
    .alignItems(VerticalAlign.Center)
    .justifyContent(FlexAlign.Center)
  }

  /**
   * icon onclick callback
   */
  iconOnclick() {
    if (this.isPlay === true) {
      this.controller.pause()
      this.isPlay = false;
      this.isOpacity = false;
      return;
    }
    if (this.flag === true) {
      this.controller.start();
      this.isPlay = true;
      this.isOpacity = true;
    } else {
      this.isLoading = true;
      // The video loading is not complete. The loading action is displayed.
      let intervalLoading = setInterval(() => {
        if (this.progressVal >= STACK_STYLE.PROGRESS_TOTAL) {
          this.progressVal = 0;
        } else {
          this.progressVal += STACK_STYLE.PROGRESS_STEP;
        }
      }, STACK_STYLE.MILLI_SECONDS)
      // The scheduled task determines whether the video loading is complete.
      let intervalFlag = setInterval(() => {
        if (this.flag === true) {
          this.controller.start();
          this.isPlay = true;
          this.isOpacity = true;
          this.isLoading = false;
          clearInterval(intervalFlag);
          clearInterval(intervalLoading);
        }
      }, STACK_STYLE.MILLI_SECONDS);
    }
  }

  /**
   * video slider component onchange callback
   */
  sliderOnchange(value: number, mode: SliderChangeMode) {
    this.currentTime = Number.parseInt(value.toString());
    this.controller.setCurrentTime(Number.parseInt(value.toString()), SeekMode.Accurate);
    if (mode === SliderChangeMode.Begin || mode === SliderChangeMode.Moving) {
      this.isOpacity = false;
    }
    if (mode === SliderChangeMode.End) {
      this.isOpacity = true;
    }
  }
}
 private controller : VideoController;

Flex() {
        Video({src:$rawfile("videoTest.mp4"),previewUri:$r('app.media.preview')})
          .height("30%")
          .controls(false)
        VideoSlider({ controller: this.controller })

      }

呈现效果

在这里插入图片描述


http://www.niftyadmin.cn/n/5275134.html

相关文章

【设计类】

一、设计并实现几个类,用于打开并读取不同类型的文件内容。要求如下: 一种类型对应一个类。根据文件名后缀区分文件类型。需要支持 .ini, .xml, .json。这些类需要支持以下几个方法: bool open() - 打开文件 std::string file() const - 返回…

Ubuntu中基础命令使用

前言 以下指令测试来自于Ubuntu18.04 如果有说的不对的,欢迎指正与补充 以下指令为我学习嵌入式开发中使用过最多的指令 目录 前言 1 ls 首先我们进入到Linux操作系统中 2 touch创建一个文件 3 pwd查看当前路径 4 创建目录 5 删除文件 6 cd 目录跳转 0…

PIC单片机项目(5)——基于PIC16F877A的多功能防盗门

1.功能设计 本次设计的功能如下:如果红外对管检测到有人经过,LCD1602可以显示,我设计的是显示字符串“someone”。 如果有人强行破门,FSR402压力传感器会检测到压力过大,然后触发蜂鸣器报警,LCD1602也显示“…

从源码到实践:深入了解鸿鹄电子招投标系统与电子招投标

在数字化采购领域,企业需要一个高效、透明和规范的管理系统。通过采用Spring Cloud、Spring Boot2、Mybatis等先进技术,我们打造了全过程数字化采购管理平台。该平台具备内外协同的能力,通过待办消息、招标公告、中标公告和信息发布等功能模块…

玩转Docker(七):Docker Compose

文章目录 一、什么是Docker Compose二、一个完整的例子 一、什么是Docker Compose Docker Compose是一个用于定义和运行多个Docker容器应用的工具。它使用YAML文件来配置应用的服务、网络、卷等,通过一个简单的命令即可启动、停止和管理整个应用的容器。Docker Com…

日志切割,如果mv elink.log 20231220/elink.log.2023120 ,filebeat不会继续采集

1、 [rootzz filebeat-7]# ./filebeat -c /root/filebeat-7/prospectors.d/elink.yaml 2、 [rootzz prospectors.d]# cat elink.yaml filebeat.inputs: - type: log enabled: true paths: - /var/log/elink.log fields_under_root: true fields: source: 192.…

凸优化问题求解

这里写目录标题 1. 线性规划基本定理2.单纯形法2.1 转轴运算 3. 内点法3.1 线性规划的内点法 1. 线性规划基本定理 首先我们指出,线性规划均可等价地化成如下标准形式 { min ⁡ c T x , s . t A x b , x ⪰ 0 , \begin{align}\begin{cases}\min~c^Tx,\\\mathrm{s.…

人工智能超分辨率重建:揭秘图像的高清奇迹

导言 人工智能超分辨率重建技术,作为图像处理领域的一项重要创新,旨在通过智能算法提升图像的分辨率,带来更为清晰和细致的视觉体验。本文将深入研究人工智能在超分辨率重建方面的原理、应用以及技术挑战。 1. 超分辨率重建的基本原理 …