首页 新闻 会员 周边 捐助

照书上抄了一个小simplemp3player,可是怎么也不会响,大家帮忙看看,是怎么回事

0
悬赏园豆:10 [已解决问题] 解决于 2011-04-16 12:25

    

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);
}

}
}

问题补充: 那个结束标签上能显示结束时间,就是不会响,当按下play按钮时,状态栏标签上显示的是stop,似乎是mcisendstring play根本没有调用,不知道咋回事,大家帮忙看看,谢了
青石的主页 青石 | 初学一级 | 园豆:160
提问于:2011-04-11 18:18
< >
分享
最佳答案
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 有好多

收获园豆:10
2012 | 高人七级 |园豆:21445 | 2011-04-14 17:07
基本确定了,就是那个btnplay click事件中的scisendstring play 调用出错了,把from 和后面的 progressbar.value.tostring()去掉之后,就能把声音播出来,可这样一来,有些功能又丢了,这个from到底是怎么用的,还得查查资料,对了,能不能推荐两本关于c#的经典书籍,要那种例子多一点的
青石 | 园豆:160 (初学一级) | 2011-04-15 18:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册