Search code examples
c#winformsfileaudiowmp

How to find combine file name with file path for WMP url?


I'm making a music player. It has two forms; first one is the typical play/stop/pause interface. second form has a checkedlistbox to choose your songs. Once I click a button in the second form, it fill a text file, with each line listing the name of each mp3 I want played. All the mp3s are in the same folder. How does one combine each line separately with the file path?

Eg:

song title: Crazy

file path: C:\Users\Me\Desktop\JAM_MACHINE\JAMS

result: C:\Users\Me\Desktop\JAM_MACHINE\JAMS\Crazy.mp3

This is what I did:

string contents = File.ReadAllText(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt");
        var fullPath = Path.GetFullPath(contents + ".mp3");

        wplayer.URL = fullPath;

Nothing happens when I press the button that should start playing the song.

I tried replacing fullPath in the third line with a fixed path, i.e. just

wplayer.URL = @"C:\Users\Me\Desktop\JAM_MACHINE\JAMS\Crazy.mp3");

and pressing the button to play it, and it works. So it's not the coding for the play button that's the problem.

In addition, when I press the button, there is supposed to be a button press sound which works when I try the second code. However there is no sound when I try the first code.

Please help me understand. Here's the full codes for your reference:

`public PLAYER()
    {
        InitializeComponent();
    }

    WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
    WMPLib.WindowsMediaPlayer playSFX = new WMPLib.WindowsMediaPlayer();
    WMPLib.WindowsMediaPlayer pauseSFX = new WMPLib.WindowsMediaPlayer(); //initialise all the sounds
    DataRecord songRecord = new DataRecord();


    private void PLAYER_Load(object sender, EventArgs e)//the event is actually PLAYER_Activated
    {

        string contents = File.ReadAllText(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt");
        var fullPath = Path.GetFullPath(contents + ".mp3");

        wplayer.URL = fullPath;
        playSFX.URL = @"C:\Users\Me\Desktop\JAM_MACHINE\PLAY.wav";
        pauseSFX.URL = @"C:\Users\Me\Desktop\JAM_MACHINE\PAUSE.wav";
        wplayer.controls.stop();
        playSFX.controls.stop();
        pauseSFX.controls.stop();

    }

    private void playbtn_Click(object sender, EventArgs e)
    {
        playSFX.controls.play();
        wplayer.controls.play();
    }

    private void pausebtn_Click(object sender, EventArgs e)
    {
        pauseSFX.controls.play();
        wplayer.controls.pause();
    }

    private void stopbtn_Click(object sender, EventArgs e)
    {
        pauseSFX.controls.play();
        wplayer.controls.stop();
        this.Visible = false;
        JAMS JAMS = new JAMS();
        JAMS.ShowDialog();

        if(JAMS.Visible == false)//if jams isnt open, open it
        {
            this.Visible = true;
        }
    }`

Solution

  • If your records.txt file looks somewhat like this:

    myFirstSong
    mySecondSong
    myThirdSong
    Crazy
    

    You can get all the filepaths by doing the following:

    private IEnumerable<string> GetFileList(string pathToRecords)
    {
        var directory = new FileInfo(pathToRecords).DirectoryName;
        var songNames = File.ReadAllLines(pathToRecords);
        var filePaths = songNames.Select(songName => Path.Combine(directory, songName + ".mp3"));
    
        return filePaths;
    }
    

    If pathToRecords is "c:\x\records.txt" that returns you a list that looks like this.

    c:\x\myFirstSong.mp3
    c:\x\mySecondSong.mp3
    c:\x\myThirdSong.mp3
    c:\x\Crazy.mp3
    

    Then you can set the player url to one of the paths in the list:

    var songPaths = GetFileList(@"c:\x\records.txt").ToList();
    var firstSong = songPaths[0];
    wplayer.URL = firstSong;
    

    And so on for the remaining songs in the list.