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; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
分享
微博
QQ
微信
回复
2024-08-08 21:12:59
相关问题
HarmonyOS Video组件如何播放沙盒视频
778浏览 • 1回复 待解决
关于视频播放Video组件问题
276浏览 • 0回复 待解决
video player播放在线视频失败
11014浏览 • 1回复 待解决
HarmonyOS Swiper嵌套RichEditor问题
581浏览 • 1回复 待解决