Hello
I have made a simple music player that plays a randomized list of songs and when a song stops, it starts playing the next song:
I would like to display the current songs name on a label and when it switches to another song it displays the new song, but I'm not sure how to go about it. I was thinking maybe giving a SongLength property to every song and then using Timer.Tick somehow. I am able to display the list of songs in a listbox.
Any help would be appreciated!
I have made a simple music player that plays a randomized list of songs and when a song stops, it starts playing the next song:
MusicPlayer:
public class PlayrList
{
private Queue<string> playlist;
private IWavePlayer player;
private WaveStream fileWaveStream;
public PlayrList(List<string> startingPlaylist)
{
playlist = new Queue<string>(startingPlaylist);
}
public void PlaySong()
{
if (playlist.Count < 1)
{
return;
}
if (player != null && player.PlaybackState != PlaybackState.Stopped)
{
player.Stop();
}
if (fileWaveStream != null)
{
fileWaveStream.Dispose();
}
if (player != null)
{
player.Dispose();
player = null;
}
player = new WaveOutEvent();
fileWaveStream = new AudioFileReader(playlist.Dequeue());
player.Init(fileWaveStream);
player.PlaybackStopped += (sender, evn) => { PlaySong(); };
player.Play();
}
public void PauseSong()
{
if (player != null)
{
if (player.PlaybackState == PlaybackState.Playing)
{
player.Pause();
}
else if (player.PlaybackState == PlaybackState.Paused)
{
player.Play();
}
}
}
public void NextSong()
{
try
{
player.Dispose();
player = null;
fileWaveStream.Dispose();
}
catch (Exception ex)
{
throw new ArgumentException("Can't play Next Song", ex);
}
}
}
I would like to display the current songs name on a label and when it switches to another song it displays the new song, but I'm not sure how to go about it. I was thinking maybe giving a SongLength property to every song and then using Timer.Tick somehow. I am able to display the list of songs in a listbox.
MainForm:
static readonly List<Song> Songs = new List<Song>();
public static readonly Random rnd1 = new Random();
public List<string> Playlist { get; set; }
public PlayrList Player { get; set; }
public Form1()
{
InitializeComponent();
Resources.songs.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList().ForEach(a => Songs.Add(new Song(a)));
Playlist = new List<string>()
{
Songs[0].SongFilePath.ToString(),
Songs[1].SongFilePath.ToString(),
Songs[2].SongFilePath.ToString(),
Songs[3].SongFilePath.ToString(),
Songs[4].SongFilePath.ToString(),
Songs[5].SongFilePath.ToString()
}.OrderBy(a => rnd1.Next()).ToList();
Player = new PlayrList(Playlist);
}
private void Button1_Click(object sender, EventArgs e)
{
Player.PlaySong();
}
private void button2_Click(object sender, EventArgs e)
{
Player.NextSong();
}
Any help would be appreciated!