多线程放音乐
比如我有两个按键我一个按键对应的函数:是放一首歌,https://cdn.jsdelivr.net/gh/Twelve-blog/picture/f099adba52273499.png
但是我点击第一个按键,放第一首歌,点第二个按键第一首就停了,再放第二首。
所有我该怎么 按第一个按键放第一首歌,然后按第二个按键,第一首歌没有停,继续放第二首歌{:5_100:}
还是说 怎么去对线程这个
这...好家伙,我连PlaySound是个什么类都不知道...
所以我自己写了一个
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestForUI
{
public class PlaySound
{
public static SoundPlayer Play(string wavPath)
{
SoundPlayer sp = new SoundPlayer(wavPath);
sp.Load();
sp.PlayLooping();
return sp;
}
public static void Pause(SoundPlayer sp)
{
}
public static void Stop(SoundPlayer sp)
{
if (sp == null)
{
return;
}
sp.Stop();
}
}
public partial class Form1 : Form
{
private int soundPlaying = 0;
Thread[] threadPool = new Thread;
public Form1()
{
this.InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (soundPlaying != 1)
{
threadPool.Abort();
soundPlaying = 1;
threadPool = null;
threadPool = new Thread(new ParameterizedThreadStart(this.Play));
threadPool.Start(soundPlaying);
}
else
{
threadPool.Abort();
soundPlaying = 2;
threadPool = null;
threadPool = new Thread(new ParameterizedThreadStart(this.Play));
threadPool.Start(soundPlaying);
}
}
private void Play(object obj)
{
int id = (int)obj;
SoundPlayer sp = null;
try
{
if (id == 1)
{
sp = PlaySound.Play(@"./wav/A1.wav");
}
else
{
sp = PlaySound.Play(@"./wav/A2.wav");
}
while (true) ;
}
catch (ThreadAbortException)
{
if (id == 1)
{
PlaySound.Stop(sp);
}
else
{
PlaySound.Stop(sp);
}
}
}
}
}
页:
[1]