0

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.

AJ152
  • 671
  • 2
  • 12
  • 28
  • Are you sure you need a BackgroundWorker? Documentation for `play` shows simply calling it from the UI thread: http://msdn.microsoft.com/en-us/library/dd564746(v=vs.85).aspx which suggests it's asynchronous. Also, if it's an activex control then you can't simply use it on another thread--you'll have to initialize it on that new thread--which isn't really recommended for thread pool threads – Peter Ritchie Apr 21 '14 at 20:17
  • The UI I have it's causing the audio playback to freeze up or stalls when loading big chunks of data inside the same Thread UI, and I would prefer to have that audio process running on a separate thread, that why I though of BackgroundWorker. @PeterRitchie – AJ152 Apr 21 '14 at 20:27
  • 1
    I wouldn't recommend a BackgroundWorker, you'd likely have to manually create thread (ensure it's STA) and initialize the ActiveX control within that new thread. Doing stuff like that with threadpool threads (which is what the BackgroundWorker users) causes problems with the threadpool as that thread woul get reused by something else... – Peter Ritchie Apr 21 '14 at 20:43
  • This question might be helpful: http://stackoverflow.com/questions/15025626/playing-a-mp3-file-in-a-winform-application – Peter Ritchie Apr 21 '14 at 20:45
  • Are you sure it plays correctly if you launch it from a worker thread? I can be wrong, but if my memory does not fails, wmp cannot be launched on a worker thread, it must be on main thread always (you are using the activex control set in your dessigner, so I suppose the call is being derived to main thread, try to create it on the worker thread, if I'm correct it will crash after start playing). – Gusman Apr 21 '14 at 21:12

1 Answers1

0

The UI I have it's causing the audio playback to freeze up or stalls when loading big chunks of data inside the same Thread UI, and I would prefer to have that audio process running on a separate thread, that why I though of BackgroundWorker.

You should rather use BackgroundWorker (or better yet, Task.Run or naturally async IO APIs) to load big chunks of data in the background and keep the main UI thread lag-free. Then create and use the WMP control on the UI thread.

If you can't refactor the code this way and want to use a background thread for WPM, keep in mind such thread has to pump Windows messages, otherwise the WPM control may not function properly. For this purpose, you could use my ThreadWithAffinityContext from here.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486