uni-app live-pusher 直播推流 连接耳机后 部分手机仍通过扬声器发声

uni-app live-pusher 直播推流 连接耳机后 部分手机仍通过扬声器发声

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

测试过的手机

华为3.0.0 不行 还有一加不行,红米可以

操作步骤:

<template>  
<view>  
<live-pusher id='livePusher' ref="livePusher" class="livePusher" :url="rtmpPush"  
enable-mic="true" mode="FHD" :muted="false"  :enable-camera="true" :auto-focus="true"
beauty="1" whiteness="2"
    aspect="9:16" @statechange="statechange" @netstatus="netstatus" @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">切换摄像头2</button>
</view>
</template>  
<script>
import {
getPushUrlAndPullUrl, // 寺院端直播 录播
} from "@/apis/index.js";
export default {
data() {
return {
rtmpPush: '',
id: ''
}
},
onLoad(options) {
var that = this
console.log(options)
that.id = options.id
this.getUrl()
},
onReady() {
// 注意:需要在onReady中 或 onLoad 延时
this.context = uni.createLivePusherContext("livePusher", this);
},
methods: {
getUrl() {
var that = this
console.log('请求')
getPushUrlAndPullUrl({
orderId: that.id,
pullAllPush: 1
}).then(res => {
// 推流地址
that.rtmpPush = res.data.rtmpPush
console.log('获取推流地址了', res.data.rtmpPush)
// that.startTimer();
})
},
statechange(e) {
console.log("statechange:" + JSON.stringify(e));
},
netstatus(e) {
console.log("netstatus:" + JSON.stringify(e));
},
error(e) {
console.log("error:" + JSON.stringify(e));
},
start: function() {
console.log('开始推流')
this.context.start({
success: (a) => {
uni.showToast({
title: "livePusher.start:" + JSON.stringify(a),
icon: "none"
})
console.log("livePusher.start:" + JSON.stringify(a));
}, fail: (a) => {
uni.showToast({
title: "livePusher.start失败:" + JSON.stringify(a),
icon: "none"
})
}, complete: (a) => {
uni.showToast({
title: "livePusher.start统调:" + JSON.stringify(a),
icon: "none"
})
}
})
},
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));
uni.saveImageToPhotosAlbum({
filePath: e.message.tempImagePath, //图片url
success: () => {
uni.showToast({
title: "分享图片已保存到相册",
icon: 'none',
duration: 2000
})
}
})
}
});
},
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>  
1 回复

在使用 uni-applive-pusher 组件进行直播推流时,如果连接耳机后部分手机仍然通过扬声器发声,可能是由于以下几个原因导致的:

1. 耳机未正确识别

部分手机可能由于硬件或系统问题,未能正确识别耳机。可以通过以下步骤检查耳机是否被正确识别:

  • 确保耳机已完全插入。
  • 检查手机是否识别到耳机(通常会在通知栏显示耳机图标)。
  • 尝试在其他应用中播放音频,确认是否通过耳机发声。

2. 音频路由设置问题

live-pusher 组件中,音频路由可能未正确切换到耳机。可以尝试以下方法:

  • live-pusher 组件的配置中,检查是否有音频路由的配置选项。
  • 使用 uni.getAudioManager() 获取音频管理器,手动设置音频路由:
    const audioManager = uni.getAudioManager();
    audioManager.setRoute(1); // 1 表示耳机,0 表示扬声器
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!