Error trying to play an MP3 file in C #

0

I want to run an .MP3 file in my C # application, but whenever I run the timerson method, I get an error that tells me that WindowsMediaPlayer is in use .

I do not know how to close the instance or kill that process because it does not let me continue in my code.

This is the current code:

public void timerson()
{
    MessageBox.Show("Cada cierto tiempo segundos");
    WindowsMediaPlayer myplayer = new WindowsMediaPlayer(); -- Error
    myplayer.URL = @"C:\Users\david\Music\Cancion.mp3";
    myplayer.controls.play();
}

public void ejecutar()
{
    var timer = new System.Threading.Timer(
        e => timerson(),
        null,
        TimeSpan.Zero,
        TimeSpan.FromMinutes(0.3));
}
    
asked by David 30.04.2017 в 18:03
source

1 answer

2

You could leave the class instantiated and simply play with the same instance of the player:

class Clase
{
    WindowsMediaPlayer myplayer;

    public Clase()
    {
        myplayer = new WindowsMediaPlayer();
    }

    public void timerson()
    {
        MessageBox.Show("Cada cierto tiempo segundos");
        myplayer.controls.Stop();
        myplayer.URL = @"C:\Users\david\Music\Cancion.mp3";
        myplayer.controls.play();
    }

    public void ejecutar()
    {
        var timer = new System.Threading.Timer(
            e => timerson(),
            null,
            TimeSpan.Zero,
            TimeSpan.FromMinutes(0.3));
    }
}
    
answered by 30.04.2017 / 18:19
source