ThiefMaster
ThiefMaster

Reputation: 318488

How to play a mp3 file from a C# program without adding any dependencies?

In a C# program I'd like to allow users to specify a sound file (wav or mp3) to be played on a certain event.

With a WAV file I can use SoundPlayer, but unfortunately it cannot play anything else (e.g. MP3).

Now I'm looking for a way to play a sound file - wav or mp3 - without adding any dependencies.

Embedding WMP would work, but some people uninstall and it would fail in this case. Additionally I think I'd have to add an additional DLL file - and I really want to keep my application as a single exe file without any additional stuff.

Upvotes: 3

Views: 1694

Answers (2)

hemp
hemp

Reputation: 5663

.NET does not have a built-in MP3 decoder/player. You can play MP3s using DirectShow, DirectSound (part of DirectX) or MCI (Media Control Interface), with MCI being the simplest.

However, it should also be noted that packaging dependencies (say you wanted to use a specific MP3 codec for licensing reasons) does not necessarily require separate files. You just may have to be clever about packaging them into the .exe.

For instance, dependencies can be delivered as Embedded Resources within your executable and loaded from a ManifestResouceStream.

Upvotes: 1

Tim Lloyd
Tim Lloyd

Reputation: 38434

If the main driver is having a single exe file, then you could consider re-packaging dependent files into your executable using a tool like ILMerge (and a much nicer UI for it: NuGenUnify). This would allow you to use a helper library which you can embed in your exe.

Upvotes: 2

Related Questions