从应用程序播放声音
Playing sound from application
我用 C# 做了一个小型 2d 游戏。我想添加声音。在 YouTube 上观看视频后,我输入了以下代码,但它不是 运行:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
namespace Test_Sound
{
public partial class Form1 : Form
{
private SoundPlayer sound;
public Form1()
{
sound = new SoundPlayer("3G.wav");
InitializeComponent();
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
if(checkBox.Checked)
{
checkBox.Text = "Stop";
sound.Play();
}
else
{
checkBox.Text = "Play";
sound.Stop();
}
}
}
}
Visual Studio 显示以下错误:
line :27 Error 1 'System.Windows.Forms.CheckBox' does not contain a definition for 'Play' and no extension method 'Play' accepting a first argument of type 'System.Windows.Forms.CheckBox' could be found (are you missing a using directive or an assembly reference?) c:\users\hp\documents\visual studio 2013\Projects\Test_Sound\Form1.cs
line 32: Error 2 'System.Windows.Forms.CheckBox' does not contain a definition for 'Stop' and no extension method 'Stop' accepting a first argument of type 'System.Windows.Forms.CheckBox' could be found (are you missing a using directive or an assembly reference?) c:\users\hp\documents\visual studio 2013\Projects\Test_Sound\Form1.cs
谁能帮我解决我的错误?
视频 link 是 Play Sounds in Windows Forms App (C# .NET)
我已经修正了那个错误。但现在我有例外。 Visual Basic 在第 27 行显示以下异常消息。
异常信息如下:
'System.InvalidOperationException' 类型的未处理异常发生在 System.dll
附加信息:声音API只支持播放PCM wave文件。
我从互联网上下载了一个 PCM 波形文件并用它替换了现有文件。但它不起作用。
Play()
和Stop()
是您在Form1()
初始化之前声明的sound
的方法。
您的代码必须是:
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
if(checkBox.Checked)
{
checkBox.Text = "Stop";
sound.Play();
}
else
{
checkBox.Text = "Play";
sound.Stop();
}
}
参见:
- http://www.dotnetperls.com/soundplayer
- https://msdn.microsoft.com/en-us/library/System.Media.SoundPlayer_methods(v=vs.110).aspx
关于您的新例外,PCM(脉冲编码调制 - https://en.wikipedia.org/wiki/Pulse-code_modulation)是 System.Media.SoundPlayer class 唯一支持的一种。它是最常见的 WAV 格式,因此大多数 .WAV 文件都可以使用。
有一些工具可以转换音频文件。例如,Switch Audio (http://www.nch.com.au/switch/) 可以在格式之间进行转换(即使是免费版本)。您需要将文件转换为标准的 PCM 编码 WAV 文件。
我用 C# 做了一个小型 2d 游戏。我想添加声音。在 YouTube 上观看视频后,我输入了以下代码,但它不是 运行:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
namespace Test_Sound
{
public partial class Form1 : Form
{
private SoundPlayer sound;
public Form1()
{
sound = new SoundPlayer("3G.wav");
InitializeComponent();
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
if(checkBox.Checked)
{
checkBox.Text = "Stop";
sound.Play();
}
else
{
checkBox.Text = "Play";
sound.Stop();
}
}
}
}
Visual Studio 显示以下错误:
line :27 Error 1 'System.Windows.Forms.CheckBox' does not contain a definition for 'Play' and no extension method 'Play' accepting a first argument of type 'System.Windows.Forms.CheckBox' could be found (are you missing a using directive or an assembly reference?) c:\users\hp\documents\visual studio 2013\Projects\Test_Sound\Form1.cs
line 32: Error 2 'System.Windows.Forms.CheckBox' does not contain a definition for 'Stop' and no extension method 'Stop' accepting a first argument of type 'System.Windows.Forms.CheckBox' could be found (are you missing a using directive or an assembly reference?) c:\users\hp\documents\visual studio 2013\Projects\Test_Sound\Form1.cs
谁能帮我解决我的错误?
视频 link 是 Play Sounds in Windows Forms App (C# .NET)
我已经修正了那个错误。但现在我有例外。 Visual Basic 在第 27 行显示以下异常消息。 异常信息如下:
'System.InvalidOperationException' 类型的未处理异常发生在 System.dll
附加信息:声音API只支持播放PCM wave文件。 我从互联网上下载了一个 PCM 波形文件并用它替换了现有文件。但它不起作用。
Play()
和Stop()
是您在Form1()
初始化之前声明的sound
的方法。
您的代码必须是:
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
if(checkBox.Checked)
{
checkBox.Text = "Stop";
sound.Play();
}
else
{
checkBox.Text = "Play";
sound.Stop();
}
}
参见:
- http://www.dotnetperls.com/soundplayer
- https://msdn.microsoft.com/en-us/library/System.Media.SoundPlayer_methods(v=vs.110).aspx
关于您的新例外,PCM(脉冲编码调制 - https://en.wikipedia.org/wiki/Pulse-code_modulation)是 System.Media.SoundPlayer class 唯一支持的一种。它是最常见的 WAV 格式,因此大多数 .WAV 文件都可以使用。
有一些工具可以转换音频文件。例如,Switch Audio (http://www.nch.com.au/switch/) 可以在格式之间进行转换(即使是免费版本)。您需要将文件转换为标准的 PCM 编码 WAV 文件。