0

I am creating a simple MP3 player for a project, and I want to make my software play mp3 sounds. I got a dropdown combobox and a play button. What I want is the selected song to play, when the play button is pushed. (After this I am also going to script pause, next and so on buttons.

Picutre of layout:

enter image description here

And current code for the combobox to display the song names:

namespace Jukebox___Eksamensprojekt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cbSange_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] files = System.IO.Directory.GetFiles(@"C:\Programmer\Jukebox\Songs"); //Henstiller til mappen med sange

            for (int i = 0; i < files.Length; i++)
            {
                int temp = 28; //Kan ændre på tallet for at redigere hvad outputtet bliver i comboboxen
                files[i] = files[i].Substring(temp, (files[i].Length - temp)); //Sørger for jeg kun får sangnavn som output
            }

            this.cbSange.Items.AddRange(files);
        }
    }
}

EDIT: Specific question is, I have a ComboBox in which I select files (Shown in code), I also got the play button shown on the picture. What I want is, when the play button is clicked selected song will play.

EDIT. Complete code with cbSange error:

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;

namespace Jukebox___Eksamensprojekt
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        public class AudioItem
        {
            public string Name { get; set; }
            public string Path { get; set; }

            private void Form1_Load(object sender, EventArgs e)
            {
                this.cbSange.DisplayMember = "Name";

                var path = @"C:\Programmer\Jukebox\Songs";
                var files = System.IO.Directory.GetFiles(path);

                foreach (var file in files)
                {
                    var item = new AudioItem
                    {
                        Name = System.IO.Path.GetFileNameWithoutExtension(file),
                        Path = file
                    };
                    this.cbSange.Items.Add(item);
                }
            }

            private void cbSongs_SelectedIndexChanged(object sender, EventArgs e)
            {

                var selectedItem = cbSange.SelectedItem;
                if (selectedItem != null)
                {
                    var audioItem = (AudioItem)selectedItem;
                    var filePath = audioItem.Path;
                    //Use 'filePath' to open the file
                }
            }
        }
    }
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
  • 1
    Great. Keep going. Seriously: *what's the question??* – Thorsten Dittmar Apr 03 '14 at 06:39
  • How do I do it? How do I get the songs selected and make them play :) –  Apr 03 '14 at 06:45
  • I am aware of possible duplicate, but the posts I've found just doesn't work to my specific project. I want a button to play a song I selected in my combobox. All I did for now is make the name get displayed in the box, not even selecting a file. –  Apr 03 '14 at 06:51
  • Why not? That link has a lot of examples to call on. If all of them don't work then something must be seriously wrong but you haven't said what. – Steve Pettifer Apr 03 '14 at 07:08

2 Answers2

1

NAudio is an audio and midi library for .NET. It has some great features including playback, volume control and visualization, should you need it.

I have used it in WinForms projects with great success. They also have some great WinForms samples inside the package. The best part is that it is opensource.

You can find the project on CodePlex

Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
  • Thanks for the answer, but what I am looking for is some specific code to guide me along. –  Apr 03 '14 at 06:56
  • 1
    Well the clue is in the fact that NAudio is open source - that means you can get hold of the source code and have a look at how they do it. Seems to me this answer is all you need. – Steve Pettifer Apr 03 '14 at 07:07
  • Oh, I'll try open my eyes next time and noticec the "Source Code" tab next time :D I'll check it out, thanks. –  Apr 03 '14 at 07:10
  • From what I can see in your image, they already support the functionality you require. I would recommend leveraging the library and focusing your coding efforts more on your line-of-business code. Just my humble opinion :) – Elad Lachmi Apr 03 '14 at 07:31
  • Allright, I just want to point out I am very new at this, and I've never working with sound files before. I have no idea how to make all these things you say. Neither am I aware of where to put the codes I find on websites like: "WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer(); wplayer.URL = "My MP3 file.mp3"; wplayer.controls.play();" Not sure if I am completely off or what to do. –  Apr 03 '14 at 07:33
  • `NAudio` have a very nice `WinForms` sample to help you get going. – Elad Lachmi Apr 03 '14 at 07:54
0

Here's an easy way to load and select the files in the ComboBox. First, create a class to hold the name and the path of the audio files. This way you can display the name of the file in the ComboBox but the item will (invisibly) hold the path too.

public class AudioItem
{
    public string Name {get;set;}
    public string Path {get;set;}
}

In the Load event of your Form, get the files from the folder, create an instance of the AudioItem class, set the properties accordingly and add them to the ComboBox. The first line in the Load event will set the property to display in the ComboBox, otherwise it will show something like AudioItem for every item (it's a class, not a string).

private void Form1_Load(object sender, EventArgs e)
{
    this.cbSange.DisplayMember = "Name";

    var path = @"C:\Programmer\Jukebox\Songs";
    var files = System.IO.Directory.GetFiles(path);

    foreach(var file in files)
    {
        var item = new AudioItem
        {
            Name = System.IO.Path.GetFileNameWithoutExtension(file),
            Path = file
        };
        this.cbSange.Items.Add(item);
    }
}

When the selected index of the ComboBox changes, the item will be fetched and casted to an AudioItem of which you can easily get the path. This path can be used to open and play the file.

private void cbSange_SelectedIndexChanged(object sender, EventArgs e)
{
    var selectedItem = cbSange.SelectedItem;
    if(selectedItem != null)
    {
        var audioItem = (AudioItem)selectedItem ;
        var filePath = audioItem.Path;
        //Use 'filePath' to open the file
    }
}

There is no direct built-in support to play mp3 files in .NET so I suggest you use one of the suggestions already mentioned here: NAudio, DirectX, WindowsMediaPlayer, ...

Hope this helps!

Update:

You had a mix-up in your code, it should look like this:

public class AudioItem
{
    public string Name { get; set; }
    public string Path { get; set; }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.cbSange.DisplayMember = "Name";
        var path = @"C:\Programmer\Jukebox\Songs";
        var files = System.IO.Directory.GetFiles(path);

        foreach (var file in files)
        {
            var item = new AudioItem
            {
                Name = System.IO.Path.GetFileNameWithoutExtension(file),
                Path = file
            };
            this.cbSange.Items.Add(item);
        }
    }

    private void cbSongs_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedItem = cbSange.SelectedItem;
        if (selectedItem != null)
        {
            var audioItem = (AudioItem)selectedItem;
            var filePath = audioItem.Path;
            //Use 'filePath' to open the file
        }
    }
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
  • This seems very promising! Allthough I get this error with "cbSange" : Error 4 'Jukebox___Eksamensprojekt.Form1.AudioItem' does not contain a definition for 'cbSange' and no extension method 'cbSange' accepting a first argument of type 'Jukebox___Eksamensprojekt.Form1.AudioItem' could be found (are you missing a using directive or an assembly reference?) C:\Users\KROWNTHEINSANE\Documents\Visual Studio 2012\Projects\Jukebox - Eksamensprojekt\Jukebox - Eksamensprojekt\Form1.cs 40 14 Jukebox - Eksamensprojekt –  Apr 03 '14 at 09:00
  • In the next code I also seem to get error in the name foldersCB and item :/ –  Apr 03 '14 at 09:09
  • I had a few little mistakes and corrected them, could you try again? :) – Abbas Apr 03 '14 at 09:20
  • Tried them out, and it fixed the error in the last code. But I still get an error at cbSange ;) –  Apr 03 '14 at 09:23
  • Where exactly does the error occur? Post the code in your question and I'll take a look. – Abbas Apr 03 '14 at 09:25
  • It has been edited, it says it doesn't find a definition for cbSange –  Apr 03 '14 at 09:27
  • I updated my answer and corrected your code. :) – Abbas Apr 03 '14 at 09:30
  • I posted your code directly, same error with cbSange :/Edit: It works now, made a namemistake. Thank you very much! –  Apr 03 '14 at 09:31
  • Seem like I am just hammering trouble. Tried to debug it just to check, got this error: An unhandled exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll Additional information: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Jukebox___Eksamensprojekt.Form1.resources" was correctly embedded or linked into assembly "Jukebox - Eksamensprojekt" at compile time, or that all the satellite assemblies required are loadable and fully signed. –  Apr 03 '14 at 09:33
  • I'm sorry, but I think that's material for another question. This does not regard the problem of this question. :) – Abbas Apr 03 '14 at 09:38
  • Hope you being online today. I am using your code to locate the songs and putting them into my combobox. I am using your exact code, but there is nothing inside the combobox :/ –  Apr 10 '14 at 06:28
  • Are you getting any errors? Have you tried debugging with breakpoints? Is the path to the folder correct? – Abbas Apr 10 '14 at 09:03
  • Everything seems correct, no errors and path seems right aswell. Suspecting the error to be in the line: this.cbSongs.DisplayMember = "Name" not sure tho –  Apr 10 '14 at 09:24
  • Do you use the AudioItem class and fill them up, setting the properties correctly? If you're changing the propertynames of the class, make sure to use an existing property as DisplayMember. I'll try to provide a working example later. – Abbas Apr 10 '14 at 19:08
  • Would be great with a working example, because I'm a little off on the properties you're mentioning. –  Apr 11 '14 at 06:36
  • Here's a working example I created using `NAudio`: http://www.filedropper.com/naudiomp3example. Mind that it is very basic, just to show how it's done. – Abbas Apr 11 '14 at 16:59
  • The link seems dead :/ –  Apr 22 '14 at 19:59
  • I'll provide a new one tomorrow for you, good? :) – Abbas Apr 22 '14 at 21:06
  • Perfect, mich apprrciated –  Apr 22 '14 at 22:32
  • You could help me out? >. –  Apr 24 '14 at 08:24
  • I'm sorry, I forgot about it yesterday. I'll fix it this evening. – Abbas Apr 24 '14 at 08:25
  • Okay not to rush you :D It's just because it is for a project, and I am getting slightly behind when I really can't do anything before I make this part work ^^ –  Apr 24 '14 at 08:33
  • Here's the link to a working example, it's written in VS2012: https://dl.dropboxusercontent.com/u/2882292/NAudio_Mp3_Example.rar – Abbas Apr 25 '14 at 08:02
  • Has been downloaded, and will check it out soon. Thanks! –  Apr 28 '14 at 16:35