I need some help setting up a BackgroundWorker process for a Windows Media Player audio. I need to run the audio (WMP) directly from the BackgroundWorker, not from the main Thread, and that background process needs to remain opened until the end of the audio file, but even though the audio starts playing normal on PLAY, the BackgroundWorker stops, and therefore I don't think the audio is actually playing on that second Thread or backgroundWorker as is already closed.
The question I have is, how I can play this audio file using Windows Media Player (WMPLib) from a backgroundWorker that will remain opened until the end of the song?
using WMPLib;
namespace mediaplayer
{
public partial class MainWindow : Window
{
BackgroundWorker m_audioProcessingWorker;
public MainWindow()
{
InitializeComponent();
}
string filename = @"C:\audio\song1.mp3"
private void button_play_Click(object sender, EventArgs e)
{
// Create the Audio Processing Worker (Thread)
m_audioProcessingWorker = new BackgroundWorker();
m_audioProcessingWorker.DoWork += new DoWorkEventHandler(audioProcessingWorker_DoWork);
m_audioProcessingWorker.RunWorkerAsync();
m_audioProcessingWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(audioProcessingWorker_Completed);
}
private void audioProcessingWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
axWindowsMediaPlayer1.URL = filename;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void audioProcessingWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Audio is finished");
}
private void button_stop_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
m_audioProcessingWorker.CancelAsync();
}
private void button_pause_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
}
}
Thanks.