做的一个小的MP3播放器,用using System.Windows.Forms.Timer这个来计时,获取当前歌曲播放的位置(播放时间),并显示到窗体上.当我运行程序,点击一首歌曲开始播放时,程序就出现未响应。求解。
下面是一部分代码:class DoMusic
{
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength); /// <summary>
/// 获取当前播放的位置
/// </summary>
/// <returns>当前时间值(毫秒)</returns>
public string GetCurrentTime()
{
string st="";
mciSendString(@"status song position", st, 100, 0);
return st;
} public void Play(string path)
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(path, shortpath, shortpath.Capacity);
path = shortpath.ToString();
mciSendString(@"close all", null, 0, 0);
mciSendString(@"open " + path + " alias song", null, 0, 0); //打开
mciSendString(@"play song", null, 0, 0); //播放
}
}
public partial class MyMusic : Form
{
public MyMusic()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
} //播放
private void btnPlay_Click(object sender, EventArgs e)
{
string path = @"F:\Music\123.mp3";
DoMusic dm = new DoMusic();
Play(path);
progressTimer.Start();
} //Timer事件
private void progressTimer_Tick(object sender, EventArgs e)
{
DoMusic dm = new DoMusic();
string currentTime = dm.GetCurrentTime();
label1.text=currentTime;
}
}
下面是一部分代码:class DoMusic
{
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength); /// <summary>
/// 获取当前播放的位置
/// </summary>
/// <returns>当前时间值(毫秒)</returns>
public string GetCurrentTime()
{
string st="";
mciSendString(@"status song position", st, 100, 0);
return st;
} public void Play(string path)
{
StringBuilder shortpath = new StringBuilder(80);
int result = GetShortPathName(path, shortpath, shortpath.Capacity);
path = shortpath.ToString();
mciSendString(@"close all", null, 0, 0);
mciSendString(@"open " + path + " alias song", null, 0, 0); //打开
mciSendString(@"play song", null, 0, 0); //播放
}
}
public partial class MyMusic : Form
{
public MyMusic()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
} //播放
private void btnPlay_Click(object sender, EventArgs e)
{
string path = @"F:\Music\123.mp3";
DoMusic dm = new DoMusic();
Play(path);
progressTimer.Start();
} //Timer事件
private void progressTimer_Tick(object sender, EventArgs e)
{
DoMusic dm = new DoMusic();
string currentTime = dm.GetCurrentTime();
label1.text=currentTime;
}
}
DoMusic dm = new DoMusic();
dm.Play(path); progressTimer.Start();
直接可以掉play()吗??
{
string path = @"F:\Music\123.mp3";
DoMusic dm = new DoMusic();
Play(path);
progressTimer.Start();
}在private void btnPlay_Click(object sender, EventArgs e)
这个地方放个断点进去看看,就可知道了啊
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);