C # calls the MediaPlayer control to create a music player

Source: Internet
Author: User

Using the window Media player control to make a small MP3 player to listen to music, is not very enjoyable. Just write out today, listen to mp3 feel good oh. Gossip less and get to the point.

The MP3 player mainly completes the following functions:

1. Add a song, you can add a single piece of music or the specified folder contains all the MP3 music within its subfolders to the playlist.
 
2. Delete the specified song or all songs.

3. Control of playback. This includes selecting the previous one, playing the next one, sequentially playing, looping and playing randomly. Loop playback and loop playback of a single song and all the songs.

First create the class player.

public class Player
{
Private Axwmplib.axwindowsmediaplayer Myplayer;
Private string[] playList;
private int numofmusic;
private int currentplay;

public int Numofmusic
{
Get
{
return numofmusic;
}
}

Public Wmplib.wmpplaystate Playstate
{
Get
{
return myplayer.playstate;
}
}

public string PlayList (int num)
{
return Playlist[num];
}

Public Player (Axwmplib.axwindowsmediaplayer mediaPlayer)
{
Myplayer = MediaPlayer;
PlayList = new string[1000];
Numofmusic = 0;
}

public void AddFile (string path)
{
if (Numofmusic < 1000)
{
Numofmusic + +;
Playlist[numofmusic] = path;
}
}

public void delfile (int selectnum)
{
for (int i = Selectnum i <= numOfMusic-1; i++)
{
Playlist[i] = playlist[i + 1];
}
Numofmusic--;
}

public void Play (int selectnum)
{
Myplayer.url = Playlist[selectnum];
Currentplay = Selectnum;
}

public int nextplay (int type)
{
/* Type = 0 Order

Type = 1 repeat All
Type = 2 Repeat play one song
Type = 3 Random playback

*/

Switch (type)
{
Case 0:
Currentplay + +;
if (Currentplay > Numofmusic) return 0;
else return currentplay;
Case 1:
Currentplay + +;
if (Currentplay > Numofmusic) return 1;
else return currentplay;
Case 2:
return currentplay;
Case 3:
Random RDM = new Random (unchecked ((int) DateTime.Now.Ticks));
Currentplay = RDM. Next ()% Numofmusic;
if (Currentplay = = 0) return numofmusic;
else return currentplay;
Default
return 0;
}
}
}

The Player class includes a WindowsMediaPlayer object Myplayer, an array playlist that stores playlists, a numofmusic that records the total number of songs, and an ordinal number currentplay in the current playlist of songs; In addition, there are four methods for Play,addfile,delfile, and Nextplay for the next play serial number.

To list other major codes as part of the function

Add a single song

if (this.openFileDialog1.ShowDialog () = = DialogResult.OK)
{
string path = This.openFileDialog1.FileName;
FileInfo f = new FileInfo (path);
Myplayer.addfile (F.fullname);
String strfile = Convert.ToString (Myplayer.numofmusic);
for (int i = 1;i<=5-strfile. length;i++) strfile+= ';
Strfile + = F.name;
THIS.LISTBOX1.ITEMS.ADD (strfile);
}


Add a song for a folder and all its subfolders

Use recursive function showfiles to implement all layers of songs are added to the song list.

private void Showfiles (String Path,listbox listBox1)
{
DirectoryInfo dir = new DirectoryInfo (path);
foreach (FileInfo f in dir.) GetFiles ("*.mp3"))
{
Myplayer.addfile (F.fullname);
}
foreach (DirectoryInfo f in dir.) GetDirectories ())
{
Showfiles (F.fullname,listbox1);
}


Delete and empty the AddFile and Delfile functions in direct call class player

Implement play on a song

if (listbox1.selectedindex >= 0)
{
Listbox1.selectedindex--;
if (listbox1.selectedindex <0) listbox1.selectedindex = myplayer.numofmusic-1;
Myplayer.play (Listbox1.selectedindex + 1);
}


Next song

if (listbox1.selectedindex >= 0)
{
Listbox1.selectedindex = (listbox1.selectedindex + 1)% Myplayer.numofmusic;
Myplayer.play (Listbox1.selectedindex + 1);
}


Control of playback

Use the value returned by the player's Nextplay method to select the next playback.

At the same time, the Playstatechange event is used to implement the substitution from one song to the next, but in response to the Playstatechange event, the player's URL cannot be directly played to the next, and the solution is as follows:

private void Axwindowsmediaplayer1_playstatechange (object sender, Axwmplib._wmpocxevents_playstatechangeevent e)
{
if (myplayer.playstate = = WMPLib.WMPPlayState.wmppsMediaEnded)
{
Timer1. Start ();
}
}

private void Timer1_Tick (object sender, System.EventArgs e)
{
Timer1. Stop ();
int selectnum = 0;
if (menuitem13.checked) selectnum = myplayer.nextplay (0);
else if (menuitem15.checked) Selectnum = Myplayer.nextplay (1);
else if (menuitem16.checked) Selectnum = Myplayer.nextplay (2);
else if (menuitem17.checked) Selectnum = Myplayer.nextplay (3);
if (selectnum!= 0)
{
Listbox1.selectedindex = selectnum-1;
Myplayer.play (Selectnum);
}
}


To meet the conditions of the end of a song wake-up timer, Timer 100ms response function Timer1_Tick, in this function to achieve the next song selection playback will be smooth.

The main function is completed. Immediately to listen to MP3, their own things feel is not the same OH.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.