0

I have tried this in my class constructor to test the windows media player for playing sounds (specifically mp3) from files inside the .exe resource folder. It writes the file correctly to the temp diectory and the file plays just fine if launched from the folder both in WMP and VLC.

But I still cannot get the sound to play in the app.

 class AudioPlayer
{
    private static string _tempFolder; 
    private static string _filename;
    private string _musicPath;
    public WindowsMediaPlayer _wmp;

    public AudioPlayer()
    {
        _tempFolder = Path.GetTempPath();
        _filename = "Music1.mp3";
        _musicPath = Path.Combine(_tempFolder,_filename);
        CopyResource(_musicPath, Resources.Music1);
        _wmp = new WindowsMediaPlayer();
        _wmp.URL = _musicPath;
        _wmp.controls.play();
    }

    private void CopyResource(string resourceName, byte[] file)
    {
        File.WriteAllBytes(resourceName, file);
    }
}

Would this be a threading issue? I would think not since this is a form creating the audio player not a console app so it does not terminate immediately. I also get no errors indicating an issue with playback.

2 Answers2

0

I Threaded out the operation and Audio Player from the main program and it worked fine. I might add that .Wav is highly inefficient for filesizes but WMP is too slow to load .mp3 files. I would imagine this is largely to do with file compression compromising speed for decent file sizes. The final solution was this.

class AudioPlayer
{
    private static string _tempFolder; 
    private static string _filename;
    private string _musicPath;
    public WindowsMediaPlayer _wmp;

    public AudioPlayer()
    {
        _tempFolder = Path.GetTempPath();
        _filename = "Music1.mp3";
        _musicPath = Path.Combine(_tempFolder,_filename);
        CopyResource(_musicPath, Resources.Music1);
        _wmp = new WindowsMediaPlayer();
        _wmp.URL = _musicPath;

    }

    private void CopyResource(string resourceName, byte[] file)
    {
        File.WriteAllBytes(resourceName, file);
    }

    public void Play()
    {
        _wmp.controls.play();
    }

    public void Stop()
    {
        _wmp.controls.stop();
    }
}

And I call it from the program Main like so.

_ap = new AudioPlayer();
_audioThread = new Thread(_ap.Play);
_audioThread.Start();

And I call

_ap.Stop();
_audioThread.Join();

During the program save and exit method to terminate it.

  • I have since found that when I remove the threading and call _ap.Play(); from outside the AudioPlayer class it works the same as the threaded model. None of this makes sense. – That Homeless Guy Jan 28 '16 at 18:53
0

A form is required to use WMPLib. If you are on a console application, [STAThread] and Application.Run() should fix it.

void playSong()
{
    System.Threading.Thread musicThread = new System.Threading.Thread(() =>
    {
        WMPLib.WindowsMediaPlayer windowsMediaPlayer = new WMPLib.WindowsMediaPlayer();
        windowsMediaPlayer.URL = "MySong.mp3";
        windowsMediaPlayer.settings.setMode("loop", true); /* Play in a loop. Remove this line if you don't want to play the song in a loop. */
        windowsMediaPlayer.controls.play();
        System.Windows.Forms.Application.Run();
    });
    musicThread.IsBackground = true;
    musicThread.Start();

    Console.WriteLine("The song is playing. Press any key to exit...");
    Console.ReadKey();
    Environment.Exit(0);
}

[STAThread]
static void Main(string[] args) => new Program().playSong();
3dsbros64
  • 1
  • 2