Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing Music player based on winform in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge of how to implement a music player based on winform in C#. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

First, right-click the component of the toolbox, find the selection, find the Windows Media Player component, and add it.

Design interface:

First of all, implement the basic functions

Add code to the playback pause of "Boss player"

MusicPlayer.Ctlcontrols.play (); / / play MusicPlayer.Ctlcontrols.pause (); / / pause MusicPlayer.Ctlcontrols.stop (); / / stop

First, rename the Windows Media Player control to MusicPlayer, turn off auto-playback when the program loads and give it a default address.

Private void Form1_Load (object sender, EventArgs e) {/ / when the program loads, cancel the autoplay function of the player MusicPlayer.settings.autoStart = false; MusicPlayer.URL = @ "E:\ CloudMusic\ Chen Liang-Untitled .mp3"; label1.Image = Image.FromFile (@ "C:\ Users\ 14505\ Desktop\ continue .jpg");}

Next is the button for the play button.

List list = new List (); / / full-path private void btnPlayorPause_Click (object sender, EventArgs e) {if (btnPlayorPause.Text = = "play") {if (b) {/ / get the selected song to play the music from scratch MusicPlayer.URL = list [listBox1.SelectedIndex] } MusicPlayer.Ctlcontrols.play (); btnPlayorPause.Text = "pause";} else if (btnPlayorPause.Text = = "pause") {MusicPlayer.Ctlcontrols.pause (); btnPlayorPause.Text = "play"; b = false;}}

Use the list collection to store the path to the file, and the items of the listbox control also corresponds to the list, so we can find the path in the corresponding indexed list collection and play it by clicking on the listbox selection (get its index).

Add a double-click event to listbox:

/ double-click to play the corresponding music / private void listBox1_DoubleClick (object sender, EventArgs e) {if (listBox1.Items.Count = = 0) {MessageBox.Show ("Please principle music first"); return } try {MusicPlayer.URL = list [listBox1.SelectedIndex]; MusicPlayer.Ctlcontrols.play (); btnPlayorPause.Text = "pause"; lblinformation.Text = MusicPlayer.Ctlcontrols.currentPosition.ToString ();} catch {}}

Next is to open the button, we need to open the dialog box to select the desired music file

/ Open the button / private void button4_Click (object sender, EventArgs e) {OpenFileDialog ofd = new OpenFileDialog (); ofd.Title = "Please select your file"; ofd.Filter = "Music file | * .mp3 | all files | *. *" Ofd.InitialDirectory = @ "E:\ CloudMusic"; ofd.Multiselect = true; ofd.ShowDialog (); / / get the full path string [] path = ofd.FileNames; for selected in the text box (int I = 0; I < path.Length; iTunes +) {list.Add (path [I]) / / store the file name of the music file in listbox listBox1.Items.Add (Path.GetFileName (path [I]));}}

Here is the function of the previous song and the next song. We only need to get the index of the currently selected item in the listbox control, and we can use lst

/ next song / private void button5_Click (object sender, EventArgs e) {/ / get the currently selected index int a = listBox1.SelectedIndex + 1 / / clear all selected indexes. Here, we need to clean up listBox1.SelectedIndices.Clear (); if (a = = listBox1.Items.Count) {a = 0 because we have enabled the multi-select attribute. } / / reassign the changed index to the index of the currently selected item listBox1.SelectedIndex = a; MusicPlayer.URL = list [a]; MusicPlayer.Ctlcontrols.play () } / private void button6_Click (object sender, EventArgs e) {int a = listBox1.SelectedIndex-1; listBox1.SelectedIndices.Clear () If (a < 0) {a = listBox1.Items.Count-1;} / / reassigns the changed index to the currently selected index listBox1.SelectedIndex = a; MusicPlayer.URL = list [a]; MusicPlayer.Ctlcontrols.play ();}

To add a right-click menu to the listbox control, we need the multi-select delete function.

Here, you must first clear the contents of the collection, and then clear the contents of the listbox control, otherwise it will cause an exception to the program.

/ / Click to delete the selected item / private void delete ToolStripMenuItem_Click (object sender, EventArgs e) {/ / to delete the selected item in the list / / delete the collection first / / first get the number of songs to be deleted int count = listBox1.SelectedItems.Count For (int I = 0; I < count; iTunes +) {/ / delete the collection list.RemoveAt (listBox1.SelectedIndex) first; / / delete the list listBox1.Items.RemoveAt (listBox1.SelectedIndex);}}

Next is the mute and play buttons. Here I use the label control to add pictures (Baidu can find the pictures to be played and paused on its own)

/ Click to play or mute / private void label1_Click (object sender, EventArgs e) {if (label1.Tag.ToString () = = "1") {/ / purpose: to mute you MusicPlayer.settings.mute = true / / mute / / display muted picture label1.Image = Image.FromFile (@ "C:\ Users\ 14505\ Desktop\ pause .jpg"); label1.Tag = "2";} else {MusicPlayer.settings.mute = false / / display the playback picture label1.Image = Image.FromFile (@ "C:\ Users\ 14505\ Desktop\ continue .jpg"); label1.Tag = 1;}}

Next, we need to add a function to play the next song automatically after playing.

I use all the songs here to compare all the songs with the current playing time. When the current playing time + 1 equals all the time, we will switch to the next song.

Or using bool to judge the playback status of the control is the same.

Private void timer1_Tick (object sender, EventArgs e) {/ / if the player is playing if (MusicPlayer.playState = = WMPLib.WMPPlayState.wmppsPlaying) {lblinformation.Text = MusicPlayer.currentMedia.duration.ToString () + "\ r\ n" + MusicPlayer.currentMedia.durationString + "\ r\ n" + MusicPlayer.Ctlcontrols.currentPositionString Double b1 = double.Parse (MusicPlayer.currentMedia.duration.ToString ()); double b2 = double.Parse (MusicPlayer.Ctlcontrols.currentPosition.ToString ()) + 1; / / if the current playback time of the song is equal to the total time of the song, the value if (b1) of the next song / / comparison time is automatically played.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report