HarmonyOS 视频播放器如何旋转屏幕

视频如何横屏播放 如何控制某一个页面可以为横屏

HarmonyOS
2024-12-26 12:53:53
1364浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以参考如下demo:

import { window } from '@kit.ArkUI';
@Entry
@Component
struct VideoPlayerPage {
  controller: VideoController = new VideoController();
  @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X
  @State fullHeight: Length = 600
  @State isFullVertical: boolean = false
  @State isFullHorizontal: boolean = false
  onPageHide(): void {
    this.handlerOrientation(window.Orientation.PORTRAIT)
  }

  handlerOrientation(type: number) {
    window.getLastWindow(getContext(this), (err, win) => {
      win.setPreferredOrientation(type)
    })
  }
  build() {
    Column() {
      Stack({ alignContent: Alignment.Bottom }) {
        Video({
          src: $rawfile('goodsIntro.mp4'),
          previewUri: $r('app.media.app_icon'),
          currentProgressRate: this.curRate,
          controller: this.controller
        })
          .width('100%')
          .height(this.fullHeight)
          .loop(false)
          .objectFit(ImageFit.Contain)
          .autoPlay(false)
          .controls(false)
          .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
        Row() {
          Text('播放').onClick(() => {
            this.controller.start() // 开始播放
          }).margin(5).fontColor(Color.White).fontSize(12)
          Text('暂停').onClick(() => {
            this.controller.pause() // 暂停播放
          }).margin(5).fontColor(Color.White).fontSize(12)
          Text('0.75X').onClick(() => {
            this.curRate = PlaybackSpeed.Speed_Forward_0_75_X
          }).margin(5).fontColor(Color.White).fontSize(12)
          Text('1.0X').onClick(() => {
            this.curRate = PlaybackSpeed.Speed_Forward_1_00_X
          }).margin(5).fontColor(Color.White).fontSize(12)
          Text('2.0').onClick(() => {
            this.curRate = PlaybackSpeed.Speed_Forward_2_00_X
          }).margin(5).fontColor(Color.White).fontSize(12)
          Text(this.isFullVertical ? '退出竖屏' : '竖屏')
            .margin(5)
            .fontSize(12)
            .onClick(() =>
            {
              if (this.isFullVertical) {
                this.fullHeight = 600
              } else {
                this.fullHeight = '100%'
              }
              this.isFullVertical = !this.isFullVertical
            }).fontColor(Color.White)
          Text(this.isFullHorizontal ? '退出横屏' : '横屏')
            .fontSize(12)
            .onClick(() => {
              this.isFullHorizontal = !this.isFullHorizontal
              if (this.isFullHorizontal) {
                this.handlerOrientation(window.Orientation.LANDSCAPE_INVERTED)
                this.fullHeight = '100%'
              } else {
                this.handlerOrientation(window.Orientation.PORTRAIT)
                this.fullHeight = 600
              }
            }).fontColor(Color.White)
        }
        .zIndex(1)
      }
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
  • 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.

页面级别无法设置,只能设置窗口级别的,在页面的生命周期中添加旋转属性:

onPageShow(): void {
  window.getLastWindow(getContext(this), (err, win) => {
  win.setPreferredOrientation(window.Orientation.LANDSCAPE_INVERTED)
})
}

onPageHide(): void {
  window.getLastWindow(getContext(this), (err, win) => {
  win.setPreferredOrientation(window.Orientation.PORTRAIT)
})
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
分享
微博
QQ
微信
回复
2024-12-26 15:06:22


相关问题
HarmonyOS 视频播放器问题
1587浏览 • 1回复 待解决
使用AVPlayer实现视频播放器
3070浏览 • 1回复 待解决
HarmonyOS 点播视频播放器选型咨询
1742浏览 • 1回复 待解决
HarmonyOS 需要视频播放器的选型
1315浏览 • 1回复 待解决
关于视频播放器Video组件的问题
507浏览 • 0回复 待解决
如何适配网页内播放器全屏
1639浏览 • 1回复 待解决
HarmonyOS 播放器功能拓展
983浏览 • 1回复 待解决
HarmonyOS 播放器后台暂停音频播放
1044浏览 • 1回复 待解决