How can I play compressed sound files in C# in a portable way?

前端 未结 9 1815
陌清茗
陌清茗 2020-12-14 02:43

Is there a portable, not patent-restricted way to play compressed sound files in C# / .Net? I want to play short \"jingle\" sounds on various events occuring in the program.

9条回答
  •  一个人的身影
    2020-12-14 03:00

    I finally revisited this topic, and, using help from BrokenGlass on writing WAVE header, updated csvorbis. I've added an OggDecodeStream that can be passed to System.Media.SoundPlayer to simply play any (compatible) Ogg Vorbis stream. Example usage:

    using (var file = new FileStream(oggFilename, FileMode.Open, FileAccess.Read))
    {
      var player = new SoundPlayer(new OggDecodeStream(file));
      player.PlaySync();
    }
    

    'Compatible' in this case means 'it worked when I tried it out'. The decoder is fully managed, works fine on Microsoft .Net - at the moment, there seems to be a regression in Mono's SoundPlayer that causes distortion.

    Outdated:

    System.Diagnostics.Process.Start("fullPath.mp3");

    I am surprised but the method Dinah mentioned actually works. However, I was thinking about playing short "jingle" sounds on various events occurring in the program, I don't want to launch user's media player each time I need to do a 'ping!' sound.

    As for the code project link - this is unfortunately only a P/Invoke wrapper.

提交回复
热议问题