Gen*_*ius 36 c# audio visual-studio
如何从我的项目资源中播放WAV音频文件?我的项目是C#中的Windows窗体应用程序.
Eva*_*ski 48
因为mySoundFile是a Stream,你可以利用SoundPlayer重载的构造函数,它接受一个Stream对象:
System.IO.Stream str = Properties.Resources.mySoundFile;
System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
snd.Play();
Run Code Online (Sandbox Code Playgroud)
Pra*_*ran 44
a)好的,首先将音频文件(.wav)添加到项目资源中.




b)现在,只需编写此代码即可播放音频.
在这段代码中,我正在表单加载事件上播放音频.
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; // at first you've to import this package to access SoundPlayer
namespace WindowsFormsApplication1
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
private void login_Load(object sender, EventArgs e)
{
playaudio(); // calling the function
}
private void playaudio() // defining the function
{
SoundPlayer audio = new SoundPlayer(WindowsFormsApplication1.Properties.Resources.Connect); // here WindowsFormsApplication1 is the namespace and Connect is the audio file name
audio.Play();
}
}
}
Run Code Online (Sandbox Code Playgroud)
而已.
全部完成,现在运行项目(按f5)并享受你的声音.
祝一切顺利.:)
Stream str = Properties.Resources.mySoundFile;
RecordPlayer rp = new RecordPlayer();
rp.Open(new WaveReader(str));
rp.Play();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72699 次 |
| 最近记录: |