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

前端 未结 9 1800
陌清茗
陌清茗 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.

    0 讨论(0)
  • 2020-12-14 03:00

    i think you should have a look a fmod, which is the mother of all audio api

    please feel free to dream about http://www.fmod.org/index.php/download#FMODExProgrammersAPI

    0 讨论(0)
  • 2020-12-14 03:01

    There is a pure C# vorbis decoder available that is open source:

    http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/

    0 讨论(0)
  • 2020-12-14 03:08

    The XNA Audio APIs work well in .net/c# applications, and work beautifully for this application. Event-based triggering, along with concurent playback of multiple sounds. Exactly what you want. Oh, and compression as well.

    0 讨论(0)
  • 2020-12-14 03:11

    Not sure if this is still relevant. Simplest solution would be to use NAudio, which is a managed open source audio API written in C#. Another thing to try would be utilizing ffmpeg, and creating a process to ffplay.exe (the right binaries are under shared builds).

    0 讨论(0)
  • 2020-12-14 03:23

    Well, it depends on a patent-related laws in a given country, but there is no way to write a mp3 decoder without violating patents, as far as i know. I think the best cross-platform, open source solution for your problem is GStreamer. It has c# bindings, which evolve rapidly. Using and building GStreamer on Windows is not an easy task however. Here is a good starting point. Banshee project uses this approach, but it is not really usable on windows yet (however, there are some almost-working nightly builds). FMOD is also a good alternative. Unfortunately, it is not open source and i find that its API is somehow C-styled.

    0 讨论(0)
提交回复
热议问题