1

I know this has been asked before but am still not able to play a wav file in wpf.

This is the code I am using.

 <MediaElement Name="mediaSound" LoadedBehavior="Manual" Source="Alarm10.wav"/>

and

  try
        {
            mediaSound.Play();
        }
        catch (Exception a)
        {
            MessageBox.Show(a.Message);
        }

No exception is thrown and the sound is not played.

4

1 回答 1

1

Because there is nothing wrong with the markup, I'm left to only believe that the location of Alarm10.wav is not in the bin\* directory in which the application is executing out of.

Another thing that needs to be evaluated is the volume level of the MediaElement, for example:

mediaSound.Volume = .75;

EDIT

The OP confirmed it was the location of the WAV file that was the problem. When providing a path, it's relative to the WorkingDirectory of the application. During debugging this is the directory in which it's executing out of (generally).

于 2013-05-16T18:34:52.980 回答