g***@gmail.com
g***@gmail.com
  • 发布:2026-10-28 09:34
  • 更新:2026-10-28 09:35
  • 阅读:21

uniapp在使用 plus.video.createLivePusher 然后调用 switchCamera 方法切换前置的时候页面出现卡死的问题,需要如何处理。ios正常,部分安卓手机不行。

分类:uni-app

createPusher() {
const pages = getCurrentPages()
const currentWebview = pages[pages.length - 1].$getAppWebview()
this.pusher = plus.video.createLivePusher('livepusher', {
url: '',
top: '0px',
left: '0px',
width: '100%',
height: '100%',
position: 'absolute',
aspect: '9:16',
'z-index': 1,
})
currentWebview.append(this.pusher)
try {
this.pusher.switchCamera()
} catch (e) {}
this.pusher.preview()
},

2026-10-28 09:34 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

欢迎到专业群( uni-app 官方技术交流群 1 ) 咨询,群中有相关专业的管理员和群友。

这是一个在部分安卓机型上已知的兼容性问题,社区中有多位开发者反馈了相同的现象,主要集中在小米、华为、OPPO等设备上,表现为调用 switchCamera 切换前置摄像头时画面卡死或黑屏。相关反馈可以参考以下社区帖子:

针对你提供的代码,建议从以下两个方面进行处理:

1. 调整代码调用顺序与时机

在你的代码中,append 之后立即调用了 switchCamera(),随后才调用 preview()。这可能导致摄像头底层尚未初始化完成时就执行了切换操作,从而在部分安卓机型上引发卡死。

建议先调用 preview() 开启预览,然后再通过 setTimeout 延迟一段时间再调用 switchCamera(),确保摄像头准备就绪:

createPusher() {  
  const pages = getCurrentPages()  
  const currentWebview = pages[pages.length - 1].$getAppWebview()  
  this.pusher = plus.video.createLivePusher('livepusher', {  
    url: '',  
    top: '0px',  
    left: '0px',  
    width: '100%',  
    height: '100%',  
    position: 'absolute',  
    aspect: '9:16',  
    'z-index': 1,  
  })  
  currentWebview.append(this.pusher)  

  // 先开启预览  
  this.pusher.preview()  

  // 延迟一段时间再切换摄像头,避免摄像头未初始化完成导致卡死  
  setTimeout(() => {  
    try {  
      this.pusher.switchCamera()  
    } catch (e) {}  
  }, 500)  
}

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

2. 官方推荐方案:使用 nvue 的 live-pusher 组件

在 App 端进行直播推流开发时,官方推荐使用 nvue 页面的 <live-pusher> 组件,而不是在 vue 页面使用 plus.video.createLivePusher。

根据官方文档说明,nvue 的优势在于:

  • nvue 也可以一套代码编译多端。
  • nvue 没有层级问题,任意组件都可以覆盖 live-pusher 组件。
  • 若需要视频内嵌在 swiper 里上下滑动(类抖音、映客首页模式),App 端只有 nvue 才能实现。

建议将推流页面改为 nvue 页面,并使用 <live-pusher> 组件配合 uni.createLivePusherContext 来控制摄像头切换,这样可以获得更好的原生渲染性能和兼容性。详情请参考官方文档:live-pusher 组件文档

内容为 AI 生成,仅供参考

要回复问题请先登录或注册