You need to use WMPLib.WindowsMediaPlayer if yuo want to play non .wav files otehrwise you'll have to resort to a third party library.

Play File Example


	//Play the file
	WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
	wplayer.URL= TemporaryFilename;
	wplayer.controls.play();

Play From Stream

You can't!

Play Using Same Temporary File Example

Global object

	WMPLib.WindowsMediaPlayer wplayer;
Play the file

//WindowsMediaPlayer is a problem for this application because:
	//	It won't play from a stream
	//	So we have to play from a temporary file, but WindowsMediaPlayer doesn't release the file quickly even after closing and killing it
	//	So we have had to come up with a bodgit use multiple temporary files to work aroudn this issue.

	//KILL WindowsMediaPlayer IF IS STILL PLAYING OR ACTIVE
	if (wplayer != null)
	{
		try
		{
			wplayer.close();
		}
		catch (Exception)
		{
		}
		try
		{
			System.Diagnostics.Process[] prc = System.Diagnostics.Process.GetProcessesByName("wmplayer");
			if (prc.Length > 0)
				prc[prc.Length - 1].Kill();
		}
		catch (Exception)
		{
		}
	}

	int FileId;
	string TemporaryFilename;
				

	//Create directory if necessary
	TemporaryFilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + Application.CompanyName + "\\" + Application.ProductName + "\\";
	if (!Directory.Exists(Path.GetDirectoryName(TemporaryFilename)))
		Directory.CreateDirectory(Path.GetDirectoryName(TemporaryFilename));

	//Delete any old files if they've been released finally
	for (FileId = 0; FileId < 20; FileId++)
	{
		TemporaryFilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + Application.CompanyName + "\\" + Application.ProductName + "\\temp_play_file" + FileId + ".mp3";
		if (File.Exists(TemporaryFilename))
		{
			try
			{
				File.SetAttributes(TemporaryFilename, FileAttributes.Normal);
				if ((File.GetAttributes(TemporaryFilename) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
					File.SetAttributes(TemporaryFilename, FileAttributes.Normal);

				File.Delete(TemporaryFilename);
			}
			catch (Exception)
			{
			}
		}
	}

	//Write the file
	for (FileId = 0; FileId < 20; FileId++)
	{
		TemporaryFilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + Application.CompanyName + "\\" + Application.ProductName + "\\temp_play_file" + FileId + ".mp3";
		if (!File.Exists(TemporaryFilename))
		{
			//Write the file
			File.WriteAllBytes(TemporaryFilename, SelectedCardAudioFileData);
			File.SetAttributes(TemporaryFilename, FileAttributes.Normal);

			//Play the file
			wplayer = new WMPLib.WindowsMediaPlayer();
			wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);
			wplayer.URL= TemporaryFilename;
			wplayer.controls.play();
			break;
		}
	}
Method to handle end of playback

	//*************************************************
	//*************************************************
	//********** MP3 PLAYER FINISHED PLAYING **********
	//*************************************************
	//*************************************************
	void wplayer_PlayStateChange(int NewState)
	{
		if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded)
		{
			wplayer.close();
			wplayer = null;

			try
			{
				System.Diagnostics.Process[] prc = System.Diagnostics.Process.GetProcessesByName("wmplayer");
				if (prc.Length > 0)
					prc[prc.Length - 1].Kill();
			}
			catch (Exception)
			{
			}
		}
	}

 

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *