Click here to Skip to main content
14,331,485 members
Rate this:
Please Sign up or sign in to vote.
See more:
Howdy

In c#+VS2008+XP, I can play a wav file by calling

System.Media.SoundPlayer sp = new System.Media.SoundPlayer(new FileInfo( newfile).FullName); 

sp.Play();//sp.PlaySync();


In year 2010 when I test it in Win7 and vista, I found it doesn't work at all(no sound from speaker and no exception from program).

However there is no problem when I play back this file in any players or in c++ Directshow and c++ soundplay();

Do anyone know how to solve this C# SoundPlayer problem?


REF: http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx#Y2800[^]

Thanks in advanced
Du Sijun
Posted
Updated 4-May-11 8:20am
v6
Comments
DaveAuld 1-May-11 4:53am
   
Edit: Code Formatting and link
strogg 1-May-11 4:58am
   
One possibility is that you have a wav file with a compressed format and the codec is missing in the system. Should not be a problem if it's plain PCM wave file
Du Sijun 1-May-11 6:25am
   
Hi,thanks for your hints, I am now checking file format.
   
You should not say "does not work" but report the problem properly.
--SA
Du Sijun 4-May-11 14:19pm
   
Good point, I mean no sound from speaker and no exception from program. However I had fix this problem.Thanks for your reply.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I have tried both of the following methods on Windows 7 and they work fine;

String filename = "C:\\Windows\\Media\\tada.wav";
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(filename);
sp.Play();


and

String filename = "C:\\Windows\\Media\\chord.wav";
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(new System.IO.FileInfo(filename).FullName);
sp.Play();
   
Comments
Du Sijun 1-May-11 5:15am
   
thanks, your code works for me too, so I believe there is something wrong with my wav format.
Du Sijun 1-May-11 6:33am
   
Thanks all, the problem is solved, the problem is cause by the file format.

In my program , when I call

_binaryWriter.Write("data");

It will write {0x04,'d','a','t','a'}
but not {'d','a','t','a'} to the file which cause the format problem. Even it can be played in WindowsMediaPlayer, it will not work in soundplayer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100