HarmonyOS 鸿蒙Next播放视频时,如果视频在竖屏状态下如何设置成横屏

HarmonyOS 鸿蒙Next播放视频时,如果视频在竖屏状态下如何设置成横屏 播放视频,如果视频在竖屏状态下,如何设置成横屏?

2 回复

在模块配置文件module.json5中给EntryAbility设置"orientation": “auto_rotation_restricted”,再打开手机自动旋转即可。应用中通过window.getLastWindow 获取window实例->用setPreferredOrientation设置窗口显示方向的属性。通过display.on可以监听屏幕状态改变。点击设置了具体方向后,再加上传感器模式判断屏幕方向。

import window from '@ohos.window';
import display from '@ohos.display';

const TAG = 'foo'
const ORIENTATION: Array<string> = ['垂直', '平', '反向垂直', '反向水平']

@Entry
@Component
struct Index {

  @State rotation: number = 0
  @State message: string = ORIENTATION[this.rotation]
  @Watch('setWindowLayOut') @State isLandscape: boolean = false;// 是否横屏状态

  aboutToAppear() {

    this.setOrientation(1)
    let callback = async () => {
      let d = await display.getDefaultDisplaySync()
      this.rotation = d.rotation
      this.message = ORIENTATION[this.rotation]
      console.info(TAG, JSON.stringify(d))
    }
    try {
      display.on("change", callback);//监听屏幕状态改变
    } catch (exception) {
      console.error(TAG, 'Failed to register callback. Code: ' + JSON.stringify(exception));
    }
  }

  setOrientation(type : number) {
    try {
      window.getLastWindow(getContext(this), (err, data) => { //获取window实例
        if (err.code) {
          console.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err));
          return;
        }
        let windowClass = data;
        console.info(TAG, 'Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));

        let orientation : number;
        if (type === 1) {
          orientation = window.Orientation.AUTO_ROTATION; //设置窗口方向为传感器自动旋转模式。
        } else {
          orientation = window.Orientation.UNSPECIFIED; //设置窗口方向为传感器锁定。
        }
        try {
          windowClass.setPreferredOrientation(orientation, (err) => {
            if (err.code) {
              console.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err));
              return;
            }
            console.info(TAG, 'Succeeded in setting window orientation.');
          });
        } catch (exception) {
          console.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception));
        }
        ;
      });
    } catch (exception) {
      console.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
    }
    ;
  }

  setWindowLayOut() {
    // 调用该接口手动改变设备横竖屏状态(设置全屏模式,先强制横屏,再加上传感器模式)
    window.getLastWindow(getContext(this)).then(windowClass => {
      if (this.isLandscape) {
        console.log('设置屏幕横屏')
        windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE);

      } else {
        console.log('设置屏幕竖屏')
        windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT);
      }
    });
  }

  build() {
    Row() {
      Column() {
        Text(`${this.rotation}`).fontSize(25)
        Text(`${this.message}`).fontSize(25)
        Button('全屏')
          .width(140)
          .onClick(() => {
            this.isLandscape = !this.isLandscape; // 设置横屏
          });
      }
      .width("100%")
    }
    .height('100%')
  }
}

传感器监听手机旋转角度的Demo:

import sensor from '@ohos.sensor';
import base from '@ohos.base';

export function onDegree(callback: base.Callback<string>): void {
  sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => {
    let degree: number = -1;
    let rotation: string = 'INVALID';
    degree = CalDegree(data.x, data.y, data.z)
    if (degree >= 0 && (degree <= 30 || degree >= 330)) {
      rotation = 'ROTATION_0';
    } else if (degree >= 60 && degree <= 120) { // Use ROTATION_90 when degree range is [60, 120]
      rotation = 'ROTATION_90';
    } else if (degree >= 150 && degree <= 210) { // Use ROTATION_180 when degree range is [150, 210]
      rotation = 'ROTATION_180';
    } else if (degree >= 240 && degree <= 300) { // Use ROTATION_270 when degree range is [240, 300]
      rotation = 'ROTATION_270';
    }
    callback(rotation);
  });
}

function CalDegree(x: number, y: number, z: number): number {
  let degree: number = -1;
  // 3 为 有效_增量_角度_阈值_系数
  if ((x * x + y * y) * 3 < z * z) {
    return degree;
  }
  degree = 90 - (Number)(Math.round(Math.atan2(y, -x) / Math.PI * 180));
  return degree >= 0 ? degree % 360 : degree % 360 + 360;
}

更多关于HarmonyOS 鸿蒙Next播放视频时,如果视频在竖屏状态下如何设置成横屏的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙系统中,Next播放视频时若希望将竖屏状态下的视频切换为横屏,可以通过以下步骤实现(不涉及Java或C语言内容):

  1. 检查自动旋转设置:首先确保设备的自动旋转屏幕功能已开启。这通常在设备的下拉通知栏或设置中可找到,确保“自动旋转”选项是打开的。

  2. 应用内设置:部分视频应用可能内置了屏幕方向锁定功能,检查Next播放器的设置菜单,看是否有关于屏幕旋转的选项,并确保其未被锁定为竖屏。

  3. 强制横屏:如果Next播放器没有提供直接的横屏设置,且自动旋转不起作用,可以尝试通过第三方应用或开发者选项来强制应用横屏显示(但此方法可能涉及对系统更深层次的调整,不建议非专业人士操作)。

  4. 物理旋转:简单直接的方法是,在开启自动旋转后,将设备物理旋转至横屏状态,大多数情况下应用会自动适应屏幕方向。

如果以上方法均未能解决问题,可能是由于应用本身的限制或系统兼容性问题。此时,建议直接联系Next播放器的开发者或查看其官方文档获取更具体的解决方案。

如果问题依旧没法解决请联系官网客服, 官网地址是 https://www.itying.com/category-93-b0.html

回到顶部