{"id":165,"date":"2016-02-19T17:14:36","date_gmt":"2016-02-19T17:14:36","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=165"},"modified":"2022-02-17T06:24:14","modified_gmt":"2022-02-17T06:24:14","slug":"playing-audio-with-wmplib-windowsmediaplayer","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/audio\/playing-audio-with-wmplib-windowsmediaplayer","title":{"rendered":"Playing Audio With WMPLib.WindowsMediaPlayer"},"content":{"rendered":"<p>\n\tYou need to use WMPLib.WindowsMediaPlayer if yuo want to play non .wav files otehrwise you&#39;ll have to resort to a third party library.\n<\/p>\n<h4>\n\tPlay File Example<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/Play the file\r\n\tWMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();\r\n\twplayer.URL= TemporaryFilename;\r\n\twplayer.controls.play();\r\n<\/code><\/pre>\n<h4>\n\tPlay From&nbsp;Stream<br \/>\n<\/h4>\n<p>\n\tYou can&#39;t!\n<\/p>\n<h4>\n\tPlay Using Same Temporary File Example<br \/>\n<\/h4>\n<h5>\n\tGlobal object<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tWMPLib.WindowsMediaPlayer wplayer;\r\n<\/code><\/pre>\n<h5>\n\tPlay the file<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\/\/WindowsMediaPlayer is a problem for this application because:\r\n\t\/\/\tIt won&#39;t play from a stream\r\n\t\/\/\tSo we have to play from a temporary file, but WindowsMediaPlayer doesn&#39;t release the file quickly even after closing and killing it\r\n\t\/\/\tSo we have had to come up with a bodgit use multiple temporary files to work aroudn this issue.\r\n\r\n\t\/\/KILL WindowsMediaPlayer IF IS STILL PLAYING OR ACTIVE\r\n\tif (wplayer != null)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\twplayer.close();\r\n\t\t}\r\n\t\tcatch (Exception)\r\n\t\t{\r\n\t\t}\r\n\t\ttry\r\n\t\t{\r\n\t\t\tSystem.Diagnostics.Process[] prc = System.Diagnostics.Process.GetProcessesByName(&quot;wmplayer&quot;);\r\n\t\t\tif (prc.Length &gt; 0)\r\n\t\t\t\tprc[prc.Length - 1].Kill();\r\n\t\t}\r\n\t\tcatch (Exception)\r\n\t\t{\r\n\t\t}\r\n\t}\r\n\r\n\tint FileId;\r\n\tstring TemporaryFilename;\r\n\t\t\t\t\r\n\r\n\t\/\/Create directory if necessary\r\n\tTemporaryFilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &quot;\\\\&quot; + Application.CompanyName + &quot;\\\\&quot; + Application.ProductName + &quot;\\\\&quot;;\r\n\tif (!Directory.Exists(Path.GetDirectoryName(TemporaryFilename)))\r\n\t\tDirectory.CreateDirectory(Path.GetDirectoryName(TemporaryFilename));\r\n\r\n\t\/\/Delete any old files if they&#39;ve been released finally\r\n\tfor (FileId = 0; FileId &lt; 20; FileId++)\r\n\t{\r\n\t\tTemporaryFilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &quot;\\\\&quot; + Application.CompanyName + &quot;\\\\&quot; + Application.ProductName + &quot;\\\\temp_play_file&quot; + FileId + &quot;.mp3&quot;;\r\n\t\tif (File.Exists(TemporaryFilename))\r\n\t\t{\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tFile.SetAttributes(TemporaryFilename, FileAttributes.Normal);\r\n\t\t\t\tif ((File.GetAttributes(TemporaryFilename) &amp; FileAttributes.ReadOnly) == FileAttributes.ReadOnly)\r\n\t\t\t\t\tFile.SetAttributes(TemporaryFilename, FileAttributes.Normal);\r\n\r\n\t\t\t\tFile.Delete(TemporaryFilename);\r\n\t\t\t}\r\n\t\t\tcatch (Exception)\r\n\t\t\t{\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t\/\/Write the file\r\n\tfor (FileId = 0; FileId &lt; 20; FileId++)\r\n\t{\r\n\t\tTemporaryFilename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + &quot;\\\\&quot; + Application.CompanyName + &quot;\\\\&quot; + Application.ProductName + &quot;\\\\temp_play_file&quot; + FileId + &quot;.mp3&quot;;\r\n\t\tif (!File.Exists(TemporaryFilename))\r\n\t\t{\r\n\t\t\t\/\/Write the file\r\n\t\t\tFile.WriteAllBytes(TemporaryFilename, SelectedCardAudioFileData);\r\n\t\t\tFile.SetAttributes(TemporaryFilename, FileAttributes.Normal);\r\n\r\n\t\t\t\/\/Play the file\r\n\t\t\twplayer = new WMPLib.WindowsMediaPlayer();\r\n\t\t\twplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);\r\n\t\t\twplayer.URL= TemporaryFilename;\r\n\t\t\twplayer.controls.play();\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n<h5>\n\tMethod to handle end of playback<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/*************************************************\r\n\t\/\/*************************************************\r\n\t\/\/********** MP3 PLAYER FINISHED PLAYING **********\r\n\t\/\/*************************************************\r\n\t\/\/*************************************************\r\n\tvoid wplayer_PlayStateChange(int NewState)\r\n\t{\r\n\t\tif (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded)\r\n\t\t{\r\n\t\t\twplayer.close();\r\n\t\t\twplayer = null;\r\n\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tSystem.Diagnostics.Process[] prc = System.Diagnostics.Process.GetProcessesByName(&quot;wmplayer&quot;);\r\n\t\t\t\tif (prc.Length &gt; 0)\r\n\t\t\t\t\tprc[prc.Length - 1].Kill();\r\n\t\t\t}\r\n\t\t\tcatch (Exception)\r\n\t\t\t{\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You need to use WMPLib.WindowsMediaPlayer if yuo want to play non .wav files otehrwise you&#39;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&nbsp;Stream You can&#39;t! Play Using Same Temporary File Example Global object WMPLib.WindowsMediaPlayer wplayer; Play the file \/\/WindowsMediaPlayer [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-audio"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/165\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}