Windows 媒体播放器中的 Wav 文件排队的项目有时无法使用 C# 正确播放

本文关键字:播放 媒体播放器 Wav 文件 项目有 排队 Windows | 更新日期: 2023-09-27 17:55:37

我正在做一个在屏幕上显示令牌的应用程序,例如 A0001 计数器 1。每个标记"A","0","0","0","0","0" "计数器" "1" 在我的项目中将 wav 文件存储在 ENGLISH 文件夹中,如 A.wav、0.wav、0.wav 等。现在我正在使用 wmplib.dll 并在 Win 2012 上运行 7 64 位。我尝试使用单线程,例如

new System.Threading.Thread(() => {
//my code here
}).Start();
Thread.Sleep(700); //I know this is not a good way to use threads. 

此代码在 Win 7 64 位上完美运行。现在,当我尝试在XP 32位上使用它时,音频有时会重叠,有时无法播放。所以我使用后台工作线程的替代方法。代码如下。

这样做的问题是排队的项目,即C:'project path'ENGLISH'A.wavC:'project path'ENGLISH'0.wav等文件的URL排队到队列中,然后添加到Windows 媒体播放器播放列表中。但是它们没有按顺序播放,有时会失去同步。

public void playAudiofile(String counter_prefix, String counter_name, String token_number)
        {
            String[] token_langs = Config.getTokenLanguages(token_number);
            /*BUZZER*/
            audio_annoucements.Enqueue(audio_file_dir  + "buzzer.wav");
            foreach (String tlang in token_langs)
            {
                String lang = tlang.Trim().ToUpper();
                try
                {
                    if (lang != "NONE")
                    {
                        /*TOKEN*/
                        audio_annoucements.Enqueue(audio_file_dir  + "Token.wav");
                        /*TOKEN NO*/
                        token_number = token_number.ToUpper();
                        char[] tokens = token_number.ToCharArray();
                        foreach (char token in tokens)
                        {
                            if (Char.IsLetter(token))
                            {
                                audio_annoucements.Enqueue(audio_file_dir  + token + ".wav");
                            }
                            else
                            {
                                audio_annoucements.Enqueue(audio_file_dir  + token + ".wav");
                            }
                        }
                        /*COUNTER*/
                        counter_prefix = counter_prefix.ToUpper();
                        audio_annoucements.Enqueue(audio_file_dir  + "COUNTER.wav");
                        /*COUNTER NAME*/
                        counter_name = counter_name.ToUpper();
                        char[] counters = counter_name.ToCharArray();
                        foreach (char counter in counters)
                        {
                            if (Char.IsLetter(counter))
                            {
                                audio_annoucements.Enqueue(audio_file_dir + counter + ".wav");
                            }
                            else
                            {
                                audio_annoucements.Enqueue(audio_file_dir+ counter + ".wav");
                            }
                        }
                    }
                }
                catch (Exception) { }
                if (!backgroundWorker1.IsBusy)
                {
                    timer1.Stop();// i turn off the timer so that backgroundworker can run properly without issues. I turn it on later. 
                    backgroundWorker1.RunWorkerAsync();
                }
            }
        }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
                String tmpPlaylist = "tmpPlaylist";
                WMPLib.IWMPPlaylist pls;
                WMPLib.IWMPPlaylistArray plItems;
                WMPLib.WindowsMediaPlayer mediaPlayer = new WMPLib.WindowsMediaPlayer();
                plItems = mediaPlayer.playlistCollection.getByName(tmpPlaylist);
                if (plItems.count == 0)
                {
                    pls = mediaPlayer.playlistCollection.newPlaylist(tmpPlaylist);
                }
                else
                {
                    pls = plItems.Item(0);
                    pls.clear();
                }
                while (audio_annoucements.Count > 0)
                {
                    WMPLib.IWMPMedia m1 = mediaPlayer.newMedia(audio_annoucements.Dequeue().ToString());
                    pls.appendItem(m1);
                }
                mediaPlayer.currentPlaylist = pls;
                mediaPlayer.controls.play();
        }
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            timer1.Start();// theres a 1 second timer in the form that checks db entries
        }        

Windows 媒体播放器中的 Wav 文件排队的项目有时无法使用 C# 正确播放

如果要每

700 毫秒执行一次,请尝试:

System.Timers.Timer t = new System.Timers.Timer(700);
t.Start()
t.Elapsed += (object s1, System.Timers.ElapsedEventArgs e1) => {
                new System.Threading.Thread(() => {
                //my code here
                }).Start();
            };