Play MP3 using the Media Player control

Source: Internet
Author: User
Using the Window Media Player control, I made a small mp3 Player to listen to music. Is it really enjoyable? I just wrote it today. It sounds good to listen to mp3. If you have less time to talk about it, go to the topic.

Mp3 players provide the following functions:

1. Add a song. You can add a single music or all mp3 music in the specified folder including its subfolders to the playlist.

2. delete a specified song or all songs.

3. Playback control. This includes selecting the first, next, sequential, loop, and random playback. Loop playback is divided into loop playback of a single song and loop playback of all songs.

First, create a 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 replay all
Type = 2 replay a repeat
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 the playlist, which records the numOfMusic of the total number of songs, and the serial number currentplay in the corresponding list of currently played songs; there are also four methods: Play, AddFile, DelFile, and NextPlay for the next playback sequence number.

Other main code list by 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 the recursive function showfiles to add all layer songs 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 clear the AddFile and DelFile functions in the Player class.

Implement playing the previous one

If (listBox1.SelectedIndex> = 0)
{
ListBox1.SelectedIndex --;
If (listBox1.SelectedIndex <0) listBox1.SelectedIndex = MyPlayer. NumOfMusic-1;
MyPlayer. play (listBox1.SelectedIndex + 1 );
}
Next

If (listBox1.SelectedIndex> = 0)
{
ListBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % MyPlayer. NumOfMusic;
MyPlayer. play (listBox1.SelectedIndex + 1 );
}
Playback Control

Use the value returned by the NextPlay method of Player to select the content of the next playback.

At the same time, the PlayStateChange event is used to replace one song with the next one. However, when you respond to the PlayStateChange event, you cannot directly change the Player url so that it can play the next one directly. The solution is as follows:

Private void axwindowsmediaplayer=playstatechange (object sender, AxWMPLib. _ WMPOCXEvents_PlayStateChangeEvent e)
{
If (MyPlayer. playstate = WMPLib. WMPPlayState. wmppsMediaEnded)
{
Timer1.Start ();
}
}

Private void timerjavastick (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 );
}
}
Wake up the timer when the condition of a song ends. The timer responds to the function timereffectick within Ms. In this function, you can select to play the next song smoothly.

Related Article

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.