我正在做一个在A0001计数器1屏幕上显示令牌的应用程序。每个令牌“A”,“0”,“0”,“0”,“0”“计数器”“1”都有wav文件存储在我的项目中的ENGLISH文件夹中,如A.wav,0.wav,0.wav等。现在我正在使用wmplib.dll并在Win 7 64位上运行vs 2012。我尝试使用像
这样的单线程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位上使用它时,音频有时会重叠,有时甚至不会播放。所以我使用了一种使用backgroundworker线程的替代方法。代码如下。
问题是排队的项目即。像C:\project path\ENGLISH\A.wav
,C:\project path\ENGLISH\0.wav
等文件的URL排队到队列,然后添加到Windows Media播放器播放列表。但他们不是按顺序播放,有时不同步。
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
}
答案 0 :(得分:0)
如果你想每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();
};