音频响时无法循环播放音频 [英] Can't loop audio when audio is ringing

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

问题描述

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

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
}


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆