我找到一些代码并根据自己的目的进行修改。但是当mp3到达终点时我不知道如何挂钩。如果一首歌结束,接下来正在播放,我想要做。当前结束播放随机歌曲很简单,但如何知道它何时结束?我应该尝试计算歌曲的长度并在歌曲结束时使用定时器来调用功能吗?
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;
using System.IO;
namespace Playing_MP3_songs
{
public partial class Form1 : Form
{
public const int MM_MCINOTIFY = 0x3B9;
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
List<string> lista;
List<string> lista2;
string st;
public Form1()
{
InitializeComponent();
}
private void set_list()
{
string[] costam = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.mp3", SearchOption.AllDirectories);
string[] costam2 = new string[Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.mp3", SearchOption.AllDirectories).Count()];
int ilosc = costam.Count();
int x = 0;
while (x <= ilosc - 1)
{
costam2[x] = Path.GetFileName(costam[x]);
x++;
}
lista = new List<string>(costam);
lista2 = new List<string>(costam2);
//listBox1.Sorted = true;
listBox2.DataSource = lista;
listBox1.DataSource = lista2;
if (listBox1.Items.Count == 0)
{
button2.Enabled = false;
button3.Enabled = false;
}
else
{
button2.Enabled = false;
button3.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
set_list();
button3.Enabled = true; //So you dont play no file. lol
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
TextReader sr = new StreamReader("path.txt");
st = sr.ReadLine();
sr.Close();
folderBrowserDialog1.SelectedPath = st;
set_list();
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
TextWriter writer = new StreamWriter("path.txt");
writer.WriteLine(folderBrowserDialog1.SelectedPath);
writer.Close();
}
private void button3_Click(object sender, EventArgs e)
{
stop();
listBox2.SetSelected(listBox1.SelectedIndex, true);
mciSendString("open \"" + listBox2.SelectedItem.ToString() + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
mciSendString("play MediaFile", null, 0, IntPtr.Zero);
//button3.Enabled = false;
button2.Enabled = true;
}
private void play()
{
stop();
listBox2.SetSelected(listBox1.SelectedIndex, true);
mciSendString("open \"" + listBox2.SelectedItem.ToString() + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
mciSendString("play MediaFile", null, 0, IntPtr.Zero);
//button3.Enabled = false;
button2.Enabled = true;
}
private void stop()
{
mciSendString("close MediaFile", null, 0, IntPtr.Zero);
listBox2.ClearSelected();
//button2.Enabled = false;
button3.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
mciSendString("close MediaFile", null, 0, IntPtr.Zero);
listBox2.ClearSelected();
button2.Enabled = false;
button3.Enabled = true;
}
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() == "B")
{
listBox1.ClearSelected();
int ilosc = listBox1.Items.Count;
Random random = new Random();
int number = random.Next(0, ilosc - 1);
listBox1.SetSelected(number, true);
stop();
play();
e.Handled = true;
}
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
play();
}
}
}
答案 0 :(得分:0)
将notify
附加到play MediaFile
命令:
mciSendString("play MediaFile notify", null, 0, IntPtr.Zero);
覆盖表单中的WndProc函数,以在播放完成时接收通知消息
protected override void WndProc(ref Message m)
{
if (m.Msg == Media.MM_MCINOTIFY)
{
do smthing..
}
base.WndProc(ref m);
}