How to play a WPF Sound File resource

前端 未结 3 2032
情深已故
情深已故 2020-12-01 14:46

I am trying to play a sound file in my WPF application. Currently I have the following call:

private void PlaySound(string uriPath)
{
    Uri uri = new Uri(@         


        
3条回答
  •  暖寄归人
    2020-12-01 15:06

    The following seems to work in .NET Framework 4.5:

    var sri = Application.GetResourceStream(new Uri("pack://application:,,,/MyAssemblyName;component/Resources/CameraShutter.wav"));
    
    if ((sri != null)) 
    {
      using (s == sri.Stream) 
      {
        System.Media.SoundPlayer player = new System.Media.SoundPlayer(s);
        player.Load();
        player.Play();
      }
    }
    

    CameraShutter.wav is embedded as Resource in my project (and resides inside Resources subfolder, as indicated in the pack URI).

提交回复
热议问题