鱼C论坛

 找回密码
 立即注册
查看: 2168|回复: 1

多线程放音乐

[复制链接]
发表于 2020-12-7 16:37:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
比如我有两个按键
我一个按键对应的函数:是放一首歌,

                               
登录/注册后可看大图

但是我点击第一个按键,放第一首歌,点第二个按键第一首就停了,再放第二首。

所有我该怎么 按第一个按键放第一首歌,然后按第二个按键,第一首歌没有停,继续放第二首歌

还是说 怎么去对线程这个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-22 10:30:13 | 显示全部楼层
这...好家伙,我连PlaySound是个什么类都不知道...
所以我自己写了一个
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Media;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;

  12. namespace TestForUI
  13. {
  14.     public class PlaySound
  15.     {
  16.         public static SoundPlayer Play(string wavPath)
  17.         {
  18.             SoundPlayer sp = new SoundPlayer(wavPath);
  19.             sp.Load();
  20.             sp.PlayLooping();
  21.             return sp;
  22.         }

  23.         public static void Pause(SoundPlayer sp)
  24.         {

  25.         }

  26.         public static void Stop(SoundPlayer sp)
  27.         {
  28.             if (sp == null)
  29.             {
  30.                 return;
  31.             }
  32.             sp.Stop();
  33.         }
  34.     }
  35.     public partial class Form1 : Form
  36.     {
  37.         private int soundPlaying = 0;
  38.         Thread[] threadPool = new Thread[2];
  39.         public Form1()
  40.         {
  41.             this.InitializeComponent();
  42.         }

  43.         private void button1_Click(object sender, EventArgs e)
  44.         {
  45.             if (soundPlaying != 1)
  46.             {
  47.                 threadPool[1].Abort();
  48.                 soundPlaying = 1;
  49.                 threadPool[1] = null;
  50.                 threadPool[0] = new Thread(new ParameterizedThreadStart(this.Play));
  51.                 threadPool[0].Start(soundPlaying);
  52.             }
  53.             else
  54.             {
  55.                 threadPool[0].Abort();
  56.                 soundPlaying = 2;
  57.                 threadPool[0] = null;
  58.                 threadPool[1] = new Thread(new ParameterizedThreadStart(this.Play));
  59.                 threadPool[1].Start(soundPlaying);
  60.             }
  61.         }

  62.         private void Play(object obj)
  63.         {
  64.             int id = (int)obj;
  65.             SoundPlayer sp = null;
  66.             try
  67.             {
  68.                 if (id == 1)
  69.                 {
  70.                     sp = PlaySound.Play(@"./wav/A1.wav");
  71.                 }
  72.                 else
  73.                 {
  74.                     sp = PlaySound.Play(@"./wav/A2.wav");
  75.                 }
  76.                 while (true) ;
  77.             }
  78.             catch (ThreadAbortException)
  79.             {
  80.                 if (id == 1)
  81.                 {
  82.                     PlaySound.Stop(sp);
  83.                 }
  84.                 else
  85.                 {
  86.                     PlaySound.Stop(sp);
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 20:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表