rhythmbullet/RhythmBullet/Zer01HD/Audio/MusicList.cs

94 lines
2.4 KiB
C#

using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Audio
{
internal class MusicList
{
private volatile bool work;
private List<string> list = new List<string>();
public List<string> List
{
get
{
if (!thread.IsAlive)
{
lock (list)
{
return list;
}
}
else
{
return null;
}
}
}
private string path;
private Thread thread;
public MusicList()
{
}
public void Start()
{
if (thread == null || !thread.IsAlive)
{
work = true;
thread = new Thread(Search);
thread.Name = "Music Search Thread";
thread.Start();
}
}
public void Stop()
{
work = false;
}
private void Search()
{
lock (list)
{
list.AddRange(recursiveMusicSearch(path));
}
}
private List<string> recursiveMusicSearch(string path)
{
List<string> musicFiles = new List<string>();
string[] files = Directory.GetFiles(path);
for (int i = 0; i < files.Length && work; i++)
{
if (Directory.Exists(files[i]))
{
musicFiles.AddRange(recursiveMusicSearch(files[i]));
}
else
{
try
{
SupportedFormats format;
Enum.TryParse<SupportedFormats>(Path.GetExtension(files[i]).ToUpper(), out format);
musicFiles.Add(files[i]);
}
catch (ArgumentException e)
{
Console.WriteLine("MusicList", "Unsupported file format: " + Path.GetFileName(files[i]));
Console.WriteLine(e);
}
}
}
return musicFiles;
}
}
}