声音文件播放问题

小鸟123 2012-10-27 05:25:34
public partial class Form1 : Form
{
string soundfileName;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "E:\\Kugou\\";
openFileDialog1.Filter = "mp3 files|*.mp3|all files|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
soundfileName = openFileDialog1.FileName;
}

[DllImport("winmm.dll", EntryPoint = "PlaySound")]
public static extern bool PlaySound_DllImport(string pszSound, IntPtr hwnd, int fdwSound);

private void button2_Click(object sender, EventArgs e)
{
bool result;
System.IntPtr resourceHandle = System.IntPtr.Zero;
result = PlaySound_DllImport(soundfileName, resourceHandle, 0);
}
}
为什么我在使用button1按钮打开一个MP3文件后点击播放按钮即button2却不能播放呢,哪里有问题吗?求大神指教啊
...全文
65 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mngzilin 2012-10-27
  • 打赏
  • 举报
回复
PlaySound只能播放WAV格式的音乐文件。 如果要播放其他格式,或者进行更复杂的播放方式,可以使用MCI Command String(多媒体设备的程序接口)
wuyq11 2012-10-27
  • 打赏
  • 举报
回复

检查方法调用
播放可使用
using System.Media;
using System.IO;
SoundPlayer music = new SoundPlayer();
music = new SoundPlayer("");
music.Play();

[DllImport("winmm.dll")]
public static extern long PlaySound(String fileName,long a,long b);

[DllImport("winmm.dll")]
public static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);
public static void PlayMusic(string p_FileName)
{
try
{
mciSendString(@"close "+p_FileName ,"",0,0);
mciSendString(@"open " + p_FileName,"",0,0);
mciSendString(@"play " + p_FileName ,"",0,0);
}
catch
{
}
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧