Playing a .wav file in c# using SoundPlayer - c#

I am currently having a probelm, where a file was sent to me as .m4a and I renamed it to .wav..... (I think that is the issue as I can play other .wav files)
Now it is giving me this error when I run the code:
Additional information: The file located at c:\Windows\Media\dj.wav is not a valid wave file.
Maybe I just need to change it back to .m4a and then have some website convert it to .wav compared to me just renaming it?
My code looks like this:
using System.Media;
playSimpleSound();
private void playSimpleSound()
{
SoundPlayer simpleSound = new SoundPlayer(#"c:\Windows\Media\dj.wav");
simpleSound.Play();
}
Also, how can I write my path, so that it would hit the file in the debug folder? Because I am going to deploy this to a thin client in another building.

Changing the extension of a file does not change the content in it. M4A are MPEG-4 audio-only data, whereas WAV files are typically raw and uncompressed audio samples.
To convert the data itself, you'll need to use an audio transcoding tool like SoX or GoldWave.
As for your path specifier, you can simply use "dj.wav" or "wavfiles\dj.wav", basically relative to where your exe sits. This is because the current working directory is set to where it resides when started with a simple double click in explorer. You can, however also specify the full path using the answer here.
Hope this helps!

Related

Playing Windows System Sounds with System.Media.SoundPlayer

Is it possible that the System.Media.SoundPlayer can not play Windows System Sounds found in c:\Windows\Media??
I have the code:
using (var soundPlayer =
new SoundPlayer(#"c:\Windows\Media\Landscape\Windows Notify.wav"))
{
soundPlayer.Play();
}
Yet when I run this code I get the error:
Sound API only supports playing PCM wave files.
Am I missing something? Is there a way to play these files from a WPF application? (without converting them to PCM)
The SystemSounds class contains the following predefined system sounds:
Asterisk
Beep
Exclamation
Hand
Question
So for example, to play the Stop:
System.Media.SystemSounds.Hand.Play();
All other sounds require you read the desired sound from the registry and play it with code like this:
SoundPlayer simpleSound = new SoundPlayer(#"c:\Windows\Media\Landscape\Windows Notify.wav");
This was an interesting question, but has a simple answer. After reading about this problem, I tried out your code and got the same problem, so then I searched online for a solution. While not finding an exact solution, I did find the SoundPlayer not playing any bundled windows sounds PCM wav files post here on StackOverflow that showed some code that played an audio file from the Windows\Media folder successfully.
I tried that code and it worked, so then I just had to work out why your example didn't work. I checked for any differences between the audio file that did play and your notify audio file in an audio editor, but they were both definitely WAV files.
I tried playing a different audio file from the Windows\Media\Landscape folder and got the same error. I then tried playing an audio file from a different sub folder in the Windows\Media folder and still got the same error. However, I then noticed that many of the folders in the Windows\Media folder had the same audio files in.
That got me thinking and I eventually realised that all of the audio files that are in these folders are actually in the Windows\Media folder directly. So you can play the sounds, but you just have to ignore the ones in the sub folders and play the ones from the Windows\Media folder. This will work:
SoundPlayer soundPlayer = new SoundPlayer(#"C:\Windows\Media\Windows Notify.wav");
soundPlayer.Play();
However, I can't tell you why we got that strange error, but I can only assume that the files in the folder could perhaps be some kind of links to the actual files in the Windows\Media folder and simply used by the operating system for grouping them into categories... or something similar.
"Landscape" refers to a Windows "Sound Scheme" and the wav files therein are utilized by the Windows OS. As you have already determined, those files cannot be used directly.
Comparison of the filesize of the Windows Notify.wav within the Landscape directory and the Windows Notify.wav file within the base Windows\Media directory is quite sizable; 222KB vs 25.5KB ---
If you go into the 'Sound' control panel applet and browse to 'Sounds' tab, you will see a dropdown list for "Sound Scheme:" and those additional folders within Windows\Media will be displayed in that list.
I don't have links to back this up, but after countless hours in dealing with Control Panel Sounds programmatically, I would venture to guess that the 'Sound Scheme' wav files just contain relevant data (perhaps effect data) that is consumed at runtime to play the modified versions of those sounds ... I could be quite wrong about that last part; it's just a guess. Regardless, you won't be able to use those sound scheme files directly within your code.
If you must play that "Landscape" version of the Notify wav, then I'd suggest playing the sound and saving it into a new wave file in an audio editor. You can add the wave file to your installer/deployment project to play it into the Windows\Media folder and call it directly the same way that you are already.
Since the sound seems to be in a compressed format, it has to be decompressed before you can play it using SoundPlayer. You can use Windows Audio Compression Manager to decompress the sound for playback:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd742945(v=vs.85).aspx
However, that's rather complex to implement, so I discovered a nice library that does all that for you, NAudio:
http://naudio.codeplex.com/
Using NAudio seems to be slightly more involved than using System.Media.SoundPlayer, but it also appears to offer far more functionality.

Convert wav to mp3

I try to convert a .wav file to a .mp3 file using C# and Mono.
I found this link which might be helpful.
When I use the following code to convert, I miss a libsndfile.dll. So I searched my harddrive and found it, but using it I get the message that it is not a valid .NET assembly.
AumpelWrapper wrapper = new AumpelWrapper ();
_wrapper.Convert (filePath, (int)SoundFormat.WAV, targetPath, (int)SoundFormat.MP3);
Is there another way to convert wav to mp3 or do I need another library, that this will work?
I want to use that in our company, so it needs to be OK with the license.

How To Open a Media File From a Zip File "Directly" (Without Extracting it)?

In C#, I'm using both the DotNetZip and the System.Windows.Media
I have a zip file that contains some songs, and I want to play them directly from the zip file without having to extract them ..
Now I manged to get a stream to the sound file:
Stream stream = zip["Songs\\IronMaiden\\Song1.mp3"].OpenReader();
but the problem is, that the MediaPlayer.Open method only takes Uri as a parameter
it doesn't take a stream ..
How Can I get around this ?
Knowing that I can't switch to any other media options like fmod.dll nor anything else.
I'll also said it again, I don't want to extract them.
Any help would be appreciated .. Thanks in advance .. :)
If the media player you want to use does not take a stream, there is no way to do it. As has been mentioned, you'll have to save the file and use a URL to the saved file location. Otherwise, find another media player that works with a stream.

How do you check a file type when there is no extension in c#

How do you check a file type when there is no extension in c#
For instance, I have files with no extension, that are either .mp4 or .flv format (just no extension). I plan on converting these video files to audio files however I would like to determine the file type before I start converting it. Is there a way to do this in C#?
I was thinking that maybe I could just rename the file to name.mp4, then perform some task on the file that would either
A) succeed, meaning that the file was indeed .mp4, or
B) fail, in which case I could then rename it to .flv
then convert the file as the appropriate extension. Is there a native process in c# that can look at .mp4 properties or .flv properties? I do not want to rename the file to .mp4 and then open it in a third party application, such as Windows Media Player, in order to see if I named it correctly.
I've heard of reading the first few bytes of a file's contents and making an educated guess at the file's format. This link seems promising:
Using .NET, how can you find the mime type of a file based on the file signature not the extension
I had played this utility (TrID - File Identifier) and seems quite accurate. File type defination package (TrIDDefs) is also up to date.
And Here is a list of file type signature table if you interest. The list is continuing work-in-process.

.swf file duration (length)

I have a problem getting .swf files time duration.
I tried by calling ffmped via cmd in C# and it worked for other video files (.avi, .mp4, .mov etc.), but not for .swf.
(Also, when I open the .swf file in Media Player Classic and when I go to properties, it says the length is 00:00:00, but it plays.)
SWF is a container format. What you're looking for is the length of the *.flv file. That is the actual encoded video. What happens is the SWF file has an internal reference to the FLV file, and then loads / streams that file. AFAIK, ffmpeg should be able to get the length of an FLV file.

Categories

Resources