音频响时无法循环播放音频

音频响时无法循环播放音频

本文介绍了音频响时无法循环播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,这个问题与我以前的问题有关 [ ^ ]

当闹铃小时与当前小时时间匹配,闹铃分钟与当前分钟时间匹配,闹铃秒与当前第二时间和闹铃日期匹配时,我可以成功发出警报当前日期

但这是我的问题,当我设置重复时间时声音不会循环播放
我正在用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
            }
        }
    }
}

推荐答案

// 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
}


这篇关于音频响时无法循环播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:51