HarmonyOS video空间自定义控制器?

阅读 505
1 个回答

video自带的全屏功不能自定义设置控件

解决方案:

给视频展示区一个高度,通过调整尺寸实现全屏的效果,同时也可以显示自定义的播放控件

参考以下demo:

// xxx.ets

import { window } from '@kit.ArkUI' 
 
@Entry 
@Component 
struct VideoCreateComponent { 
  @State videoSrc: Resource = $rawfile('demo.mp4') 
  @State previewUri: Resource = $r('app.media.startIcon') 
  @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X 
  @State isAutoPlay: boolean = false 
  @State showControls: boolean = true 
  @State videoHeight: string = '600vp' 
  controller: VideoController = new VideoController() 
  @State windowClass:window.Window|undefined = undefined 
 
  aboutToAppear(): void { 
    window.getLastWindow(getContext(this)).then((window)=>{ 
      this.windowClass = window 
    }) 
  } 
 
  build() { 
    Stack() { 
      Video({ 
        src: this.videoSrc, 
        previewUri: this.previewUri, 
        currentProgressRate: this.curRate, 
        controller: this.controller 
      }) 
        .width('100%') 
        .height(this.videoHeight) 
        .autoPlay(this.isAutoPlay) 
        .controls(this.showControls) 
 
      Row() { 
        Button('controls').onClick(() => { 
          this.showControls = !this.showControls // 切换是否显示视频控制栏 
        }).margin(5) 
        Button('start').onClick(() => { 
          this.controller.start() // 开始播放 
        }).margin(5) 
        Button('pause').onClick(() => { 
          this.controller.pause() // 暂停播放 
        }).margin(5) 
        Button('fullscreen').onClick(async () => { 
          if(!this.windowClass){ 
            return; 
          } 
          if(this.videoHeight === '100%'){ 
            this.windowClass.setWindowLayoutFullScreen(false).then(()=>{ 
              this.videoHeight = '600vp' 
            }) 
          }else{ 
            this.windowClass.setWindowLayoutFullScreen(true).then(()=>{ 
              this.videoHeight = '100%' 
            }) 
          } 
        }).margin(5) 
      } 
    } 
    .alignContent(Alignment.Bottom) 
  } 
} 
 
interface DurationObject { 
  duration: number; 
} 
 
interface TimeObject { 
  time: number; 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进