从内存中播放wav / mp3

时间:2011-06-14 08:28:27

标签: c# .net winforms memory soundplayer

我从文件播放mp3 / wav来创建推送效果。但是,在基于Atom CPU的平板电脑上,触摸按钮时会有延迟。

我会尝试从内存而不是文件系统播放wav / mp3。任何人都可以提供代码片段或线索吗?

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = System.Windows.Forms.Application.StartupPath + "\\beep-7.wav";
player.Play();

1 个答案:

答案 0 :(得分:18)

这样的东西?

public class MediaPlayer
{
    System.Media.SoundPlayer soundPlayer;

    public MediaPlayer(byte[] buffer)
    {
        var memoryStream = new MemoryStream(buffer, true);
        soundPlayer = new System.Media.SoundPlayer(memoryStream);
    }

    public void Play()
    {
        soundPlayer.Play();
    }

    public void Play(byte[] buffer)
    {
        soundPlayer.Stream.Seek(0, SeekOrigin.Begin);
        soundPlayer.Stream.Write(buffer, 0, buffer.Length);
        soundPlayer.Play();
    }
}