HarmonyOS Swiper中嵌套Video如何控制当前显示视频的播放

HarmonyOS Swiper中嵌套Video如何控制当前显示视频的播放

HarmonyOS
2024-08-08 18:32:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

可以参考以下demo:

@Entry 
@Component 
struct VideoCreateComponent { 
  @State videoSrc: Resource = $rawfile('video1.mp4') 
  @State previewUri: Resource = $rawfile('video2.mp4') 
  @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X 
  @State isAutoPlay: boolean = true 
  @State showControls: boolean = true 
  controller: VideoController = new VideoController() 
 
  build() { 
    Column() { 
      Video({ 
        src: this.videoSrc, 
        previewUri: this.previewUri, 
        currentProgressRate: this.curRate, 
        controller: this.controller 
      }) 
        .width('100%') 
        .height(600) 
        .autoPlay(this.isAutoPlay) 
        .controls(this.showControls) 
 
      Row() { 
        Button('src').onClick(() => { 
          this.videoSrc = $rawfile('video2.mp4') // 切换视频源 
        }).margin(5) 
        Button('videoSrc').onClick(() => { 
          this.videoSrc = $rawfile('video1.mp4') // 切换视频预览海报 
        }).margin(5) 
        Button('controls').onClick(() => { 
          this.showControls = !this.showControls // 切换是否显示视频控制栏 
        }).margin(5) 
      } 
 
      Row() { 
        Button('start').onClick(() => { 
          this.controller.start() // 开始播放 
        }).margin(5) 
        Button('pause').onClick(() => { 
          this.controller.pause() // 暂停播放 
        }).margin(5) 
        Button('stop').onClick(() => { 
          this.controller.stop() // 结束播放 
        }).margin(5) 
        Button('setTime').onClick(() => { 
          this.controller.setCurrentTime(10, SeekMode.Accurate) // 精准跳转到视频的10s位置 
        }).margin(5) 
      } 
 
      Row() { 
        Button('rate 0.75').onClick(() => { 
          this.curRate = PlaybackSpeed.Speed_Forward_0_75_X 
        }).margin(5) 
        Button('rate 1').onClick(() => { 
          this.curRate = PlaybackSpeed.Speed_Forward_1_00_X 
        }).margin(5) 
        Button('rate 2').onClick(() => { 
          this.curRate = PlaybackSpeed.Speed_Forward_2_00_X 
        }).margin(5) 
      } 
    } 
  } 
} 
 
interface DurationObject { 
  duration: number; 
} 
 
interface TimeObject { 
  time: number; 
} 
@Entry 
@Component 
struct VideoCreateComponent { 
  @State videoSrc: Resource = $rawfile('video1.mp4') 
  @State previewUri: Resource = $rawfile('video2.mp4') 
  @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X 
  @State isAutoPlay: boolean = true 
  @State showControls: boolean = true 
  controller: VideoController = new VideoController() 
  private swiperController: SwiperController = new SwiperController() 
 
  build() { 
    Column() { 
      Swiper(this.swiperController){ 
        Video({ 
          src: this.videoSrc, 
          previewUri: this.previewUri, 
          currentProgressRate: this.curRate, 
          controller: this.controller 
        }) 
          .width('100%') 
          .height(300) 
          .autoPlay(this.isAutoPlay) 
          .controls(this.showControls) 
        Video({ 
          src: this.previewUri, 
          previewUri: this.videoSrc, 
          currentProgressRate: this.curRate, 
          controller: this.controller 
        }) 
          .width('100%') 
          .padding({top:300}) 
          .height(300) 
          .autoPlay(this.isAutoPlay) 
          .controls(this.showControls) 
      } 
 
      Row() { 
        Button('src').onClick(() => { 
          this.videoSrc = $rawfile('video2.mp4') // 切换视频源 
        }).margin(5) 
        Button('videoSrc').onClick(() => { 
          this.videoSrc = $rawfile('video1.mp4') // 切换视频预览海报 
        }).margin(5) 
        Button('controls').onClick(() => { 
          this.showControls = !this.showControls // 切换是否显示视频控制栏 
        }).margin(5) 
      } 
 
      Row() { 
        Button('start').onClick(() => { 
          this.controller.start() // 开始播放 
        }).margin(5) 
        Button('pause').onClick(() => { 
          this.controller.pause() // 暂停播放 
        }).margin(5) 
        Button('stop').onClick(() => { 
          this.controller.stop() // 结束播放 
        }).margin(5) 
        Button('setTime').onClick(() => { 
          this.controller.setCurrentTime(10, SeekMode.Accurate) // 精准跳转到视频的10s位置 
        }).margin(5) 
      } 
 
      Row() { 
        Button('rate 0.75').onClick(() => { 
          this.curRate = PlaybackSpeed.Speed_Forward_0_75_X 
        }).margin(5) 
        Button('rate 1').onClick(() => { 
          this.curRate = PlaybackSpeed.Speed_Forward_1_00_X 
        }).margin(5) 
        Button('rate 2').onClick(() => { 
          this.curRate = PlaybackSpeed.Speed_Forward_2_00_X 
        }).margin(5) 
      } 
    } 
  } 
} 
 
interface DurationObject { 
  duration: number; 
} 
 
interface TimeObject { 
  time: number; 
}
分享
微博
QQ
微信
回复
2024-08-08 21:12:59
相关问题
video player播放在线视频失败
9808浏览 • 1回复 待解决
HarmonyOS video如何横屏播放
155浏览 • 1回复 待解决
HSPrawfile里视频不能播放
177浏览 • 1回复 待解决
TV 如何用Java播放本地视频
9261浏览 • 2回复 待解决
HarmonyOS webview如何播放本地沙箱视频
1167浏览 • 1回复 待解决
codelabsVideoApplication视频显示
5645浏览 • 1回复 待解决