HarmonyOS 横屏播放问题

​播放视频,如果视频在竖屏状态下,如何设置成横屏,并且这个时候还能继续转屏操作。

当前监听转屏的方法如下:​

this.orientationChangeListener = mediaquery.matchMediaSync('(orientation: portrait)'); 
this.orientationChangeListener.on('change', (result) => { 
  this.onPortrait(result) 
});
  • 1.
  • 2.
  • 3.
  • 4.

​目前通过按钮设置成横屏的方法

w.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE)
  • 1.

但是这样就无法触发竖屏的监听无法转成横屏了。

能否可以在没有转屏的时候直接设置成横屏状态。​

HarmonyOS
2024-11-13 10:59:07
782浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

​应用级设置屏幕自动旋转:在模块配置文件module.json5中给EntryAbility设置"orientation": “auto_rotation_restricted”,再打开手机自动旋转即可。

应用中通过window.getLastWindow 获取window实例–>用setPreferredOrientation设置窗口显示方向的属性。通过diplay.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%') 
  } 
}
  • 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.

传感器监听手机旋转角度的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; 
}
  • 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.
分享
微博
QQ
微信
回复
2024-11-13 17:07:47


相关问题
HarmonyOS video如何播放
1133浏览 • 1回复 待解决
HarmonyOS 后布局问题
1340浏览 • 1回复 待解决
HarmonyOS 相机旋转拍照问题
1124浏览 • 1回复 待解决
HarmonyOS Canvas后定位问题
628浏览 • 1回复 待解决
HarmonyOS webView视频展示
898浏览 • 1回复 待解决
HarmonyOS 查询当前状态是还是竖
1289浏览 • 1回复 待解决
HarmonyOS 如何让app支持
1142浏览 • 1回复 待解决
openharmony jsFA 如何显示?
8209浏览 • 1回复 待解决
HarmonyOS 怎么设置某个 page 展示
685浏览 • 1回复 待解决
如何获取当前是还是竖啊?
5912浏览 • 1回复 待解决
Web如何实现后全屏
270浏览 • 0回复 待解决