uni-app targetSdkVersion值设置为30 live-pusher推流无声音

uni-app targetSdkVersion值设置为30 live-pusher推流无声音

开发环境 版本号 项目创建方式
Windows 26100.4351 HBuilderX

产品分类:

uniapp/App

PC开发环境操作系统:

Windows

HBuilderX类型:

正式

HBuilderX版本号:

4.75

手机系统:

Android

手机系统版本号:

Android 15

手机厂商:

小米

手机机型:

小米15

页面类型:

vue

vue版本:

vue3

打包方式:

云端

项目创建方式:

HBuilderX

示例代码:

<template>  
<view>  
<live-pusher id='livePusher' ref="livePusher" class="livePusher"  
url="rtmp://gwm-000-cn-0604.bcloud365.net:9016/talk_5a9a0b11b208efd9_0/M3w1YTlhMGIxMWIyMDhlZmQ5fGIzYmRkNDk0YjBlNDJkMjBjNWY4OGZiZWI0NWYwZTJjMTA4Mz4RhODgwZGFiMGM0YjVlNTc2Mzc0ZDRhMjYzNjF8cnRtcHwxNzU0NjI1Mzg2fDE3NTY0MTYwMTE%3D.54330d11b5a3bfba6497a7fcf40c5863"  
enable-camera="false" [@statechange](/user/statechange)="statechange" [@error](/user/error)="error"></live-pusher>
    <button class="btn" @click="start">开始推流</button>
    <button class="btn" @click="pause">暂停推流</button>
    <button class="btn" @click="resume">resume</button>
    <button class="btn" @click="stop">停止推流</button>
    <button class="btn" @click="snapshot">快照</button>
    <button class="btn" @click="startPreview">开启摄像头预览</button>
    <button class="btn" @click="stopPreview">关闭摄像头预览</button>
    <button class="btn" @click="switchCamera">切换摄像头</button>
</view>
</template>  
<script>  
export default {  
data() {  
return {}  
},  
onReady() {  
// 注意:需要在onReady中 或 onLoad 延时  
this.context = uni.createLivePusherContext("livePusher", this);  
},  
methods: {  
statechange(e) {  
console.log("statechange:" + JSON.stringify(e));  
},  
error(e) {  
console.log("error:" + JSON.stringify(e));  
},  
start: function() {  
this.context.start({  
success: (a) => {  
console.log("livePusher.start:" + JSON.stringify(a));  
}  
});  
},  
close: function() {  
this.context.close({  
success: (a) => {  
console.log("livePusher.close:" + JSON.stringify(a));  
}  
});  
},  
snapshot: function() {  
this.context.snapshot({  
success: (e) => {  
console.log(JSON.stringify(e));  
}  
});  
},  
resume: function() {  
this.context.resume({  
success: (a) => {  
console.log("livePusher.resume:" + JSON.stringify(a));  
}  
});  
},  
pause: function() {  
this.context.pause({  
success: (a) => {  
console.log("livePusher.pause:" + JSON.stringify(a));  
}  
});  
},  
stop: function() {  
this.context.stop({  
success: (a) => {  
console.log(JSON.stringify(a));  
}  
});  
},  
switchCamera: function() {  
this.context.switchCamera({  
success: (a) => {  
console.log("livePusher.switchCamera:" + JSON.stringify(a));  
}  
});  
},  
startPreview: function() {  
this.context.startPreview({  
success: (a) => {  
console.log("livePusher.startPreview:" + JSON.stringify(a));  
}  
});  
},  
stopPreview: function() {  
this.context.stopPreview({  
success: (a) => {  
console.log("livePusher.stopPreview:" + JSON.stringify(a));  
}  
});  
}  
}  
</script>  
<style>  
.livePusher {  
width: 0;  
height: 0;  
}  
</style>

更多关于uni-app targetSdkVersion值设置为30 live-pusher推流无声音的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app targetSdkVersion值设置为30 live-pusher推流无声音的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的Android权限适配问题。当targetSdkVersion设置为30及以上时,Android系统对麦克风权限的获取方式发生了变化。

问题原因: Android 11(API 30)引入了新的权限策略,需要在AndroidManifest.xml中显式声明android.permission.MICROPHONE权限,并且可能需要动态请求录音权限。

解决方案:

  1. 配置manifest.json:
{
  "app-plus": {
    "distribute": {
      "android": {
        "permissions": [
          "<uses-permission android:name=\"android.permission.MICROPHONE\"/>",
          "<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>"
        ]
      }
    }
  }
}
  1. 在代码中动态请求权限:
// 在推流前检查并请求权限
async start() {
  // 检查录音权限
  const result = await uni.authorize({
    scope: 'scope.record'
  });
  
  if (result[0].authSetting['scope.record']) {
    this.context.start({
      success: (a) => {
        console.log("livePusher.start:" + JSON.stringify(a));
      }
    });
  }
}
回到顶部