fix: android修复
parent
86918a667e
commit
84223d4c70
|
|
@ -6,20 +6,20 @@ import {
|
||||||
RealtimeClientProvider,
|
RealtimeClientProvider,
|
||||||
} from "@/components/Provider/RealtimeClientProvider";
|
} from "@/components/Provider/RealtimeClientProvider";
|
||||||
import { ReportProvider } from "@/components/Provider/ReportResolveProvider";
|
import { ReportProvider } from "@/components/Provider/ReportResolveProvider";
|
||||||
import { RealtimeUtils } from "@coze/realtime-api";
|
// import { RealtimeUtils } from "@coze/realtime-api";
|
||||||
import { useLocation } from "react-router-dom";
|
import { useLocation } from "react-router-dom";
|
||||||
|
|
||||||
function MainContent() {
|
function MainContent() {
|
||||||
const { isConnected, handleDisconnect } = useContext(RealtimeClientContext);
|
const { isConnected, handleDisconnect } = useContext(RealtimeClientContext);
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
const handlePromise = async() => {
|
// const handlePromise = async() => {
|
||||||
await RealtimeUtils.checkDevicePermission(false);
|
// await RealtimeUtils.checkDevicePermission(false);
|
||||||
}
|
// }
|
||||||
|
|
||||||
useEffect(() => {
|
// useEffect(() => {
|
||||||
handlePromise();
|
// handlePromise();
|
||||||
}, []);
|
// }, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isConnected) {
|
if (isConnected) {
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,9 @@ export default function AntechamberFile({handleLoading}:Props) {
|
||||||
|
|
||||||
handleConnect({
|
handleConnect({
|
||||||
fileInfo: {type: resp.type,url: resp.url,tableName: resp.tableName,provinceName: resp.provinceName,subjectClaim: resp.subjectClaim},
|
fileInfo: {type: resp.type,url: resp.url,tableName: resp.tableName,provinceName: resp.provinceName,subjectClaim: resp.subjectClaim},
|
||||||
|
}).then(() => {
|
||||||
|
setHasHandledReport(true);
|
||||||
});
|
});
|
||||||
setHasHandledReport(true);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -105,17 +105,7 @@ export const RealtimeClientProvider = ({
|
||||||
initMessage?: string;
|
initMessage?: string;
|
||||||
fileInfo?: FileInfo;
|
fileInfo?: FileInfo;
|
||||||
}) => {
|
}) => {
|
||||||
const perm = await RealtimeUtils.checkDevicePermission(false);
|
|
||||||
const device = await RealtimeUtils.getAudioDevices();
|
|
||||||
|
|
||||||
if (!perm.audio) {
|
|
||||||
toast({ title: "连接错误", description: "需要麦克风访问权限" });
|
|
||||||
throw new Error("需要麦克风访问权限");
|
|
||||||
}
|
|
||||||
if (device.audioInputs.length === 0) {
|
|
||||||
toast({ title: "连接错误", description: "没有麦克风设备" });
|
|
||||||
throw new Error("没有麦克风设备");
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = new RealtimeClient({
|
const client = new RealtimeClient({
|
||||||
accessToken: token,
|
accessToken: token,
|
||||||
|
|
@ -125,7 +115,7 @@ export const RealtimeClientProvider = ({
|
||||||
allowPersonalAccessTokenInBrowser: true,
|
allowPersonalAccessTokenInBrowser: true,
|
||||||
suppressStationaryNoise: true,
|
suppressStationaryNoise: true,
|
||||||
suppressNonStationaryNoise: true,
|
suppressNonStationaryNoise: true,
|
||||||
debug: true,
|
debug: false,
|
||||||
});
|
});
|
||||||
clientRef.current = client;
|
clientRef.current = client;
|
||||||
|
|
||||||
|
|
@ -149,6 +139,18 @@ export const RealtimeClientProvider = ({
|
||||||
connectingLockRef.current = false;
|
connectingLockRef.current = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const perm = await RealtimeUtils.checkDevicePermission(false);
|
||||||
|
const device = await RealtimeUtils.getAudioDevices();
|
||||||
|
if (!perm.audio) {
|
||||||
|
toast({ title: "连接错误", description: "需要麦克风访问权限" });
|
||||||
|
return;
|
||||||
|
// throw new Error("需要麦克风访问权限");
|
||||||
|
}
|
||||||
|
if (device.audioInputs.length === 0) {
|
||||||
|
toast({ title: "连接错误", description: "没有麦克风设备" });
|
||||||
|
return;
|
||||||
|
// throw new Error("没有麦克风设备");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!clientRef.current) {
|
if (!clientRef.current) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,75 @@
|
||||||
|
import { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
|
export function useTtsPlayer() {
|
||||||
|
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||||
|
|
||||||
|
// 第一次挂载时,创建一个隐藏的 <audio> 并追加到 body
|
||||||
|
useEffect(() => {
|
||||||
|
const audio = document.createElement('audio');
|
||||||
|
audio.style.display = 'none';
|
||||||
|
document.body.appendChild(audio);
|
||||||
|
audioRef.current = audio;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
// 卸载时清理
|
||||||
|
document.body.removeChild(audio);
|
||||||
|
audioRef.current = null;
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 播放指定 URL 的 TTS,返回一个 Promise,在播放成功时 resolve
|
||||||
|
*/
|
||||||
|
function playTts(url: string): Promise<void> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const audio = audioRef.current;
|
||||||
|
if (!audio) {
|
||||||
|
reject(new Error('Audio element not initialized'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
audio.src = url;
|
||||||
|
let resolved = false;
|
||||||
|
|
||||||
|
// 真正触发播放的逻辑
|
||||||
|
const tryPlay = () => {
|
||||||
|
audio.play()
|
||||||
|
.then(() => {
|
||||||
|
if (!resolved) {
|
||||||
|
resolved = true;
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
// 如果因为缺少用户手势被阻止,就交给后面两个事件
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// 1⃣️ 立即尝试一次
|
||||||
|
tryPlay();
|
||||||
|
|
||||||
|
// 2⃣️ 在微信 JSBridgeReady 后再试一次(仅触发一次)
|
||||||
|
const onBridge = () => {
|
||||||
|
tryPlay();
|
||||||
|
document.removeEventListener('WeixinJSBridgeReady', onBridge);
|
||||||
|
};
|
||||||
|
document.addEventListener('WeixinJSBridgeReady', onBridge);
|
||||||
|
|
||||||
|
// 3⃣️ 绑定一次性的 touchstart 保底,用户首次触摸时一定能播放
|
||||||
|
const onTouch = () => {
|
||||||
|
tryPlay();
|
||||||
|
document.body.removeEventListener('touchstart', onTouch);
|
||||||
|
};
|
||||||
|
document.body.addEventListener('touchstart', onTouch, { once: true });
|
||||||
|
|
||||||
|
// 可选:加个超时,播放一直没成功就 reject
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!resolved) {
|
||||||
|
reject(new Error('TTS play timeout'));
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { playTts };
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue