|
@@ -23,31 +23,74 @@ const AudioPlayerComponent = ({ audioPath, label, onPlay, onPause }) => {
|
23
|
23
|
});
|
24
|
24
|
|
25
|
25
|
useEffect(() => {
|
26
|
|
- if (audioPath) {
|
27
|
|
- // 设置文件路径
|
28
|
|
- setAudioFile(audioPath);
|
29
|
|
-
|
30
|
|
- // 提取文件名
|
31
|
|
- const fileName = path.basename(audioPath);
|
32
|
|
-
|
33
|
|
- // 获取文件大小信息
|
|
26
|
+ if (audioPath && typeof audioPath === 'string' && audioPath.trim() !== '') {
|
34
|
27
|
try {
|
35
|
|
- // 通过Electron的fs模块获取文件信息
|
36
|
|
- window.electron.ipcRenderer.invoke('get-file-stats', audioPath)
|
37
|
|
- .then(stats => {
|
38
|
|
- if (stats) {
|
39
|
|
- setAudioInfo({
|
40
|
|
- fileName,
|
41
|
|
- fileSize: formatFileSize(stats.size)
|
|
28
|
+ // 设置文件路径
|
|
29
|
+ setAudioFile(audioPath);
|
|
30
|
+
|
|
31
|
+ // 安全地提取文件名
|
|
32
|
+ let fileName = '未知文件';
|
|
33
|
+ try {
|
|
34
|
+ if (path && typeof path.basename === 'function') {
|
|
35
|
+ fileName = path.basename(audioPath);
|
|
36
|
+ } else {
|
|
37
|
+ // 如果path模块不可用,使用简单方法获取文件名
|
|
38
|
+ const parts = audioPath.split(/[/\\]/);
|
|
39
|
+ fileName = parts[parts.length - 1];
|
|
40
|
+ }
|
|
41
|
+ } catch (pathError) {
|
|
42
|
+ console.error('提取文件名时出错:', pathError);
|
|
43
|
+ // 使用简单方法获取文件名作为备选
|
|
44
|
+ const parts = audioPath.split(/[/\\]/);
|
|
45
|
+ fileName = parts[parts.length - 1];
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ // 获取文件大小信息
|
|
49
|
+ try {
|
|
50
|
+ // 通过Electron的fs模块获取文件信息
|
|
51
|
+ if (window.electron && window.electron.ipcRenderer) {
|
|
52
|
+ window.electron.ipcRenderer.invoke('get-file-stats', audioPath)
|
|
53
|
+ .then(stats => {
|
|
54
|
+ if (stats) {
|
|
55
|
+ setAudioInfo({
|
|
56
|
+ fileName,
|
|
57
|
+ fileSize: formatFileSize(stats.size)
|
|
58
|
+ });
|
|
59
|
+ }
|
|
60
|
+ })
|
|
61
|
+ .catch(err => {
|
|
62
|
+ console.error('获取音频文件信息时出错:', err);
|
|
63
|
+ // 设置基本信息,不包含文件大小
|
|
64
|
+ setAudioInfo({
|
|
65
|
+ fileName,
|
|
66
|
+ fileSize: '未知大小'
|
|
67
|
+ });
|
42
|
68
|
});
|
43
|
|
- }
|
44
|
|
- })
|
45
|
|
- .catch(err => {
|
46
|
|
- console.error('获取音频文件信息时出错:', err);
|
|
69
|
+ } else {
|
|
70
|
+ // 如果electron不可用,只设置文件名
|
|
71
|
+ setAudioInfo({
|
|
72
|
+ fileName,
|
|
73
|
+ fileSize: '未知大小'
|
|
74
|
+ });
|
|
75
|
+ }
|
|
76
|
+ } catch (error) {
|
|
77
|
+ console.error('处理音频文件信息时出错:', error);
|
|
78
|
+ // 设置基本信息作为备选
|
|
79
|
+ setAudioInfo({
|
|
80
|
+ fileName,
|
|
81
|
+ fileSize: '未知大小'
|
47
|
82
|
});
|
|
83
|
+ }
|
48
|
84
|
} catch (error) {
|
49
|
85
|
console.error('处理音频文件路径时出错:', error);
|
50
|
86
|
}
|
|
87
|
+ } else {
|
|
88
|
+ // 如果路径无效,重置状态
|
|
89
|
+ setAudioFile(null);
|
|
90
|
+ setAudioInfo({
|
|
91
|
+ fileName: '',
|
|
92
|
+ fileSize: ''
|
|
93
|
+ });
|
51
|
94
|
}
|
52
|
95
|
}, [audioPath]);
|
53
|
96
|
|