Alex Rivera | Logout

Playing sound byte[] in C#

Asked 2011-05-29T16:38:32.583
9

I realize that you can use SoundPlayer to play a sound in C#; however, I want to be able to play a sound with the OS-default sound player using this method from a sound represented in a byte array. Is this possible?

Edit
Report

1 Answer

7

Opening any file with an application, e.g. media player, requires a file.

Thus you need to save your byte array into file in some format, e.g. WAV, and open it:

byte[] bytes = ...
string name = Path.ChangeExtension(Path.GetRandomFileName(), ".wav");
string path = Path.Combine(Path.GetTempPath(), name);
File.WriteAllBytes(path, bytes);
Process.Start(path);

This code will open user default media player, the same as you clicked on it in Windows Explorer.

answered 2011-05-29T16:56:01.003

Your Answer