|
发表于 2021-7-22 10:30:13
|
显示全部楼层
这...好家伙,我连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[2];
- public Form1()
- {
- this.InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (soundPlaying != 1)
- {
- threadPool[1].Abort();
- soundPlaying = 1;
- threadPool[1] = null;
- threadPool[0] = new Thread(new ParameterizedThreadStart(this.Play));
- threadPool[0].Start(soundPlaying);
- }
- else
- {
- threadPool[0].Abort();
- soundPlaying = 2;
- threadPool[0] = null;
- threadPool[1] = new Thread(new ParameterizedThreadStart(this.Play));
- threadPool[1].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);
- }
- }
- }
- }
- }
复制代码 |
|