实际上,这个问题与我以前的问题有关提醒(警报)从db获取时间和日期,并与当前时间和当前日期匹配 [ ^ ] 当闹铃小时与当前小时时间匹配,闹铃分钟与当前分钟时间匹配,闹铃秒与当前第二时间和闹铃日期匹配时,我可以成功发出警报当前日期 但这是我的问题,当我设置重复时间时声音不会循环播放 我正在用naudio来发声 我尝试过的事情:
Actually this problem has relationship with my previous question Reminder (alarm) get time and day from db and matching with the current time and current day[^] I have successes to sound alarm when the hour of alarm match with the current hour time, and when the minute of alarm match with the current minute time, and the second of alarm also match with the current second time and also the day of alarm match with current day but this is my problem when i set repeat time the sound won''t be looped I''m using naudio to sound the time What I have tried:
private void timer_tick(object sender, EventArgs e) { DateTime now = DateTime.Now; alarmlbl.Content = now.ToLongTimeString(); var alarms = ac.alarm(); foreach (var alarm in alarms) { string path = alarm.path; if (alarm.time.Hour == now.Hour && alarm.time.Minute == now.Minute && alarm.time.Second == now.Second) { for (int i = 0; i < alarm.totalbunyi; ++i) { //MessageBox.Show("Hello"); audioPlayer.LoadFile(path); audioPlayer.Play(); This is my problem it wont be looped but when i use messagebox it can loops } } } }推荐答案
是重复(可能),只是没有循环! .Play()方法不会阻塞,因此它会循环执行适当的次数,并且每次都会重新开始播放.这是如此之快,以至于好像只播放一次. (最后一次通过循环.) 偷取者!我从没用过NAudio. 您需要等待PlaybackStopped被触发".像这样的东西: It is repeating (probably), just not looping! The .Play() method isn''t blocking so it goes through the loop the appropriate number of times and restarts playing each time. This is so fast that it seems like it is only playing once. (The last time through the loop.) Caveat emptor! I''ve never used NAudio. You''ll need to wait on the PlaybackStopped to be "fired". Something like: // pick a value that is long enough to avoid interrupting the playback but still prevents total lockup! private static readonly TimeSpan MaximumPlaybackWait = TimeSpan.FromSeconds(120); private System.Threading.AutoResetEvent PlaybackCompleted = new System.Threading.AutoResetEvent(false); // when setting up the audioPlayer (WaveOut ? class) something like: audioPlayer.PlaybackStopped += audioPlayer_PlaybackStopped; void audioPlayer_PlaybackStopped(object sender, StoppedEventArgs e) { PlaybackCompleted.Set(); }
for (int i = 0; i < alarm.totalbunyi; ++i) { //MessageBox.Show("Hello"); audioPlayer.LoadFile(path); audioPlayer.Play(); PlaybackCompleted.WaitOne(MaximumPlaybackWait); PlaybackCompleted.Reset(); // just in case we're here by timeout }
更多推荐
音频响时无法循环播放音频
发布评论