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); } }}
public partial class Form1 : Form { BkGrndMusic objBkMusic; int i = 0; public Form1() { InitializeComponent(); objBkMusic = new BkGrndMusic(); } private void button1_Click(object sender, EventArgs e)