using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace simpleMP3Player
{
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
private static extern long mciSendString(string lpstrCommand, StringBuilder lpsstrReturnString, int uReturnLength, int hwndCallback);
private string sFileName;
StringBuilder buffer = new StringBuilder(128);
private void form1_Load(object sender, EventArgs e)
{
btnPlay.Enabled = false;
btnPause.Enabled = false;
btnPre.Enabled = false;
btnStop.Enabled = false;
btnNext.Enabled = false;
timer.Enabled = false;
timer.Interval = 500;
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
if (sFileName != null && sFileName.Trim() != "")
{
mciSendString("stop MP3File", null, 0, 0);
mciSendString("close MP3File", null, 0, 0);
}
sFileName = openFileDialog.FileName;
string strLength = "";
buffer = new StringBuilder(128);
mciSendString("open \""+ sFileName +"\""+" type MPEGVideo alias MP3File", null, 0, 0);
mciSendString("set MP3File time format milliseconds", null, 0, 0);
mciSendString("set MP3File seek exactly on", null, 128, 0);
mciSendString("seek MP3File to start", null, 0, 0);
mciSendString("status MP3File length",buffer, 128, 0);
strLength = buffer.ToString().PadLeft(8,' ').Substring (0,8);
if (strLength .Trim() !="")
{
progressBar.Minimum =0;
progressBar .Value=0;
progressBar.Maximum =int.Parse (strLength );
}
TimeSpan ts = new TimeSpan(long.Parse(strLength) * 10000);
lbCurrent.Text="00:00:00";
lbBegin.Text="00:00:00";
toolStripStatusLabel1 .Text="";
lbEnd.Text=ts.Hours.ToString("00:")+ts.Minutes.ToString("00:")+ts.Seconds .ToString("00");
timer.Enabled = false;
btnPlay.Enabled=true;
}
}
private void btnPlay_Click(object sender, EventArgs e)
{
string strMode = "";
btnPause.Enabled = true;
btnStop.Enabled = true;
btnPre.Enabled = true;
btnNext.Enabled = true;
mciSendString("play MP3File from" + progressBar.Value.ToString(), null, 0, 0);
mciSendString("status MP3File mode", buffer, 128, 0);
strMode = buffer.ToString().PadLeft(8, ' ').Substring(0, 8);
toolStripStatusLabel1.Text = strMode;
timer.Enabled = true;
}
private void btnPause_Click(object sender, EventArgs e)
{
mciSendString("pause MP3File", null, 0, 0);
btnPlay.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
btnPre.Enabled = false;
btnNext.Enabled = false;
}
private void btnStop_Click(object sender, EventArgs e)
{
mciSendString("seek MP3File to start",null,0,0);
mciSendString("stop MP3File", null, 0, 0);
btnPlay.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
btnPre.Enabled =false ;
btnNext.Enabled = false;
}
private void btnPre_Click(object sender, EventArgs e)
{
mciSendString("play MP3File from" + ((int)(progressBar.Value -(progressBar.Maximum / 10))).ToString(), null, 0, 0);
}
private void btnNext_Click(object sender, EventArgs e)
{
mciSendString("play MP3File from" + ((int)(progressBar.Value + (progressBar.Maximum / 10))).ToString(), null, 0, 0);
}
private void timer_Tick(object sender, EventArgs e)
{
string strPosition = "";
string strMode = "";
buffer = new StringBuilder(128);
mciSendString("status MP3File position", buffer, 128, 0);
if (buffer.Length >= 8)
{
strPosition = buffer.ToString(0, 8);
}
else
{
strPosition = buffer.ToString();
}
progressBar.Value = int.Parse(strPosition);
TimeSpan ts = new TimeSpan(long.Parse(strPosition) * 10000);
lbCurrent.Text = ts.Hours.ToString("00:") + ts.Minutes.ToString("00:") + ts.Seconds.ToString("00");
if (progressBar.Value == progressBar.Maximum)
{
btnStop_Click(null,null);
}
buffer = new StringBuilder(128);
mciSendString("status MP3File mode", buffer, 128, 0);
strMode = buffer.ToString().PadLeft(8, ' ').Substring(0, 8);
toolStripStatusLabel1.Text = strMode;
}
private void form1_FormClosing(object sender, FormClosingEventArgs e)
{
mciSendString("close MP3File", null, 0, 0);
}
}
}
C# 还是使用FCL库吧,参考 http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx http://msdn.microsoft.com/zh-cn/library/dd564582(v=vs.85).aspx 这些高级控件更方便一些
我以前C++使用winmm.dll的一些函数时,还是比较复杂的,采样率什么要正确设置,或者找一个保证好的C++ 类参考http://www.codeproject.com 有好多