日期:2014-05-20  浏览次数:21605 次

C# WinForm 窗体程序如何调用mp3格式的音频文件
读取wav格式的我是这样读取的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  SoundPlayer player1 = new SoundPlayer("cishi.wav");
  player1.Play();

    
  }

  }
}
这种方法只适用于wav格式的。
后我读取mp3格式的我是这样读取的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
    

  WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
  wmp.URL = "mp3cishi.mp3";
  wmp.controls.play();
  }

  }
}
在此中引用Interop.WMPLib.dll这个组件。这个是可以读取,但在杀毒软件的检测下,它是有漏洞的,检测为蠕虫病毒。


后我又用了这种方法
#region 播放Mp3
  public class Mp3Player
  {
  // <summary>
  /// 使用API
  /// </summary>
  static uint SND_ASYNC = 0x0001; // play asynchronously  
  static uint SND_FILENAME = 0x00020000; // name is file name
  [DllImport("winmm.dll")]
  static extern int mciSendString(string m_strCmd, string m_strReceive, int m_v1, int m_v2);

  [DllImport("Kernel32", CharSet = CharSet.Auto)]
  static extern Int32 GetShortPathName(String path,StringBuilder shortPath, Int32 shortPathLength);

  public static void Play(string MusicFile)
  {
  if (!System.IO.File.Exists(MusicFile)) return;
  StringBuilder shortpath=new StringBuilder(80);
  int result = GetShortPathName(MusicFile, shortpath, shortpath.Capacity);
  MusicFile = shortpath.ToString();
  mciSendString(@"close all",null,0,0);
  mciSendString(@"open " + MusicFile + " alias song", null, 0, 0); //打开
  mciSendString("play song",null,0,0); //播放
  }
  }
  #endregion
但这种方法调用时,为何有些MP3格式的音频文件无法读取。
望大家能有好的建议,好的其它方法帮我解决这个问题(C# WinForm 窗体程序如何调用mp3格式的音频文件),在此谢谢了。
------解决方案--------------------
你问,要加载的音频文件放在什么目录下?