Как воспроизводить mp3 через определенные промежутки времени - C#

Узнай цену своей работы

Формулировка задачи:

Допустим, есть кнопка button1. Как сделать так, что бы после нажатия на неё через определённые промежутки времени воспроизводился определённый файл mp3? Например,
Пауза n мс (n записано в переменной pause1) Воспроизвести файл 1.mp3 Пауза m мс (m записано в переменной pause2) Воспроизвести файл 2.mp3 и т.п.
При этом нужно, что кнопки интерфейса оставались активными и работали.

Решение задачи: «Как воспроизводить mp3 через определенные промежутки времени»

textual
Листинг программы
  1.  public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         private void button1_Click(object sender, EventArgs e)
  9.         {
  10.             PlayList tracks = new PlayList();
  11.             tracks.GetAdress();
  12.             int interruptionTime = 5000;
  13.             while (true)
  14.             {
  15.  
  16.                 tracks.Play();
  17.                 Thread.Sleep(interruptionTime);
  18.                 tracks.Next();
  19.                 Thread.Sleep(interruptionTime);
  20.             }
  21.         }
  22.     }
  23.  
  24.  
  25.  
  26.     //===========================================================================================================
  27.  
  28.  
  29.     public class PlayList
  30.     {
  31.         private string[] soundAdress;
  32.         private string[] soundNames;
  33.         private int Count;
  34.         private int currentTrack;
  35.         private Audio player;
  36.         private int trackTime;
  37.         public int TrackTime
  38.         {
  39.             get
  40.             {
  41.                 return trackTime;
  42.             }
  43.         }
  44.         public int CurrentTrak
  45.         {
  46.             get
  47.             {
  48.                 return currentTrack;
  49.             }
  50.         }
  51.         /// <summary>
  52.         /// Количество треков в плейлисте
  53.         /// </summary>
  54.  
  55.         public int TrackCount
  56.         {
  57.             get
  58.             {
  59.                 return Count;
  60.             }
  61.  
  62.         }
  63.         public string[] TrackInList
  64.         {
  65.             get
  66.             {
  67.                 string[] TrackList = new string[Count];
  68.                 for (int i = 0; i < Count; i++)
  69.                 {
  70.                     TrackList[i] = soundNames[i];
  71.                 }
  72.  
  73.                 return TrackList;
  74.             }
  75.  
  76.         }
  77.  
  78.  
  79.  
  80.         public PlayList()
  81.         {
  82.             currentTrack = -1;
  83.             Count = 0;
  84.             this.soundAdress = new string[10000];
  85.             this.soundNames = new string[10000];
  86.  
  87.         }
  88.         public void GetAdress()
  89.         {
  90.             FolderBrowserDialog dialog = new FolderBrowserDialog();
  91.             dialog.ShowNewFolderButton = false;
  92.             dialog.Description = "Выберите папку с музыкой";
  93.  
  94.  
  95.             if (dialog.ShowDialog() == DialogResult.OK)
  96.             {
  97.                 AllFolderAndFiles(dialog.SelectedPath);
  98.             }
  99.         }
  100.         private void Add(string adress, string name)
  101.         {
  102.             soundAdress[Count] = adress;
  103.             soundNames[Count] = name;
  104.             Count++;
  105.         }
  106.         private void AllFolderAndFiles(string folder)
  107.         {
  108.  
  109.             DirectoryInfo dir = new DirectoryInfo(folder);
  110.             FileInfo[] filesMP3 = dir.GetFiles("*.mp3");
  111.             FileInfo[] filesWAV = dir.GetFiles("*.wav");
  112.  
  113.             foreach (FileInfo file in filesMP3)
  114.             {
  115.                 Add(file.FullName, file.Name);
  116.             }
  117.             foreach (FileInfo file in filesWAV)
  118.             {
  119.                 Add(file.FullName, file.Name);
  120.             }
  121.  
  122.  
  123.  
  124.             DirectoryInfo[] folders = dir.GetDirectories();
  125.             foreach (DirectoryInfo info in folders)
  126.             {
  127.                 AllFolderAndFiles(info.FullName);
  128.  
  129.             }
  130.  
  131.  
  132.  
  133.         }
  134.        
  135.         /// <summary>
  136.         /// Удалить текущий трек
  137.         /// </summary>
  138.  
  139.         public void Play(int index)
  140.         {
  141.  
  142.  
  143.             if (player != null)
  144.             {
  145.  
  146.                 if (player.State == StateFlags.Paused && currentTrack == index)
  147.                 {
  148.                     player.Play();
  149.                 }
  150.                 else
  151.                 {
  152.                     player.Open(soundAdress[index]);
  153.                     player.Play();
  154.                 }
  155.             }
  156.             else
  157.             {
  158.  
  159.                 player = new Audio(soundAdress[index]);
  160.                 player.Play();
  161.  
  162.             }
  163.             this.trackTime = (int)player.Duration;
  164.             this.currentTrack = index;
  165.         }
  166.  
  167.  
  168.         public void Play()
  169.         {
  170.             if (soundAdress[0] != null)
  171.             {
  172.                 player.Open(soundAdress[0], true);
  173.             }
  174.             else
  175.             {
  176.                 throw new Exception("No track to play!");
  177.             }
  178.         }
  179.         public void Stop()
  180.         {
  181.             player.Stop();
  182.         }
  183.         public void Pause()
  184.         {
  185.             player.Pause();
  186.         }
  187.         public void Next()
  188.         {
  189.             if (currentTrack < Count)
  190.             {
  191.                 currentTrack = currentTrack + 1;
  192.                 Play(currentTrack);
  193.             }
  194.         }
  195.         public void Previous()
  196.         {
  197.             if (currentTrack > 0)
  198.             {
  199.                 currentTrack = currentTrack - 1;
  200.                 Play(currentTrack);
  201.             }
  202.  
  203.         }
  204.         public void PlayTime(int seconds)
  205.         {
  206.             player.CurrentPosition = (double)seconds;
  207.  
  208.  
  209.         }
  210.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы