Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play compressed sound files in C# in a portable way?

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]

like image 733
skolima Avatar asked Aug 30 '08 11:08

skolima


People also ask

Is a WAV file compressed?

WAV files are also uncompressed, meaning that the data is stored as-is in full original format that doesn't require decoding.

How do I unzip a WAV file?

Windows and Mac are both capable of opening WAV files. For Windows, if you double-click a WAV file, it will open using Windows Media Player. For Mac, if you double-click a WAV, it will open using iTunes or Quicktime. If you're on a system without these programs installed, then consider third-party software.

How do I play a WAV file in C++?

To play the wave file over and over again: PlaySound("*. wav", GetModuleHandle(NULL), SND_FILENAME | SND_ASYNC | SND_LOOP); //or sndPlaySound("*. wav", SND_FILENAME | SND_ASYNC | SND_LOOP);

How is a WAV file encoded?

The usual bitstream encoding is the linear pulse-code modulation (LPCM) format. WAV is an application of the Resource Interchange File Format (RIFF) bitstream format method for storing data in chunks, and thus is similar to the 8SVX and the AIFF format used on Amiga and Macintosh computers, respectively.


1 Answers

I finally revisited this topic, and, using help from BrokenGlass on writing WAVE header, updated csvorbis. I've added an OggDecodeStream that can be passed to System.Media.SoundPlayer to simply play any (compatible) Ogg Vorbis stream. Example usage:

using (var file = new FileStream(oggFilename, FileMode.Open, FileAccess.Read))
{
  var player = new SoundPlayer(new OggDecodeStream(file));
  player.PlaySync();
}

'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 SoundPlayer that causes distortion.

Outdated:

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.

like image 85
skolima Avatar answered Sep 17 '22 19:09

skolima