Repository navigation
Expand file tree
/
Copy pathAddEditBroadcastWindow.xaml.cs
More file actions
519 lines (452 loc) · 19 KB
/
Copy pathAddEditBroadcastWindow.xaml.cs
File metadata and controls
519 lines (452 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
using Microsoft.Win32;
using NAudio.Wave;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace WpfApp1
{
/// <summary>
/// AddEditBroadcastWindow.xaml 的交互逻辑
/// </summary>
public partial class AddEditBroadcastWindow : Window
{
public BroadcastItem EditedItem { get; private set; }
private IWavePlayer _wavePlayer;
private AudioFileReader? _audioFileReader;
private bool _isPlaying = false;
private BroadcastItem? _originalItem; // 保存原始项用于比较
// 添加计时器和循环计数
private DispatcherTimer _playbackTimer;
private int _currentLoopCount = 0;
private int _totalLoopCount = 1;
private static float VolumeSliderToScalar(double volume)
{
return Math.Max(0.0f, Math.Min(1.0f, (float)(volume / 100.0)));
}
public AddEditBroadcastWindow(BroadcastItem? item = null)
{
InitializeComponent();
// 初始化时间选择器
InitializeTimeComboBoxes();
_wavePlayer = new WaveOutEvent();
_wavePlayer.PlaybackStopped += WavePlayer_PlaybackStopped;
// 初始化播放计时器
_playbackTimer = new DispatcherTimer();
_playbackTimer.Interval = TimeSpan.FromMilliseconds(500); // 半秒更新一次
_playbackTimer.Tick += PlaybackTimer_Tick;
// 如果传入了广播项,则进入编辑模式
if (item != null)
{
EditedItem = item;
_originalItem = item; // 保存原始项引用
LoadItemData();
}
else
{
// 新建模式
EditedItem = new BroadcastItem();
EditedItem.TimeString = "08:00:00"; // 默认时间
EditedItem.Volume = 80;
EditedItem.Status = BroadcastStatuses.Ready;
// 设置默认时间
HourComboBox.SelectedItem = 8;
MinuteComboBox.SelectedItem = 0;
}
}
private void InitializeTimeComboBoxes()
{
// 小时选择器 (0-23)
for (int i = 0; i < 24; i++)
{
HourComboBox.Items.Add(i);
}
// 分钟选择器 (0-59)
for (int i = 0; i < 60; i++)
{
MinuteComboBox.Items.Add(i);
}
}
private void LoadItemData()
{
// 解析时间字符串
if (TimeSpan.TryParse(EditedItem.TimeString, out TimeSpan time))
{
HourComboBox.SelectedItem = time.Hours;
MinuteComboBox.SelectedItem = time.Minutes;
}
else
{
HourComboBox.SelectedItem = 8;
MinuteComboBox.SelectedItem = 0;
}
DescriptionTextBox.Text = EditedItem.Description;
MondayCheckBox.IsChecked = EditedItem.Monday;
TuesdayCheckBox.IsChecked = EditedItem.Tuesday;
WednesdayCheckBox.IsChecked = EditedItem.Wednesday;
ThursdayCheckBox.IsChecked = EditedItem.Thursday;
FridayCheckBox.IsChecked = EditedItem.Friday;
SaturdayCheckBox.IsChecked = EditedItem.Saturday;
SundayCheckBox.IsChecked = EditedItem.Sunday;
FilePathTextBox.Text = EditedItem.FilePath;
DurationTextBox.Text = EditedItem.Duration;
VolumeSlider.Value = EditedItem.Volume;
LoopCountTextBox.Text = EditedItem.LoopCount.ToString();
}
private void WavePlayer_PlaybackStopped(object? sender, StoppedEventArgs e)
{
Dispatcher.Invoke(() =>
{
// 增加循环计数
_currentLoopCount++;
// 检查是否需要继续循环播放
if (_isPlaying && _currentLoopCount < _totalLoopCount)
{
try
{
if (System.IO.File.Exists(FilePathTextBox.Text))
{
// 重新播放
if (_audioFileReader != null)
{
_audioFileReader.Dispose();
}
_audioFileReader = new AudioFileReader(FilePathTextBox.Text);
_audioFileReader.Volume = VolumeSliderToScalar(VolumeSlider.Value);
_wavePlayer.Init(_audioFileReader);
_wavePlayer.Play();
// 更新显示
RemainingDurationTextBlock.Text = TimeFormatHelper.FormatDuration(_audioFileReader.TotalTime);
RemainingLoopCountTextBlock.Text = (_totalLoopCount - _currentLoopCount).ToString();
return;
}
}
catch
{
// 播放失败,结束循环
}
}
// 循环结束或播放失败
_isPlaying = false;
TestPlayButton.Content = "测试播放";
if (_audioFileReader != null)
{
_audioFileReader.Dispose();
_audioFileReader = null;
}
// 停止计时器并重置显示
_playbackTimer.Stop();
RemainingDurationTextBlock.Text = "--:--";
RemainingLoopCountTextBlock.Text = "--";
});
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
// 如果正在播放,先停止播放
if (_isPlaying)
{
StopCurrentPlayback();
_isPlaying = false;
TestPlayButton.Content = "测试播放";
// 停止计时器并重置显示
_playbackTimer.Stop();
RemainingDurationTextBlock.Text = "--:--";
RemainingLoopCountTextBlock.Text = "--";
}
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "音频文件|*.mp3;*.wav;*.wma|所有文件|*.*",
Title = "选择音频文件"
};
if (openFileDialog.ShowDialog() == true)
{
FilePathTextBox.Text = openFileDialog.FileName;
// 获取音频文件时长
try
{
using (var reader = new AudioFileReader(openFileDialog.FileName))
{
TimeSpan duration = reader.TotalTime;
DurationTextBox.Text = TimeFormatHelper.FormatDuration(duration);
}
}
catch
{
DurationTextBox.Text = BroadcastStatuses.UnknownDuration;
}
}
}
private void TestPlayButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(FilePathTextBox.Text))
{
MessageBox.Show("请先选择一个音频文件", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (!_isPlaying)
{
try
{
if (!System.IO.File.Exists(FilePathTextBox.Text))
{
MessageBox.Show("文件不存在,请重新选择", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 停止任何正在进行的播放
StopCurrentPlayback();
// 开始新的播放
_audioFileReader = new AudioFileReader(FilePathTextBox.Text);
_audioFileReader.Volume = VolumeSliderToScalar(VolumeSlider.Value);
_wavePlayer.Init(_audioFileReader);
_wavePlayer.Play();
// 更新状态和UI
_isPlaying = true;
TestPlayButton.Content = "停止播放";
// 重置循环计数
_currentLoopCount = 0;
_totalLoopCount = int.Parse(LoopCountTextBox.Text);
// 启动计时器
_playbackTimer.Start();
// 更新显示
RemainingDurationTextBlock.Text = TimeFormatHelper.FormatDuration(_audioFileReader.TotalTime);
RemainingLoopCountTextBlock.Text = _totalLoopCount.ToString();
}
catch (Exception ex)
{
MessageBox.Show($"播放错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
// 停止播放
StopCurrentPlayback();
// 更新状态和UI
_isPlaying = false;
TestPlayButton.Content = "测试播放";
// 停止计时器并重置显示
_playbackTimer.Stop();
RemainingDurationTextBlock.Text = "--:--";
RemainingLoopCountTextBlock.Text = "--";
}
}
private void VolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (_audioFileReader != null && _isPlaying)
{
_audioFileReader.Volume = VolumeSliderToScalar(VolumeSlider.Value);
}
}
private void StopCurrentPlayback()
{
try
{
// 停止播放计时器
if (_playbackTimer != null && _playbackTimer.IsEnabled)
{
_playbackTimer.Stop();
}
// 停止播放
if (_wavePlayer != null)
{
if (_wavePlayer.PlaybackState != PlaybackState.Stopped)
{
_wavePlayer.Stop();
}
}
// 释放音频资源
if (_audioFileReader != null)
{
_audioFileReader.Dispose();
_audioFileReader = null;
}
}
catch (Exception ex)
{
// 静默处理异常,确保UI能继续运行
System.Diagnostics.Debug.WriteLine($"停止播放时出错: {ex.Message}");
}
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
// 如果正在播放,先停止播放
if (_isPlaying)
{
StopCurrentPlayback();
_isPlaying = false;
TestPlayButton.Content = "测试播放";
// 停止计时器并重置显示
_playbackTimer.Stop();
RemainingDurationTextBlock.Text = "--:--";
RemainingLoopCountTextBlock.Text = "--";
}
// 表单验证
if (string.IsNullOrEmpty(DescriptionTextBox.Text))
{
MessageBox.Show("请输入广播描述", "数据错误", MessageBoxButton.OK, MessageBoxImage.Warning);
DescriptionTextBox.Focus();
return;
}
if (string.IsNullOrEmpty(FilePathTextBox.Text))
{
MessageBox.Show("请选择音频文件", "数据错误", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (!System.IO.File.Exists(FilePathTextBox.Text))
{
MessageBox.Show("所选文件不存在,请重新选择", "数据错误", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
int loopCount;
if (!int.TryParse(LoopCountTextBox.Text, out loopCount) || loopCount < 1)
{
MessageBox.Show("循环次数必须是大于0的整数", "数据错误", MessageBoxButton.OK, MessageBoxImage.Warning);
LoopCountTextBox.Focus();
return;
}
// 处理小时和分钟
int hour = (int)HourComboBox.SelectedItem;
int minute = (int)MinuteComboBox.SelectedItem;
string timeString = $"{hour:00}:{minute:00}:00";
// 如果是编辑模式,并且修改了时间,检查是否有冲突(可选)
if (_originalItem != null && _originalItem.TimeString != timeString)
{
if (HasTimeConflict(timeString))
{
if (MessageBox.Show("这个时间点已经有一个广播,确定要创建重复时间点的广播吗?",
"时间冲突", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
{
return;
}
}
}
// 创建或更新广播项
if (_originalItem == null)
{
// 新建项
EditedItem = new BroadcastItem();
}
// 更新属性
EditedItem.TimeString = timeString;
EditedItem.Description = DescriptionTextBox.Text;
EditedItem.FilePath = FilePathTextBox.Text;
EditedItem.Duration = DurationTextBox.Text;
EditedItem.LoopCount = loopCount;
EditedItem.Volume = (int)VolumeSlider.Value;
EditedItem.Monday = MondayCheckBox.IsChecked == true;
EditedItem.Tuesday = TuesdayCheckBox.IsChecked == true;
EditedItem.Wednesday = WednesdayCheckBox.IsChecked == true;
EditedItem.Thursday = ThursdayCheckBox.IsChecked == true;
EditedItem.Friday = FridayCheckBox.IsChecked == true;
EditedItem.Saturday = SaturdayCheckBox.IsChecked == true;
EditedItem.Sunday = SundayCheckBox.IsChecked == true;
EditedItem.Status = BroadcastStatuses.Ready;
// 关闭窗口
DialogResult = true;
}
private bool HasTimeConflict(string timeString)
{
// 此处仅为示例,实际应修改为检查主窗口中的现有项目
// 如果有必要,可以传入父窗口中的广播列表进行检查
// 简单实现可以从配置中加载现有项目进行比较
// 这里仅做演示
return false;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
// 如果正在播放,先停止播放
if (_isPlaying)
{
StopCurrentPlayback();
_isPlaying = false;
}
// 取消操作,关闭窗口
DialogResult = false;
}
private void AllDaysCheckBox_Checked(object sender, RoutedEventArgs e)
{
// 设置所有日期选择框
bool isChecked = ((CheckBox)sender).IsChecked == true;
MondayCheckBox.IsChecked = isChecked;
TuesdayCheckBox.IsChecked = isChecked;
WednesdayCheckBox.IsChecked = isChecked;
ThursdayCheckBox.IsChecked = isChecked;
FridayCheckBox.IsChecked = isChecked;
SaturdayCheckBox.IsChecked = isChecked;
SundayCheckBox.IsChecked = isChecked;
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
try
{
// 如果正在播放,停止播放
if (_isPlaying)
{
_isPlaying = false;
}
// 停止当前播放并释放资源
StopCurrentPlayback();
// 释放播放器资源
if (_wavePlayer != null)
{
_wavePlayer.Dispose();
_wavePlayer = null;
}
// 停止计时器
if (_playbackTimer != null && _playbackTimer.IsEnabled)
{
_playbackTimer.Stop();
}
}
catch (Exception ex)
{
// 静默处理异常,确保窗口能正常关闭
System.Diagnostics.Debug.WriteLine($"窗口关闭时出错: {ex.Message}");
}
base.OnClosing(e);
}
private void WeekdaysCheckBox_Checked(object sender, RoutedEventArgs e)
{
// 设置工作日
MondayCheckBox.IsChecked = true;
TuesdayCheckBox.IsChecked = true;
WednesdayCheckBox.IsChecked = true;
ThursdayCheckBox.IsChecked = true;
FridayCheckBox.IsChecked = true;
SaturdayCheckBox.IsChecked = false;
SundayCheckBox.IsChecked = false;
}
private void NumberValidationTextBox(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}
private bool IsTextAllowed(string text)
{
int value;
return int.TryParse(text, out value);
}
private void PlaybackTimer_Tick(object? sender, EventArgs e)
{
if (_isPlaying && _audioFileReader != null)
{
TimeSpan currentPosition = _audioFileReader.CurrentTime;
TimeSpan totalTime = _audioFileReader.TotalTime;
TimeSpan remaining = totalTime - currentPosition;
// 更新UI显示剩余时间
RemainingDurationTextBlock.Text = TimeFormatHelper.FormatDuration(remaining);
}
}
private void FilePathTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
// 如果文件路径发生变化且当前正在播放,则自动停止播放
if (_isPlaying)
{
StopCurrentPlayback();
_isPlaying = false;
TestPlayButton.Content = "测试播放";
// 停止计时器并重置显示
_playbackTimer.Stop();
RemainingDurationTextBlock.Text = "--:--";
RemainingLoopCountTextBlock.Text = "--";
}
}
}
}