本文介绍了如何在C#中播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在winforms中播放声音.我正在使用以下代码,但无法播放.
How to play sound in winforms. I am using below code but its not play.
Stream audioStream = new MemoryStream(Properties.Resources.TestSound.ReadByte());
SoundPlayer player = new SoundPlayer(audioStream);
player.Play();
推荐答案
using System.Runtime.InteropServices; //with this code you are able to import dll files
第2部分:
下:
Part 2:
under:
public Form1()
{
InitializeComponent();
}
输入代码:
enter code:
[DllImport("winmm.dll")]
private static extern long mciSendString(
string command,
StringBuilder returnValue,
int returnLength,
IntPtr winHandle);//imports a dll file and creates a new command
第3部分:
Part 3:
string filename1 = "";//location of first file
mciSendString("close mf1", null, 0, IntPtr.Zero);
mciSendString("open \"" + filename1 + "\" type mpegvideo alias mf1", null, 0, IntPtr.Zero);
mciSendString("play mf1", null, 0, IntPtr.Zero)
这对我有用;如果您想了解有关此代码的更多信息,请搜索MCI(媒体控制接口);一些额外的命令:
暂停MF1
简历MF1
停止MF1
寻求mf1到x//x是某个数字
尝试让他们看看他们做什么
this worked for me;if you want to know more about this code search for MCI(Media Control Interface);Some extra commands:
pause mf1
resume mf1
stop mf1
seek mf1 to x // x is some number
try them to see what they do
SoundPlayer player = new SoundPlayer(Properties.Resources.sound1);
player.Play();
例如,sound1是媒体文件名.
sound1 is media file name for example.
这篇关于如何在C#中播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!