Tutorial on playing principles of Peking University qingbird KTV: Yuan Yuming and Yuan Yuming
Step 1:
Create a Song class
// Song name
Public string SongName {get; set ;}
// Song Lu Jin
Public string SongPath {get; set ;}
Step 2:
Create a ListSong class
// Store the songs to be played
Public static List <Song> list = new List <Song> ();
Public static int SongIndex = 0; // index of the currently played song in the Set
/// <Summary>
/// Add a song to the List set
/// </Summary>
Public static void AddSong (Song song)
{
List. Add (song );
}
/// <Summary>
/// Obtain the played song
/// </Summary>
Public static Song GetPalySong ()
{
If (list. Count> 0) à judge whether the set is an element
{
If (list. Contains (list [SongIndex]) // check whether the set Contains the list [SongIndex] song.
{
Return list [SongIndex];
}
}
Return null;
}
/// <Summary>
/// Obtain the name of the next playing song
/// </Summary>
/// <Returns> </returns>
Public static string GetNextSongName ()
{
String name = string. Empty; // defines a name field and accepts the name of the next song to be played.
If (list. Count> 1) // The element in the set must be greater than 1 to get the name of the next song
{
Name = list [SongIndex + 1]. SongName;
}
Return name;
}
/// <Summary>
/// Play the next one
/// </Summary>
Public static void MoveOn ()
{
// Judge whether the current song is not empty and the playing status is not replay. Then, execute SongIndex ++ to play the next song.
If (list [SongIndex]! = Null & list [SongIndex]. Sate = Sateze. again)
{
List [SongIndex]. ChagenSate ();
}
Else
{
SongIndex ++;
}
}
Step 3:
When you click a song in the irlview control,
// Create a Song Class Object
Song song = new Song ();
// Obtain the song name
Song. SongName = this. dataGridView1.SelectedRows [0]. Cells [3]. Value. ToString ();
// Obtain the song path strength
Song. SongPath = this. dataGridView1.SelectedRows [0]. Cells [2]. Value. ToString ();
/Call the AddSong method in ListSong to add data to the set.
ListSong. AddSong (song );
Step 4:
Define a Song class object in the main form FrmMain to accept the Song to be played
Private Song song; // The currently played Song
// Define a method to play the song to be played
/// <Summary>
/// Play a song
/// </Summary>
Public void LoadPalyedSong ()
{
This. song = ListSong. GetPalySong (); // call the GetPalySong method in the ListSong class,
If (song! = Null)
{
This. song. ChagenSate (); // Changes the song status
AxWindowsMediaPlayer1.URL = @ "E: \ KTV \ song \" + this. song. SongPath; // The playback power of the player
This. textBox1.Text = this. song. SongName; // name of the currently played song
This. textBox2.Text = ListSong. GetNextSongName (); // call the GetNextSongName method in the ListSong class to obtain the name of the next song.
}
}
There is a Timer space in the main form FrmMain, it has a Tick event, and there are several codes in its event
If (song = null) // forcibly leave song empty to play the song
{
LoadPalyedSong ();
}
// When playing the next song to determine whether the player space is in the stopped state
If (axWindowsMediaPlayer1.playState = WMPLib. WMPPlayState. wmppsStopped)
{
Song = null; // set song to null, and song points to the next song
ListSong. MoveOn (); // call the MoveOn method in the ListSong class to play the next song.
}
Step 5:
Add the song selected from the irlview space to the ListView control.
This. listView1.Items. Clear (); // first Clear the ListView control.
Foreach (Song item in ListSong. list) // traverses the entire set
{
ListViewItem lvitem = new ListViewItem (item. SongName );
Lvitem. SubItems. Add (item. SongPath );
String sate = item. Sate = Sateze. unPaly? "Not played": "played ";
Lvitem. SubItems. Add (sate );
This. listView1.Items. Add (lvitem); // bind it to the ListView control.
}