Playlist
WMP Playlist - Creating a playlist without writing an XML file is very simple in C# provided
you know the API. I couldn't find this information anywhere and eventually had someone else
show me how. Here is a small example:
mediaPlaylist.cs
public WMPLib.IWMPPlaylist playlist; //Declare a WMP Playlist.
public WMPLib.IWMPMedia temp; //Declare a media element to add to the playlist.
public AxWMPLib.AxWindowsMediaPlayer MediaPlayer; //Declare a media player.
Function()
{
MediaPlayer = new AxWMPLib.AxWindowsMediaPlayer(); //Create media player.
string SongURL = "C:\Documents and Settings\Name\My Documents\My Music\blah.mp3";
temp = this.MediaPlayer.newMedia(SongURL); //Load media from URL.
playlist.appendItem(temp); //Add song to playlist.
MediaPlayer.settings.autoStart = true; //not necessary
MediaPlayer.currentPlaylist = p; //Set media player to use the playlist.
}
|
And that's it. Not too difficult, yet it doesn't seem to really be covered anywhere. Everyone wants to unnecessarily use
an XML playlist. What I like to do in my programs is just have a music directory and when you start the program it just reads
all the songs in the folder and builds the playlist then.
My C# Playlist - I created my own playlist for a more straight-forward way to handle the songs. I also have a lot more
control this way. With all of the Get/Set Methods it is a little long. But you can download the class here:
Playlist.cs.
It basically works by storing an ArrayList of all the song URLs, then plays each song individually. When the song is
finished it fires a song ended mediaplayer event, which I catch and then tell it to play the next song. There is also
a timer in the class which is a necesary evil. When I catch the stop event I can't tell it to play the next song because
that would fire another media player event from within the function. So instead I start a timer and then in the timer
function I stop the timer and advance the playlist.
public class Playlist
{
public bool SongEnded=true;
private System.Windows.Forms.Timer CheckSong;
private System.ComponentModel.IContainer play_components;
ArrayList SongsInPlaylist = new ArrayList();
private int Index = 0;
public AxWMPLib.AxWindowsMediaPlayer MediaPlayer;
public Playlist(AxWMPLib.AxWindowsMediaPlayer Player) {
MediaPlayer = Player;
Index = 0;
this.play_components = new System.ComponentModel.Container();
this.CheckSong = new System.Windows.Forms.Timer(this.play_components);
this.CheckSong.Tick += new System.EventHandler(this.CheckSong_Tick);
MediaPlayer.PlayStateChange +=
new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(MediaPlayer_PlayStateChange);
Play();
}
public void AddSongs(string[] Songs) {
for( int i=0; i< Songs.Length ; i++)
AddSong(Songs[i]);
}
public void AddSong(string Song) {
SongsInPlaylist.Add(Song);
}
public int Volume {
set { MediaPlayer.settings.volume = value; }
get { return MediaPlayer.settings.volume; }
}
public void DeleteSong(string Song)
{
if(Song == SongsInPlaylist[Index].ToString()) {
MediaPlayer.Ctlcontrols.stop();
Index--;
}
SongsInPlaylist.Remove(Song);
MediaPlayer.Ctlcontrols.play();
}
public void DeletePlaylist() {
MediaPlayer.Ctlcontrols.stop();
SongsInPlaylist.Clear();
Index = 0;
}
public void Play() {
if(SongsInPlaylist[Index] != null)
MediaPlayer.URL = SongsInPlaylist[Index].ToString();
}
public void Play(int Slot) {
if(SongsInPlaylist[Slot-1] != null)
MediaPlayer.URL = SongsInPlaylist[Slot-1].ToString();
}
public void Play(string name) {
int slot = SongsInPlaylist.BinarySearch(name,null);
if(slot >= 0 && slot < SongsInPlaylist.Count)
MediaPlayer.URL = SongsInPlaylist[slot].ToString();
}
public void Pause() {
MediaPlayer.Ctlcontrols.pause();
}
public void Stop() {
MediaPlayer.Ctlcontrols.stop();
}
public void NextSong() {
if(Index != SongsInPlaylist.Count - 1) {
Index++;
MediaPlayer.Ctlcontrols.stop();
MediaPlayer.URL = SongsInPlaylist[Index].ToString();
MediaPlayer.Ctlcontrols.play();
}
else {
Index = 0;
MediaPlayer.Ctlcontrols.stop();
MediaPlayer.URL = SongsInPlaylist[0].ToString();
MediaPlayer.Ctlcontrols.play();
}
}
public void PrevSong() {
if(Index != 0) {
Index--;
MediaPlayer.Ctlcontrols.stop();
MediaPlayer.URL = SongsInPlaylist[Index].ToString();
MediaPlayer.Ctlcontrols.play();
}
else {
Index = SongsInPlaylist.Count - 1;
MediaPlayer.Ctlcontrols.stop();
MediaPlayer.URL = SongsInPlaylist[Index].ToString();
MediaPlayer.Ctlcontrols.play();
}
}
private void CheckSong_Tick(object sender, System.EventArgs e) {
if(SongEnded) {
NextSong();
SongEnded = false;
CheckSong.Stop();
}
}
public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) {
switch(MediaPlayer.playState) {
case WMPLib.WMPPlayState.wmppsMediaEnded:
SongEnded = true;
CheckSong.Start();
break;
default:
break;
}
}
}
|