打在C#中的多个wav文件 [英] Playing multiple wav files in C#

查看:149
本文介绍了打在C#中的多个wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我需要发挥WAV文件时,一个键或按钮pressed或点击,我使用的SoundPlayer类的,但是当我尝试播放另一wav文件在同一时间将一个被打停止。

I have an app that I need to play a wav file when a key or button pressed or clicked, I use the SoundPlayer class but when i try to play another wav file at the same time the one that was playing stops.

有没有在同一时间播放多个wav文件的方法吗?
如果它的人可以请您给我的例子或教程?

Is there a way to play multiple wav files at the same time? If its one could you please give me examples or tutorial?

下面是我走到这一步:

private void pictureBox20_Click(object sender, EventArgs e)
{
    if (label30.Text == "Waiting 15.wav")
    {
        MessageBox.Show("No beat loaded");
        return;
    }
    using (SoundPlayer player = new SoundPlayer(label51.Text))
    {
        try
        {
            player.Play();
        }
        catch (FileNotFoundException)
        {
            MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
        }
    }
}

谢谢!

推荐答案

您可以做到这一点的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.aspx\"><$c$c>System.Windows.Media.MediaPlayer类。请注意,您需要将引用添加到 WindowsBase presentationCore

You can do this with the System.Windows.Media.MediaPlayer class. Note that you will need to add references to WindowsBase and PresentationCore.

private void pictureBox20_Click(object sender, EventArgs e)
{
    const bool loopPlayer = true;
    if (label30.Text == "Waiting 15.wav")
    {
        MessageBox.Show("No beat loaded");
        return;
    }
    var player = new System.Windows.Media.MediaPlayer();
    try
    {
        player.Open(new Uri(label51.Text));
        if(loopPlayer)
            player.MediaEnded += MediaPlayer_Loop;
        player.Play();
    }
    catch (FileNotFoundException)
    {
        MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
    }
}

编辑:您可以循环的声音通过订阅 MediaEnded 事件

void MediaPlayer_Loop(object sender, EventArgs e)
{
    MediaPlayer player = sender as MediaPlayer;
    if (player == null)
        return;

    player.Position = new TimeSpan(0);
    player.Play();
}

根据您的code,我猜你正在写某种形式的音乐制作软件。我真的不知道,这种方法会循环每一次完美的,但据我所知,这是循环使用的MediaPlayer控制的唯一途径。

Based on your code, I'm guessing that you are writing some kind of music production software. I'm honestly not sure that this method will loop perfectly every time, but as far as I can tell, it's the only way to loop using the MediaPlayer control.

这篇关于打在C#中的多个wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆