Compare commits
2 Commits
7f6aaff61c
...
wuxichen
| Author | SHA1 | Date | |
|---|---|---|---|
| bfa3d914e8 | |||
| 81fd471fd9 |
@@ -18,91 +18,36 @@ function Index(props: DefinedProps) {
|
|||||||
const [currentPlayingId, setCurrentPlayingId] = useState<number>();
|
const [currentPlayingId, setCurrentPlayingId] = useState<number>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isRecording) {
|
if (isRecording && currentPlayingId) {
|
||||||
stopAllAudio();
|
audioRefs.current[currentPlayingId].pause();
|
||||||
|
audioRefs.current[currentPlayingId].currentTime = 0;
|
||||||
|
setCurrentPlayingId(undefined);
|
||||||
}
|
}
|
||||||
}, [isRecording]);
|
}, [isRecording, currentPlayingId]);
|
||||||
const onVoiceChange = () => {
|
|
||||||
setIsPlating(!isPlaying);
|
|
||||||
};
|
|
||||||
const playAudio = (id: number, audioUrl: string) => {
|
const playAudio = (id: number, audioUrl: string) => {
|
||||||
if (isRecording) {
|
if (isRecording) {
|
||||||
Toast.show("录音中,无法播放音频");
|
Toast.show("录音中,无法播放音频");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentPlayingId === id && audioRefs.current[id]) {
|
|
||||||
if (audioRefs.current[id]) {
|
|
||||||
audioRefs.current[id].pause();
|
|
||||||
audioRefs.current[id].currentTime = 0;
|
|
||||||
}
|
|
||||||
setCurrentPlayingId(undefined);
|
|
||||||
setIsPlating(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
stopAllAudio();
|
|
||||||
if (!audioRefs.current[id]) {
|
|
||||||
audioRefs.current[id] = new Audio(audioUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
const audio = audioRefs.current[id];
|
|
||||||
audio.currentTime = 0;
|
|
||||||
|
|
||||||
audio.onended = () => {
|
|
||||||
setCurrentPlayingId(undefined);
|
|
||||||
setIsPlating(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
audio.onerror = () => {
|
|
||||||
const error = audio.error;
|
|
||||||
switch (error?.code) {
|
|
||||||
case MediaError.MEDIA_ERR_ABORTED:
|
|
||||||
console.warn("音频播放被用户中断");
|
|
||||||
break;
|
|
||||||
case MediaError.MEDIA_ERR_NETWORK:
|
|
||||||
console.warn("网络错误,无法加载音频");
|
|
||||||
break;
|
|
||||||
case MediaError.MEDIA_ERR_DECODE:
|
|
||||||
console.warn("解码失败,可能是格式不支持");
|
|
||||||
break;
|
|
||||||
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
|
|
||||||
console.warn("浏览器不支持该音频格式");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
console.warn("未知错误");
|
|
||||||
}
|
|
||||||
Toast.show("音频播放失败1111");
|
|
||||||
setIsPlating(false);
|
|
||||||
};
|
|
||||||
console.log("audio play", id);
|
|
||||||
audio
|
|
||||||
.play()
|
|
||||||
.then(() => {
|
|
||||||
setCurrentPlayingId(id);
|
|
||||||
setIsPlating(true);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("音频播放失败:", error);
|
|
||||||
Toast.show("音频播放失败");
|
|
||||||
});
|
|
||||||
};
|
|
||||||
const stopAllAudio = () => {
|
|
||||||
if (currentPlayingId && audioRefs.current[currentPlayingId]) {
|
if (currentPlayingId && audioRefs.current[currentPlayingId]) {
|
||||||
audioRefs.current[currentPlayingId].pause();
|
audioRefs.current[currentPlayingId].pause();
|
||||||
audioRefs.current[currentPlayingId].currentTime = 0;
|
audioRefs.current[currentPlayingId].currentTime = 0;
|
||||||
setIsPlating(false);
|
|
||||||
setCurrentPlayingId(undefined);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
if (currentPlayingId !== id) {
|
||||||
Object.values(audioRefs.current).forEach((audio) => {
|
setCurrentPlayingId(id);
|
||||||
if (!audio.paused) {
|
audioRefs.current[id] = new Audio(audioUrl);
|
||||||
audio.pause();
|
audioRefs.current[id].play();
|
||||||
audio.currentTime = 0;
|
audioRefs.current[id].onended = () => {
|
||||||
}
|
setCurrentPlayingId(undefined);
|
||||||
});
|
};
|
||||||
|
} else {
|
||||||
|
audioRefs.current[id].pause();
|
||||||
|
audioRefs.current[id].currentTime = 0;
|
||||||
|
setCurrentPlayingId(undefined);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderAvatar = (item: Message) => {
|
const renderAvatar = (item: Message) => {
|
||||||
return <Avatar src={item.petAvatar || ""} style={{ "--border-radius": "32px" }} />;
|
return <Avatar src={item.petAvatar || ""} style={{ "--border-radius": "32px" }} />;
|
||||||
};
|
};
|
||||||
@@ -155,7 +100,7 @@ function Index(props: DefinedProps) {
|
|||||||
return (
|
return (
|
||||||
<div className="message">
|
<div className="message">
|
||||||
{data.map((item, index) => (
|
{data.map((item, index) => (
|
||||||
<div className="item" key={index} onClick={() => playAudio(item.id, item.contentText)}>
|
<div className="item" key={index}>
|
||||||
{renderAvatar(item)}
|
{renderAvatar(item)}
|
||||||
|
|
||||||
<div className="rig">
|
<div className="rig">
|
||||||
@@ -166,40 +111,14 @@ function Index(props: DefinedProps) {
|
|||||||
<Divider direction="vertical" style={{ margin: "0px 8px" }} />
|
<Divider direction="vertical" style={{ margin: "0px 8px" }} />
|
||||||
<span className="">{item.createTime}</span>
|
<span className="">{item.createTime}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="voice-container">
|
<div className="voice-container" onClick={() => playAudio(item.id, item.contentText)}>
|
||||||
<VoiceIcon
|
<VoiceIcon
|
||||||
onChange={onVoiceChange}
|
// onChange={onVoiceChange}
|
||||||
isPlaying={isPlaying && currentPlayingId === item.id}
|
isPlaying={currentPlayingId === item.id}
|
||||||
/>
|
/>
|
||||||
<div className="time">{item.contentDuration}''</div>
|
<div className="time">{item.contentDuration}''</div>
|
||||||
</div>
|
</div>
|
||||||
{renderTranslateResult(item)}
|
{renderTranslateResult(item)}
|
||||||
{/* {item.isTranslating ? (
|
|
||||||
<div className="translate">
|
|
||||||
<SpinLoading color="default" style={{ "--size": "12px" }} />
|
|
||||||
<span>翻译中...</span>
|
|
||||||
</div>
|
|
||||||
) : item.transStatus === 1 ? (
|
|
||||||
<div className="translate">
|
|
||||||
{item.transResult?.length ? (
|
|
||||||
item.transResult
|
|
||||||
) : (
|
|
||||||
<Space justify={"between"}>
|
|
||||||
<span>未知语句</span>
|
|
||||||
<Refresh onClick={(e) => refreshMessage(item.id, e)} size="12" fill="#333" />
|
|
||||||
</Space>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<Space justify={"between"} className="translate">
|
|
||||||
<span>翻译失败,请重试</span>
|
|
||||||
<Refresh onClick={(e) => refreshMessage(item.id, e)} size="12" fill="#333" />
|
|
||||||
</Space>
|
|
||||||
// <div className="translate">
|
|
||||||
// <span> 翻译失败,请重试</span>
|
|
||||||
// <Refresh onClick={(e) => refreshMessage(item.id, e)} size="12" fill="#333" />
|
|
||||||
// </div>
|
|
||||||
)} */}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in New Issue
Block a user