Как воспроизводить mp3 через определенные промежутки времени - C#
Формулировка задачи:
Допустим, есть кнопка button1.
Как сделать так, что бы после нажатия на неё через определённые промежутки времени воспроизводился определённый файл mp3?
Например,
При этом нужно, что кнопки интерфейса оставались активными и работали.
Пауза n мс (n записано в переменной pause1)
Воспроизвести файл 1.mp3
Пауза m мс (m записано в переменной pause2)
Воспроизвести файл 2.mp3
и т.п.
Решение задачи: «Как воспроизводить mp3 через определенные промежутки времени»
textual
Листинг программы
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PlayList tracks = new PlayList();
tracks.GetAdress();
int interruptionTime = 5000;
while (true)
{
tracks.Play();
Thread.Sleep(interruptionTime);
tracks.Next();
Thread.Sleep(interruptionTime);
}
}
}
//===========================================================================================================
public class PlayList
{
private string[] soundAdress;
private string[] soundNames;
private int Count;
private int currentTrack;
private Audio player;
private int trackTime;
public int TrackTime
{
get
{
return trackTime;
}
}
public int CurrentTrak
{
get
{
return currentTrack;
}
}
/// <summary>
/// Количество треков в плейлисте
/// </summary>
public int TrackCount
{
get
{
return Count;
}
}
public string[] TrackInList
{
get
{
string[] TrackList = new string[Count];
for (int i = 0; i < Count; i++)
{
TrackList[i] = soundNames[i];
}
return TrackList;
}
}
public PlayList()
{
currentTrack = -1;
Count = 0;
this.soundAdress = new string[10000];
this.soundNames = new string[10000];
}
public void GetAdress()
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowNewFolderButton = false;
dialog.Description = "Выберите папку с музыкой";
if (dialog.ShowDialog() == DialogResult.OK)
{
AllFolderAndFiles(dialog.SelectedPath);
}
}
private void Add(string adress, string name)
{
soundAdress[Count] = adress;
soundNames[Count] = name;
Count++;
}
private void AllFolderAndFiles(string folder)
{
DirectoryInfo dir = new DirectoryInfo(folder);
FileInfo[] filesMP3 = dir.GetFiles("*.mp3");
FileInfo[] filesWAV = dir.GetFiles("*.wav");
foreach (FileInfo file in filesMP3)
{
Add(file.FullName, file.Name);
}
foreach (FileInfo file in filesWAV)
{
Add(file.FullName, file.Name);
}
DirectoryInfo[] folders = dir.GetDirectories();
foreach (DirectoryInfo info in folders)
{
AllFolderAndFiles(info.FullName);
}
}
/// <summary>
/// Удалить текущий трек
/// </summary>
public void Play(int index)
{
if (player != null)
{
if (player.State == StateFlags.Paused && currentTrack == index)
{
player.Play();
}
else
{
player.Open(soundAdress[index]);
player.Play();
}
}
else
{
player = new Audio(soundAdress[index]);
player.Play();
}
this.trackTime = (int)player.Duration;
this.currentTrack = index;
}
public void Play()
{
if (soundAdress[0] != null)
{
player.Open(soundAdress[0], true);
}
else
{
throw new Exception("No track to play!");
}
}
public void Stop()
{
player.Stop();
}
public void Pause()
{
player.Pause();
}
public void Next()
{
if (currentTrack < Count)
{
currentTrack = currentTrack + 1;
Play(currentTrack);
}
}
public void Previous()
{
if (currentTrack > 0)
{
currentTrack = currentTrack - 1;
Play(currentTrack);
}
}
public void PlayTime(int seconds)
{
player.CurrentPosition = (double)seconds;
}
}