I am doing an application which shows tokens on screen like A0001 Counter 1. Each of the tokens "A","0","0","0","0" "Counter" "1" has wav files stored in a folder ENGLISH in my project as A.wav, 0.wav,0.wav etc. Now I'm using wmplib.dll and running vs 2012 on Win 7 64 bit. I tried using single threading like
new System.Threading.Thread(() => {
//my code here
}).Start();
Thread.Sleep(700); //I know this is not a good way to use threads.
This code works on Win 7 64 bit perfectly. Now when I try to use it on XP 32 bit the audio sometimes overlaps and sometimes doesn't play. So I used an alternative way using backgroundworker thread. The code is as below.
Issue with this is that the queued items ie. the URL of the files like C:\project path\ENGLISH\A.wav
, C:\project path\ENGLISH\0.wav
etc. are queued to the queue and then added to Windows Media player playlist. But they are not playing in order and going off sync at times.
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
}