Play two sounds simultaneusly

↘锁芯ラ 提交于 2019-11-26 18:52:16

Reference PresentationCore and WindowsBase and try this...

var p1 = new System.Windows.Media.MediaPlayer();
p1.Open(new System.Uri(@"C:\windows\media\tada.wav"));
p1.Play();

// this sleep is here just so you can distinguish the two sounds playing simultaneously
System.Threading.Thread.Sleep(500);

var p2 = new System.Windows.Media.MediaPlayer();
p2.Open(new System.Uri(@"C:\windows\media\tada.wav"));
p2.Play();

EDIT I received a downvote probably because at first glance this looks like it will play the second sound after the first is finished. It doesn't, they are played by windows asynchronously. The sleep is there so if you test this code verbatim you can hear the sounds play together, it wouldn't be noticeable without the delay since they are the same sound.

This code demonstrates the two sounds playing on separate threads on top of each other, which is sort of pointless since the playback doesn't block anyway

new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"C:\windows\media\tada.wav"));
        c.Play();
    }).Start();

System.Threading.Thread.Sleep(500);

new System.Threading.Thread(() => {
        var c = new System.Windows.Media.MediaPlayer();
        c.Open(new System.Uri(@"C:\windows\media\tada.wav"));
        c.Play();
    }).Start();

http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.stop.aspx The class also has the control you need to stop playback

The "MediaPlayer" object will not let you play two sounds at once, even if you create two instances. You will need to bring in the native windows API "mciSendString".

    [DllImport("winmm.dll")]
    static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);

    public Form1()
    {
        InitializeComponent();

        mciSendString(@"open C:\Users\Jono\Desktop\applause.wav type waveaudio alias applause", null, 0, IntPtr.Zero);
        mciSendString(@"play applause", null, 0, IntPtr.Zero);

        mciSendString(@"open C:\Users\Jono\Desktop\foghorn.wav type waveaudio alias foghorn", null, 0, IntPtr.Zero);
        mciSendString(@"play foghorn", null, 0, IntPtr.Zero);

    }

check PlaySound method here http://msdn.microsoft.com/en-us/library/aa909766.aspx, and its flag SND_ASYNC .

Solution : Hi, I was developing a WP8 App and i needed multiple sounds to play simultaneously, the solutions mentioned above didnt work for me, So i used the XNA framwork. here is the link

http://msdn.microsoft.com/en-us/library/ff842408.aspx

and then play ur sound files like this...

SoundEffect Sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri("Assets/Sounds/wav/sound.wav", UriKind.Relative)).Stream);
Sound.Play();

For looping...

SoundEffectInstance Sound = SoundEffect.FromStream(Application.GetResourceStream(new Uri("Assets/Sounds/wav/sound.wav", UriKind.Relative)).Stream).CreateInstance();
Sound.IsLooped = true;
Sound.Play();

Note: the files must be in ".wav" (PCM, 8 or 16-bit, 8KHz to 48KHz, mono or stereo) format

From http://alvas.net/alvas.audio,samples.aspx#sample7 and http://alvas.net/alvas.audio,samples.aspx#sample6

Player pl = new Player();
byte[] arr = File.ReadAllBytes(@"in.wav");
pl.Play(arr);
Player pl2 = new Player();
pl2.FileName = "123.mp3";
pl2.Play();

or mix audio data before playing How to mix to mix two audio file..

private void Mix(string outfile, string infile1, string infile2, int shiftSec)
{
    WaveReader wr1 = new WaveReader(File.OpenRead(infile1));
    WaveReader wr2 = new WaveReader(File.OpenRead(infile2));
    IntPtr format1 = wr1.ReadFormat();
    WaveFormat wf = AudioCompressionManager.GetWaveFormat(format1);
    WaveWriter ww = new WaveWriter(File.Create(outfile), AudioCompressionManager.FormatBytes(format1));
    byte[] data0 = wr1.ReadData(0, shiftSec);
    byte[] data1 = wr1.ReadData(shiftSec);
    byte[] data2 = wr2.ReadData();
    byte[] mixData = AudioCompressionManager.Mix(format1, data2, data1);
    ww.WriteData(data0);
    ww.WriteData(mixData);
    ww.Close();
    wr2.Close();
    wr1.Close();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!