gpt4 book ai didi

c# - 使用 winmm 的 mp3 播放器,事件 mp3 结束

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:52 26 4
gpt4 key购买 nike

<分区>

我找到了一些代码并根据自己的目的对其进行了修改。但是我不知道当mp3结束时如何 Hook 。如果一首歌结束,我想做的是下一首歌。当前结束时播放随机歌曲很简单,但如何知道它何时结束?我应该尝试计算歌曲的长度并在歌曲结束时使用计时器调用函数吗?

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

}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com