Domanda

I am using the System.Media.SoundPlayer API to play sound files in my application.

using System.Media;

public class MySoundPlayer
{
     private SoundPlayer player = new SoundPlayer();

     public PlayFile(string file)
     {
          try 
          {
              player.SoundLocation = fileName;
              player.Load();
              player.Play();
          }
          catch (Exception e) { Debug.WriteLine(e.Message); }
     }
}

This works perfectly for nearly all WAV files. However, I found that there is one type of wav file, the RF64 type (which has an extra (but in my case always empty) 'JUNK' chunck just in case the file size might exceed 4GB. The SoundPlayer loads this file just fine, doesn't throw any exception, but doesn't play. Windows Media Player on the other hand plays it just fine, so the file is correct it seems.

As an alternative, I tried another way of playing the file:

[DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
static extern bool PlaySound(string pszSound,IntPtr hMod,SoundFlags sf);

public PlayFile(string file)
{
    // (These flags have been defined as 0x00020000 and 0x0001)
    PlaySound(filename, IntPtr.Zero, SoundFlags.SOUND_FILENAME | SoundFlags.SOUND_ASYNC); 
}

Again, this plays normal WAV files perfectly well, but gives an error 'Ding!' when it gets a RF64 file.

Is there a way to get these APIs to get to play my files? Or is there yet another Windows API that does play RF64 WAV file?

È stato utile?

Soluzione

SoundPlayer is extremely limited in its capabilities. It only plays standard WAV files, it offers no means to reposition or set the volume. You can try and see if the MediaElement from WPF will play your WAV file.

If that doesn't work, one option is NAudio, which is an open source library I maintain, and the WaveFileReader class can read RF64 files.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top