views:

2357

answers:

8

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.

System.Media.SoundPlayer 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 csvorbis but I don't know how to play it afterwards).

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.

[this question is a follow up to a mailing list discussion on mono-dev mailing list a year ago]

A: 

System.Diagnostics.Process.Start("fullPath.mp3");

I am surprised but the method Dinah mentioned 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.

As for the code project link - this is unfortunately only a P/Invoke wrapper.

skolima
I am assuming that this will work *only* if the user has an MP3 player installed, correct?
Clay Nichols
This will "work" by launching the default application for the MP3 extension, if one is defined on the computer. I don't know the answer (or I'd answer it) but I would try to find a better way, *especially* if interoperability is your goal.
Erik Forbes
+1  A: 

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:

    //
    // Summary:
    //     Starts a process resource by specifying the name of a document or application
    //     file and associates the resource with a new System.Diagnostics.Process component.
    //
    // Parameters:
    //   fileName:
    //     The name of a document or application file to run in the process.
    //
    // Returns:
    //     A new System.Diagnostics.Process component that is associated with the process
    //     resource, or null, if no process resource is started (for example, if an
    //     existing process is reused).
    //
    // Exceptions:
    //   System.ComponentModel.Win32Exception:
    //     There was an error in opening the associated file.
    //
    //   System.ObjectDisposedException:
    //     The process object has already been disposed.
    //
    //   System.IO.FileNotFoundException:
    //     The PATH environment variable has a string containing quotes.
Trap
+2  A: 

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.

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.

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:

  1. Call an external program to play the sound (as suggested earlier)

  2. P/Invoke a C module to play the sound

  3. P/Invoke the OS APIs to play the sound.

(4.) If you're only running this code on windows you could probably just use DirectShow.

P/Invoke can be used in a cross platform way http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names

Once you have your PCM data (using a OGG C Lib or Managed Code, something like this http://www.robburke.net/mle/mp3sharp/ 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 http://msdn.microsoft.com/en-us/library/ms229685.aspx

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.

docflabby
A: 

There is no way for you to do this without using something else for your play handling.

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?

Voted up because it looks like an interesting question. :D

Sergio Tapia
+1  A: 

i think you should have a look a fmod, which is the mother of all audio api

please feel free to dream about http://www.fmod.org/index.php/download#FMODExProgrammersAPI

A: 

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.

edrowland
This is not portable, XNA is not supported on Mono and the port is mostly dead: http://www.monoxna.org/node/4
skolima
A: 

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 GStreamer. It has c# bindings, which evolve rapidly. Using and building GStreamer on Windows is not an easy task however. Here is a good starting point. Banshee project uses this approach, but it is not really usable on windows yet (however, there are some almost-working nightly builds). FMOD is also a good alternative. Unfortunately, it is not open source and i find that its API is somehow C-styled.

n535
A: 

There is a pure C# vorbis decoder available that is open source:

http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/

Kris Ray
I know, I mentioned in the question that I had no luck getting it to work.
skolima