<p>Is there a portable, not patent-restricted way to play compressed sound files in C# / .Net? I want to play short "jingle" sounds on various events occuring in the program.</p>

<p><a href="http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx" rel="noreferrer">System.Media.SoundPlayer</a> can handle only WAV, but those are typically to big to embed in a downloadable apllication. MP3 is protected with patents, so even if there was a fully managed decoder/player it wouldn't be free to redistribute. The best format available would seem to be OGG Vorbis, but I had no luck getting any C# Vorbis libraries to work (I managed to extract a raw PCM with <a href="https://github.com/mono/csvorbis" rel="noreferrer">csvorbis</a> but I don't know how to play it afterwards).</p>

<p>I neither want to distribute any binaries with my application nor depend on P/Invoke, as the project should run at least on Windows and Linux. I'm fine with bundling .Net assemblies as long as they are license-compatible with GPL.</p>

<p>[this question is a follow up to a <a href="http://lists.ximian.com/pipermail/mono-devel-list/2007-June/023863.html" rel="noreferrer">mailing list discussion on mono-dev</a> mailing list a year ago]</p>

[{'answer_id': 36050, 'author': 'skolima', 'author_id': 3205, 'author_profile': 'https://Stackoverflow.com/users/3205', 'pm_score': 4, 'selected': True, 'text': '<p>I finally revisited this topic, and, using help from <a href="https://stackoverflow.com/a/7152153/3205">BrokenGlass on writing WAVE header</a>, updated csvorbis. I\'ve added an <a href="https://github.com/mono/csvorbis/blob/master/OggDecoder/OggDecodeStream.cs" rel="nofollow noreferrer">OggDecodeStream</a> that can be passed to <code>System.Media.SoundPlayer</code> to simply play any (compatible) Ogg Vorbis stream. Example usage:</p>\n\n<pre><code>using (var file = new FileStream(oggFilename, FileMode.Open, FileAccess.Read))\n{\n  var player = new SoundPlayer(new OggDecodeStream(file));\n  player.PlaySync();\n}\n</code></pre>\n\n<p>\'Compatible\' in this case means \'it worked when I tried it out\'. The decoder is fully managed, works fine on Microsoft .Net - at the moment, there seems to be a regression in Mono\'s <code>SoundPlayer</code> that causes distortion.</p>\n\n<p>Outdated:</p>\n\n<p><s> <code>System.Diagnostics.Process.Start("fullPath.mp3");</code></p>\n\n<p>I am surprised but the <a href="https://stackoverflow.com/questions/35896/how-can-i-play-compressed-sound-files-in-c-in-a-portable-way#35987">method Dinah mentioned</a> actually works. However, I was thinking about playing short "jingle" sounds on various events occurring in the program, I don\'t want to launch user\'s media player each time I need to do a \'ping!\' sound.</p>\n\n<p>As for the code project link - this is unfortunately only a P/Invoke wrapper.</s></p>\n'}, {'answer_id': 130628, 'author': 'Trap', 'author_id': 7839, 'author_profile': 'https://Stackoverflow.com/users/7839', 'pm_score': 1, 'selected': False, 'text': "<p>Calling something which is located in 'System.Diagnostics' to play a sound looks like a pretty bad idea to me. Here is what that function is meant for:</p>\n\n<pre><code>    //\n    // Summary:\n    //     Starts a process resource by specifying the name of a document or application\n    //     file and associates the resource with a new System.Diagnostics.Process component.\n    //\n    // Parameters:\n    //   fileName:\n    //     The name of a document or application file to run in the process.\n    //\n    // Returns:\n    //     A new System.Diagnostics.Process component that is associated with the process\n    //     resource, or null, if no process resource is started (for example, if an\n    //     existing process is reused).\n    //\n    // Exceptions:\n    //   System.ComponentModel.Win32Exception:\n    //     There was an error in opening the associated file.\n    //\n    //   System.ObjectDisposedException:\n    //     The process object has already been disposed.\n    //\n    //   System.IO.FileNotFoundException:\n    //     The PATH environment variable has a string containing quotes.\n</code></pre>\n"}, {'answer_id': 939155, 'author': 'K M', 'author_id': 114272, 'author_profile': 'https://Stackoverflow.com/users/114272', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>I neither want to distribute any\n  binaries with my application nor\n  depend on P/Invoke, as the project\n  should run at least on Windows and\n  Linux. I\'m fine with bundling .Net\n  assemblies as long as they are\n  license-compatible with GPL.</p>\n</blockquote>\n\n<p>Unfortunatly its going to be impossible to avoid distributing binaries, or avoid P/Invoke.  The .net class libraries use P/Invoke underneath anyway, the managed code has to communicate with the unmanage operating system API at some point, in order to do anything.</p>\n\n<p>Converting the OGG file to PCM should be possible in Managed code, but because there is no Native Support for Audio in .net, you really have 3 options:</p>\n\n<ol>\n<li><p>Call an external program to play the sound (as suggested earlier)</p></li>\n<li><p>P/Invoke a C module to play the sound</p></li>\n<li><p>P/Invoke the OS APIs to play the sound.</p></li>\n</ol>\n\n<p>(4.) If you\'re only running this code on windows you could probably just use DirectShow.  </p>\n\n<p>P/Invoke can be used in a cross platform way\n<a href="http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names" rel="nofollow noreferrer">http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names</a></p>\n\n<p>Once you have your PCM data (using a OGG C Lib or Managed Code, something like this <a href="http://www.robburke.net/mle/mp3sharp/" rel="nofollow noreferrer">http://www.robburke.net/mle/mp3sharp/</a> of course there are licencing issues with MP3), you will need a way to play it, unfortunatly .net does not provide any direct assess to your sound card or methods to play streaming audio.  You could convert the ogg files to PCM at startup, and then use System.Media.SoundPlayer, to play the wav files generated.  The current method Microsoft suggests uses P/Invoke to access Sound playing API in the OS <a href="http://msdn.microsoft.com/en-us/library/ms229685.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms229685.aspx</a></p>\n\n<p>A cross platform API to play PCM sound is OpenAL and you should be able to play (PCM) sound using the c# bindings for OpenAL at www.taoframework.com, you will unfortunatly need to copy a number of DLL and .so files with your application in order for it to work when distributed, but this is, as i\'ve explained earlier unavoidable.</p>\n'}, {'answer_id': 1586278, 'author': 'Sergio Tapia', 'author_id': 112355, 'author_profile': 'https://Stackoverflow.com/users/112355', 'pm_score': -1, 'selected': False, 'text': '<p>There is no way for you to do this without using something else for your play handling.</p>\n\n<p>Using the System.Diagnostic will launch an external software and I doubt you want that, right? You just want X sound file to play in the background when Y happens in your program, right?</p>\n\n<p>Voted up because it looks like an interesting question. :D</p>\n'}, {'answer_id': 2474535, 'author': 'user217299', 'author_id': 217299, 'author_profile': 'https://Stackoverflow.com/users/217299', 'pm_score': 1, 'selected': False, 'text': '<p>i think you should have a look a fmod, which is the mother of all audio api</p>\n\n<p>please feel free to dream about <a href="http://www.fmod.org/index.php/download#FMODExProgrammersAPI" rel="nofollow noreferrer">http://www.fmod.org/index.php/download#FMODExProgrammersAPI</a></p>\n'}, {'answer_id': 2874244, 'author': 'edrowland', 'author_id': 346161, 'author_profile': 'https://Stackoverflow.com/users/346161', 'pm_score': 0, 'selected': False, 'text': '<p>The XNA Audio APIs work well in .net/c# applications, and work beautifully for this application. Event-based triggering, along with concurent playback of multiple sounds. Exactly what you want. Oh, and compression as well.</p>\n'}, {'answer_id': 2891604, 'author': 'n535', 'author_id': 192657, 'author_profile': 'https://Stackoverflow.com/users/192657', 'pm_score': 0, 'selected': False, 'text': '<p>Well, it depends on a patent-related laws in a given country, but there is no way to write a mp3 decoder without violating patents, as far as i know. I think the best cross-platform, open source solution for your problem is <a href="http://www.gstreamer.net/" rel="nofollow noreferrer">GStreamer</a>. It has c# bindings, which evolve rapidly. Using and building GStreamer on Windows is not an easy task however. <a href="http://www.gstreamer.net/data/doc/gstreamer/head/manual/html/section-integration-win32.html" rel="nofollow noreferrer">Here</a> is a good starting point. <a href="http://banshee-project.org/" rel="nofollow noreferrer">Banshee</a> project uses this approach, but it is not really usable on windows yet (however, there are some almost-working nightly builds). <a href="http://www.fmod.org/" rel="nofollow noreferrer">FMOD</a> is also a good alternative. Unfortunately, it is not open source and i find that its API is somehow C-styled.</p>\n'}, {'answer_id': 2894463, 'author': 'Kris Ray', 'author_id': 348613, 'author_profile': 'https://Stackoverflow.com/users/348613', 'pm_score': 0, 'selected': False, 'text': '<p>There is a pure C# vorbis decoder available that is open source:</p>\n\n<p><a href="http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/" rel="nofollow noreferrer">http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/</a></p>\n'}, {'answer_id': 6914077, 'author': 'Dmitry', 'author_id': 874773, 'author_profile': 'https://Stackoverflow.com/users/874773', 'pm_score': 0, 'selected': False, 'text': '<p>Not sure if this is still relevant. Simplest solution would be to use <a href="http://naudio.codeplex.com/" rel="nofollow">NAudio</a>, which is a managed open source audio API written in C#. Another thing to try would be utilizing ffmpeg, and creating a process to <a href="http://ffmpeg.zeranoe.com/builds/" rel="nofollow">ffplay.exe</a> (the right binaries are under shared builds).</p>\n'}]
['https://Stackoverflow.com/questions/35896', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3205/']