如何适配网页内播放器全屏

如何适配网页内播放器全屏

HarmonyOS
2024-06-13 23:36:50
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
pfuchenlu

示例代码如下:

import web_webview from '@ohos.web.webview' 
import mediaquery from '@ohos.mediaquery'; 
import window from '@ohos.window'; 
import common from '@ohos.app.ability.common'; 
 
@Entry 
@Component 
struct MediaQueryExample { 
  @State color: string = '#DB7093'; 
  @State text: string = 'Portrait'; 
  @State portraitFunc: mediaquery.MediaQueryResult | void | null = null; 
  handler: FullScreenExitHandler | null = null 
  // 当设备横屏时条件成立 
  listener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)'); 
  controller: web_webview.WebviewController = new web_webview.WebviewController() 
 
  onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { 
    if (mediaQueryResult.matches as boolean) { // 若设备为横屏状态,更改相应的页面布局 
      this.color = '#FFD700'; 
      this.text = 'Landscape'; 
    } else { 
      this.color = '#DB7093'; 
      this.text = 'Portrait'; 
    } 
  } 
 
  aboutToAppear() { 
    // 绑定当前应用实例 
    // 绑定回调函数 
    this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => { 
      this.onPortrait(mediaQueryResult) 
    }); 
  } 
 
  // 改变设备横竖屏状态函数 
  private changeOrientation(isLandscape: boolean) { 
    // 获取UIAbility实例的上下文信息 
    let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 
    // 调用该接口手动改变设备横竖屏状态 
    window.getLastWindow(context).then((lastWindow) => { 
      lastWindow.setPreferredOrientation(isLandscape ? window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT) 
    }); 
  } 
 
  build() { 
    Column() { 
      Web({ src: $rawfile('t1.html'), controller: this.controller }) 
        .javaScriptAccess(true) 
        .domStorageAccess(true) 
        .onFullScreenEnter((event) => { 
          console.log("onFullScreenEnter...") 
          this.handler = event.handler 
          this.changeOrientation(true); 
        }) 
        .onFullScreenExit(() => { 
          console.log("onFullScreenExit...") 
          if (this.handler) { 
            this.handler.exitFullScreen() 
            this.changeOrientation(false); 
          } 
        }) 
    } 
    .width('100%').height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-06-14 22:44:49
相关问题
播放器】硬解码支持的Demo
520浏览 • 1回复 待解决
使用AVPlayer实现视频播放器
274浏览 • 1回复 待解决
java播放器怎么用解码?
3533浏览 • 1回复 待解决
AudioRenderer和播放器是什么关系?
2539浏览 • 1回复 待解决
播放器API-timeUpdate-时间单位不清楚
464浏览 • 1回复 待解决
dayu200 HDMI接口如何适配大屏显示
1691浏览 • 1回复 待解决
如何判断Web组件是否全屏
684浏览 • 1回复 待解决
如何禁用窗口的全屏显示功能
596浏览 • 1回复 待解决
如何设置全屏返回的动效
563浏览 • 1回复 待解决