HarmonyOS webrtc: 在原生浏览器横屏下,UI样式横屏,但摄像头流为竖屏?

在原生浏览器中,使用webrtc API,getUserMedia获取到视频流后。将手机横放后,浏览器UI样式全部使用横屏样式,但是视频流依然是竖向的,没有正确适配横向。

阅读 481
1 个回答

参考以下demo,可以在h5页面进行适配,监听屏幕旋转。

<!-- index.html -->
  <!DOCTYPE html>
  <html>
  <head>
  <meta charset="UTF-8">
  </head>
  <body>
  <video id="video" width="400px" height="400px" autoplay="autoplay">
  </video>
  <input type="button" title="HTML5摄像头" value="开启摄像头" onclick="getMedia()" />
  <script>
  function getMedia() {
    let constraints = {
      video: {
        width: 500,
        height: 500
      },
      audio: true
    }
    let video = document.getElementById("video");
    let promise = navigator.mediaDevices.getUserMedia(constraints);
    promise.then(function(MediaStream) {
      video.srcObject = MediaStream;
      video.play();
    })
  }
// 监听屏幕旋转事件
window.addEventListener('orientationchange', function() {
  console.info('旋转---------**:'+window.orientation)
  // 屏幕旋转90度时
  if (window.orientation === 90) {
    document.getElementById("video").style.cssText = "width:320px; height:480px;transform:rotate(-90deg);"
  }
  else if (window.orientation === -90) {
    document.getElementById("video").style.cssText = "width:320px; height:480px;transform:rotate(90deg);"
  }else if (window.orientation === 0) {
    document.getElementById("video").style.cssText = "width:320px; height:480px;transform:rotate(0deg);"
  }
});
</script>
  </body>
  </html>