Code
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Music
{
/**/
/// <summary>
///
/// </summary>
public class Audio
{
[DllImport("winmm.dll")]
private static extern int mciSendString
(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName
(
[MarshalAs(UnmanagedType.LPTStr)] string path,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
int shortPathLength
);
public Audio()
{
}
// Music.Audio.Play("E:\\DIY\\365CrazyEnglish\\Resource\\3.mp3");
public static void Play(string FileName)
{
StringBuilder shortPathTemp = new StringBuilder(255);
int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity);
string ShortPath = shortPathTemp.ToString();
mciSendString("open " + ShortPath + " alias song", "", 0, 0);
mciSendString("play song", "", 0, 0);
}
public void Stop()
{
mciSendString("stop song", "", 0, 0);
}
public void Pause()
{
mciSendString("pause song", "", 0, 0);
}
public void Close()
{
mciSendString("close song", "", 0, 0);
}
}
}
axWindowsMediaPlayer方法循环播放
Code
private void button1_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = @"D:\Study\My Music\PerhapsLove.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();
WMPLib.IWMPPlaylist currentList = this.axWindowsMediaPlayer1.currentPlaylist;
WMPLib.IWMPMedia media;
media = this.axWindowsMediaPlayer1.newMedia(@"D:\Study\My Music\PerhapsLove.mp3");
currentList.appendItem(media);
media = this.axWindowsMediaPlayer1.newMedia(@"D:\Study\My Music\擦肩而过.mp3");
currentList.appendItem(media);
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, _WMPOCXEvents_PlayStateChangeEvent e)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}