generated from template/astro-template
fix
This commit is contained in:
parent
55c8f877cd
commit
f8f8e2b971
@ -40,14 +40,49 @@ const initGetText = () => {
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const downloadAudio = (url: string, filename?: string) => {
|
||||
const downloadAudio = async (url: string, filename?: string) => {
|
||||
const generatedFilename = filename || nanoid(10) + '.wav';
|
||||
|
||||
// 检测是否为移动设备
|
||||
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
||||
|
||||
try {
|
||||
if (isMobile) {
|
||||
// 移动端处理:使用 fetch 下载并创建 blob URL
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error('下载失败');
|
||||
|
||||
const blob = await response.blob();
|
||||
const blobUrl = URL.createObjectURL(blob);
|
||||
|
||||
// 在移动端,直接打开新窗口播放/下载
|
||||
const newWindow = window.open(blobUrl, '_blank');
|
||||
if (!newWindow) {
|
||||
// 如果无法打开新窗口,提示用户手动下载
|
||||
toast.info('请长按音频链接选择"下载"或"保存到文件"');
|
||||
return;
|
||||
}
|
||||
|
||||
// 清理 blob URL
|
||||
setTimeout(() => {
|
||||
URL.revokeObjectURL(blobUrl);
|
||||
}, 1000);
|
||||
} else {
|
||||
// 桌面端处理:使用传统的 a 标签下载
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
const generatedFilename = filename || nanoid(10) + '.wav'; // Generate a random filename
|
||||
link.download = generatedFilename;
|
||||
link.style.display = 'none';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('下载失败:', error);
|
||||
// 降级处理:直接打开链接
|
||||
window.open(url, '_blank');
|
||||
toast.info('已在新窗口打开音频文件,请手动保存');
|
||||
}
|
||||
};
|
||||
export const CreateVideos = () => {
|
||||
const [url, setUrl] = useState<string>('');
|
||||
@ -118,6 +153,8 @@ export const CreateVideos = () => {
|
||||
margin: '0 auto',
|
||||
padding: '40px 20px',
|
||||
fontFamily: 'Arial, sans-serif',
|
||||
height: '100vh',
|
||||
overflowY: 'auto',
|
||||
}}>
|
||||
<div
|
||||
style={{
|
||||
@ -318,7 +355,14 @@ export const CreateVideos = () => {
|
||||
停止
|
||||
</button>
|
||||
<button
|
||||
onClick={() => downloadAudio(url)}
|
||||
onClick={async () => {
|
||||
try {
|
||||
await downloadAudio(url);
|
||||
} catch (error) {
|
||||
console.error('下载音频时出错:', error);
|
||||
toast.error('下载失败,请稍后重试');
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
padding: '10px 20px',
|
||||
background: '#3b82f6',
|
||||
|
Loading…
x
Reference in New Issue
Block a user