Click here to Skip to main content
15,906,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// CLASS: BackGroundMusicGP
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace BackGroundMusicGP
{
    class BkGrndMusic
    {
      private string _command;
     private bool isOpen;
  [DllImport("winmm.dll")]

  private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);

  public void Close()
  {
     _command = "close MediaFile";
     mciSendString(_command, null, 0, IntPtr.Zero);
     isOpen = false;
  }

  public void Open(string sFileName)
  {
     _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
     mciSendString(_command, null, 0, IntPtr.Zero);
     isOpen = true;
  }

  public void Play(bool loop)
  {
     if(isOpen)
     {
        _command = "play MediaFile";
        if (loop)
         _command += " REPEAT";
        mciSendString(_command, null, 0, IntPtr.Zero);
     }
      }
  public void playMusic()
  {
      string path = "E:\\abc\\Habib.mp3";
       string filename = Path.GetFileName(path);
      Open(filename);
      Play(false);
  }
    }
}

///////////////////////////
C#
public partial class Form1 : Form
   {
       BkGrndMusic objBkMusic;
       int i = 0;
       public Form1()
       {
           InitializeComponent();
           objBkMusic = new BkGrndMusic();
       }
       private void button1_Click(object sender, EventArgs e)
        {
           Thread t = new Thread(new ThreadStart(objBkMusic.playMusic));
            t.Start();  // it doesn't work
           //objBkMusic.playMusic(); // it works
         }


How can i play background music using thread? when i call directly using object of the class then it works. But when i call using thread it doesn't work.
Posted
Updated 18-Oct-10 3:39am
v2
Comments
TheyCallMeMrJames 18-Oct-10 9:57am    
Are you getting an error? Set some breakpoints in the code (particularly in playMusic) and see what's happening.

new Thread(new ThreadStart(objBkMusic.playMusic)).Start(); should normally work fine, but there is a cache. But there are now messages from the message queue ignored by your thread. The UI thread handles these messages by default but for a non UI thread it is not. You cab look at the link below:

http://www.pinvoke.net/default.aspx/winmm.mcisendstring[^]

You probably need to pass it the handle of your form, like this:

mciSendString(_command, null, 0, Form1.Handle);

http://msdn.microsoft.com/en-us/library/dd757161%28VS.85%29[^]

You would of course need a property in your BkGrndMusic class that can hold this handle, but you probably already got that ;-)

Good luck!
 
Share this answer
 
No error messages. But no music heard.

@E.F. Nijboer
"You would of course need a property in your BkGrndMusic class that can hold this handle, but you probably already got that"

i don't have sufficient knowledge on that.could you please provide code. where of my code should i change?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900