3

This might seem like a very basic question, but not even by researching for hours I could find out what I did wrong.

I tried to write a simple console application that is supposed to play a sound from a resource, and this is my code:

using System;
using Microsoft.DirectX.DirectSound;

namespace AudioPlayer
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Playing...");

            Device device = new Device();
            SecondaryBuffer buffer = new SecondaryBuffer(AudioPlayer.Properties.Resources.AudioFile, device);
            // AudioPlayer.Properties.Resources.AudioFile returns a stream

            buffer.Play(0, BufferPlayFlags.Default);

            Console.WriteLine("Finished");
            Console.ReadKey(true);
        }
    }
}

But all I get is a console window with no text although at least "Playing..." should be printed, and there is no sound as well.

I am using Visual Studio 2015 which provides the option to pause an application, and if I do that, it tells me that all threads are currently processing external code. Considering that, I assumed that my program got stuck while creating the device or the buffer, but then again Console.WriteLine("Playing..."); seems remaining unexecuted!

Additional inforamtions:

  • .NET framework version 4.6

  • 32-bit program

  • sound file format: WAV

  • codec: MPEG-1 Audio Layer 3 (MP3)

It would be really great if someone could explain where I failed! And sorry if this is a duplicate or if I made a really really silly mistake, but after all the time spent on this, I still can neither figure out how to solve it, nor find an already existing explanation.

  • What codec is specified in the .WAV file? Can you play the .WAV file from Windows Explorer? You might be missing a codec? –  Jan 05 '16 at 23:01
  • How do I find out the codec? –  Jan 05 '16 at 23:41
  • And yes, I can pls the file (using Windows Media Player) from Explorer. @Micky –  Jan 05 '16 at 23:45
  • Excellent, codec appears to be present. To find out which see http://superuser.com/questions/72302/how-do-i-find-out-the-audio-format-of-a-wav-file –  Jan 05 '16 at 23:57
  • @Micky Okay thanks! The file is using the MPEG-1 Audio Layer 3 codec (MP3). –  Jan 06 '16 at 09:47
  • But I think the main problem with this is that the code does not get executet _at all_! Everything works fine, until I add something from the DirectSound namespace, the program simply does _nothing_! By the way, I tried to do the same with a Windows Forms application, but in that case, the program becomes a background process... –  Jan 06 '16 at 09:55

0 Answers0