开发TV端默认界面和视频播放界面以及手机遥控端默认界面

概述
目前家庭电视机主要通过其自带的遥控器进行操控,实现的功能较为单一。例如,当我们要在tv端搜索节目时,电视机在遥控器的操控下往往只能完成一些字母或数字的输入,而无法输入其他复杂的内容。分布式遥控器将手机的输入能力和电视遥控器的遥控能力结合为一体,从而快速便捷操控电视。
分布式遥控器的实现基于openharmony的分布式能力和rpc通信能力,ui使用ets进行开发。如下图所示,分别用两块开发板模拟tv端和手机端。
分布式组网后可以通过tv端界面的controller按钮手动拉起手机端的遥控界面,在手机端输入时会将输入的内容同步显示在tv端搜索框,点击搜索按钮会根据输入的内容搜索相关节目。
还可以通过点击方向键(上下左右)将焦点移动到我们想要的节目上,再点击播放按钮进行播放,按返回按钮返回tv端主界面。
同时还可以通过手机遥控端关机按钮同时关闭tv端和手机端界面。
实现tv端界面
在本章节中,您将学会开发tv端默认界面和tv端视频播放界面,示意图参考第一章图1和图3所示。
建立数据模型,将图片id、图片源、图片名称和视频源绑定成一个数据模型。详情代码可以查看mainability/model/picdata.ets和mainability/model/picdatamodel.ets两个文件。
实现tv端默认页面布局和样式,在mainability/pages/tvindex.ets 主界面文件中添加入口组件。页面布局代码如下:
// 入口组件
@entry
@component
struct index {
  private letters: string[] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  private source: string
  @state text: string = ''
  @state choose: number = -1
  build() {
    flex({ direction: flexdirection.column }) {
      textinput({text: this.text, placeholder: 'search' })
        .onchange((value: string) => {
          this.text = value
        })
      row({space: 30}) {
        text('clear')
          .fontsize(16)
          .backgroundcolor('#abb0ba')
          .textalign(textalign.center)
          .onclick(() => {
            this.text = ''
          })
          .clip(true)
          .borderradius(10)
        text('backspace')
          .fontsize(16)
          .backgroundcolor('#abb0ba')
          .textalign(textalign.center)
          .onclick(() => {
            this.text = this.text.substring(0, this.text.length - 1)
          })
          .clip(true)
          .borderradius(10)
        text('controller')
          .fontsize(16)
          .backgroundcolor('#abb0ba')
          .textalign(textalign.center)
          .onclick(() => {
            ......
          })
          .clip(true)
          .borderradius(10)
      }
      grid() {
        foreach(this.letters, (item) => {
          griditem() {
            text(item)
              .fontsize(20)
              .backgroundcolor('#ffffff')
              .textalign(textalign.center)
              .onclick(() => {
                this.text += item
                })
              .clip(true)
              .borderradius(5)
          }
        }, item => item)
      }
      .rowstemplate('1fr 1fr 1fr 1fr')
      .columnstemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr')
      .columnsgap(8)
      .rowsgap(8)
      .width('75%')
      .height('25%')
      .margin(5)
      .backgroundcolor('#d2d3d8')
      .clip(true)
      .borderradius(10)
      grid() {
        foreach(this.picitems, (item: picdata) => {
          griditem() {
            picgriditem({ picitem: item })
          }
        }, (item: picdata) => item.id.tostring())
      }
      .rowstemplate('1fr 1fr 1fr')
      .columnstemplate('1fr 1fr')
      .columnsgap(5)
      .rowsgap(8)
      .width('90%')
      .height('58%')
      .backgroundcolor('#ffffff')
      .margin(5)
    }
    .width('98%')
    .backgroundcolor('#ffffff')
  }
}
(左右移动查看全部内容)
其中picgriditem将picitem的图片源和图片名称绑定,实现代码如下:
// 九宮格拼图组件
@component
struct picgriditem {
  private picitem: picdata
  build() {
    column() {
      image(this.picitem.image)
        .objectfit(imagefit.contain)
        .height('85%')
        .width('100%')
        .onclick(() => {
          ......
          })
        })
      text(this.picitem.name)
        .fontsize(20)
        .fontcolor('#000000')
    }
    .height('100%')
    .width('90%')
  }
}(左右移动查看全部内容)  
实现tv端视频播放界面,在mainability/pages/videoplay.ets 文件中添加组件。页面布局代码如下:
import router from '@system.router'
@entry
@component
struct play {
// 取到index页面跳转来时携带的source对应的数据。
  private source: string = router.getparams().source
  build() {
    column() {
      video({
        src: this.source,
      })
        .width('100%')
        .height('100%')
        .autoplay(true)
        .controls(true)
    }
  }
}
(左右移动查看全部内容)
在mainability/pages/tvindex.ets中,给picgriditem的图片添加点击事件,点击图片即可播放picitem的视频源。实现代码如下:
 image(this.picitem.image)
      ......
      .onclick(() => {
      router.push({
      uri: 'pages/videoplay',
      params: { source: this.picitem.video }
  })
})(左右移动查看全部内容)  
实现手机遥控端界面
在本章节中,您将学会开发手机遥控端默认界面,示意图参考第一章图2所示。
phoneability/pages/phoneindex.ets 主界面文件中添加入口组件。页面布局代码如下:
@entry
@component
struct index {
  build() {
    flex({ direction: flexdirection.column, alignitems: itemalign.center }) {
      row() {
        image($rawfile('tv.png'))
          .width(25)
          .height(25)
        text('华为智慧屏').fontsize(20).margin(10)
      }
      // 文字搜索框
      textinput({ placeholder: 'search' })
        .margin(20)
        .onchange((value: string) => {
          if (connectmodel.mremote){
            ......
          }
        })
      grid() {
        griditem() {
      // 向上箭头
          button({ type: buttontype.circle, stateeffect: true }) {
            image($rawfile('up.png')).width(80).height(80)
          }
          .onclick(() => {
            ......
          })
          .width(80)
          .height(80)
          .backgroundcolor('#ffffff')
        }
        .columnstart(1)
        .columnend(5)
        griditem() {
      // 向左箭头
          button({ type: buttontype.circle, stateeffect: true }) {
            image($rawfile('left.png')).width(80).height(80)
          }
          .onclick(() => {
            ......
          })
          .width(80)
          .height(80)
          .backgroundcolor('#ffffff')
        }
        griditem() {
      // 播放键
          button({ type: buttontype.circle, stateeffect: true }) {
            image($rawfile('play.png')).width(60).height(60)
          }
          .onclick(() => {
            ......
          })
          .width(80)
          .height(80)
          .backgroundcolor('#ffffff')
        }
        griditem() {
      // 向右箭头
          button({ type: buttontype.circle, stateeffect: true }) {
            image($rawfile('right.png')).width(70).height(70)
          }
          .onclick(() => {
            ......
          })
          .width(80)
          .height(80)
          .backgroundcolor('#ffffff')
        }
        griditem() {
      // 向下箭头
          button({ type: buttontype.circle, stateeffect: true }) {
            image($rawfile('down.png')).width(70).height(70)
          }
          .onclick(() => {
            ......
          })
          .width(80)
          .height(80)
          .backgroundcolor('#ffffff')
        }
        .columnstart(1)
        .columnend(5)
      }
      .rowstemplate('1fr 1fr 1fr')
      .columnstemplate('1fr 1fr 1fr')
      .backgroundcolor('#ffffff')
      .margin(10)
      .clip(new circle({ width: 325, height: 325 }))
      .width(350)
      .height(350)
      row({ space:100 }) {
        // 返回键
        button({ type: buttontype.circle, stateeffect: true }) {
          image($rawfile('return.png')).width(40).height(40)
        }
        .onclick(() => {
          ......
        })
        .width(100)
        .height(100)
        .backgroundcolor('#ffffff')
        // 关机键
        button({ type: buttontype.circle, stateeffect: true }) {
          image($rawfile('off.png')).width(40).height(40)
        }
        .onclick(() => {
          ......
        })
        .width(100)
        .height(100)
        .backgroundcolor('#ffffff')
        // 搜索键
        button({ type: buttontype.circle, stateeffect: true }) {
          image($rawfile('search.png')).width(40).height(40)
        }
        .onclick(() => {
          ......
        })
        .width(100)
        .height(100)
        .backgroundcolor('#ffffff')
      }
      .padding({ left:100 })
    }
    .backgroundcolor('#e3e3e3')
  }
}
(左右移动查看全部内容)
原文标题:openharmony 实例:dayu200 分布式遥控器
文章出处:【微信公众号:harmonyos官方合作社区】欢迎添加关注!文章转载请注明出处。


5通道(3路+V和2路-V)热插拔参考设计
为什么需要电压基准部件?电压基准对A/D和D/A转换影响
Nexperia的锗化硅整流器问市,兼具高效、稳定于一身
FPGA、ASIC有望在机器学习领域中崛起
七夕送男朋友什么礼物?他是不是缺头戴式蓝牙耳机?
开发TV端默认界面和视频播放界面以及手机遥控端默认界面
Commvault在2023云计算层云奖评选中被评为全球云安全领导者
OPPO Find X已经进入发布倒计时,疑似正面照曝光
锁相放大器如何有效应用于弱信号检测
关于无人机分析和介绍
Σ-Δ模数转换器:数字滤波器类型
海尔AI智能音箱新品上市 一句话可控制全屋家电
哈啰出行将切入两轮电动车业务,推出智能两轮“新物种”
台达多功能旋压机的控制模式与特点介绍
苹果合作伙伴广达电脑将为AR眼镜制造镜头
51单片机中断系统的讲解
ColorOS 6带来完美的触控体验
飞针测试技术概述
什么是电源适配器?是干什么用的?电源适配器可以通用吗?
skylake和haswell区别