简体   繁体   English

如何使用 C# 播放 .wav 文件

[英]how to play a .wav file using c#

I am new to System.Media and I want to simply play the sound of a .wav file using c# I did exactly what is written in this question's answars]( How to play a sound in C#, .NET ) and it won't work.我是System.Media新手,我只想使用c#播放.wav文件的声音工作。 I used special .wav file that is especially for testing so it can`t be the problem.我使用了专门用于测试的特殊.wav文件,所以这不是问题。 the file path cant be the problem either, as I copied it and not manually typed it. file path也不是问题,因为我复制了它而不是手动输入它。

I don't know what have I done wrong, no errors.我不知道我做错了什么,没有错误。 thank you in advance!先感谢您!

here is the code这是代码

// using System.Media;
  const string soundLocation = @"cannot share the actual path but its not the problem anyway";
            System.Media.SoundPlayer player = new System.Media.SoundPlayer(soundLocation);
            player.Play();

wav file wav文件

If you want the sound to play, you could use:如果你想播放声音,你可以使用:

player.PlaySync();

rather than:而不是:

player.Play();

The reason that the latter doesn't work is that your program exits too quickly (once execution gets to the end of the Main method).后者不起作用的原因是您的程序退出太快(一旦执行到Main方法的末尾)。 PlaySync avoids this issue by using the current thread rather than a new thread . PlaySync通过使用当前线程而不是新线程来避免此问题。

I have put your wave file in my local drive (D:) and try with .NET Framework 3.0 until .NET Framework 4.5 all of them play the sound .here is my code or your code ;)我已将您的波形文件放在我的本地驱动器 (D:) 中,并尝试使用 .NET Framework 3.0 直到 .NET Framework 4.5 所有这些都播放声音。这是我的代码或您的代码;)

 const string soundLocation = @"D:\\file_example_WAV_1MG.wav";
 System.Media.SoundPlayer player = new System.Media.SoundPlayer(soundLocation);
 player.Play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM