Moment For Technology

Making music player (C#)

Posted on Aug. 8, 2022, 11:47 a.m. by Rita Dunn
Category: The back-end Tag: c#
This is the fifth day of my participation in Gwen Challenge

function

Realize the basic function of the music player, you can select the file to play, also can drag the file to play. In this project,.wav files are used.

The program

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; // The Path class uses using system.media; //SoundPlayer namespace player {public partial class Form1: Form {public Form1() {InitializeComponent(); } Liststring listsongs = new Liststring(); Private void button1_Click(Object Sender, EventArgs e) {OpenFileDialog ofd = new OpenFileDialog(); Ofd. Title = "Please select music file "; // Open the dialog with the title ofd.InitialDirectory = @"F:\music"; // Set the initial setting directory ofd.Multiselect = true; / / set the multiselect ofd. Filter = @ "music file | *. Mp3 | | *. Wav all files | | *. *"; // Set the file format to filter ofd.showdialog (); String [] pa_th = ofd.filenames; For (int I = 0; i  pa_th.Length; i++ ) { listBox1.Items.Add(Path.GetFileName(pa_th[i])); Listsongs.add (pa_th[I]); listsongs.add (pa_th[I]); }} SoundPlayer sp = new SoundPlayer(); private void listBox1_DoubleClick(object sender, EventArgs e) { SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = listsongs[listBox1.SelectedIndex]; sp.Play(); }Copy the code
private void button2_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; // Get the index of the currently selected song index--; if (index 0) { index = listBox1.Items.Count-1; } listBox1.SelectedIndex = index; Sp. SoundLocation = listsongs[index]; sp.Play(); } private void button3_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; // get the index of the currently selected song index++; if (index==listBox1.Items.Count) { index = 0; } listBox1.SelectedIndex = index; Sp. SoundLocation = listsongs[index]; sp.Play(); } private void Form1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void Form1_DragDrop(object sender, DragEventArgs e) { string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop); Foreach (string file in filePath) {//file is a single file SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = file; sp.Play(); // MessageBox.Show(file); } } private void button4_Click(object sender, EventArgs e) { SoundPlayer sp = new SoundPlayer(); if (listBox1.SelectedIndex  0) sp.SoundLocation = listsongs[listBox1.SelectedIndex]; sp.Play(); } private void groupBox2_Enter(object sender, EventArgs e) { } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } } }Copy the code

Scan the QR code to obtain

More wonderful

Internet of Things knowledge

Click on it to see you look good

This article uses the article synchronization assistant to synchronize

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.