简体   繁体   中英

How can I play multiple sounds in C# simultaneously?

I'm currently using the SoundPlayer of System.Media in my Windows Forms application, but thereby I can't play multiple sounds simultaneously. I know, there are already answered questions for this topic, but if I'm trying to use the Windows Media Player:

using System.Windows.Media

I get an error:

The "System.Windows.Media" is not found ("the type or namespacename "Media" is not available in the namespace Windows.Media")

I think there is missing an assembly, but I don´t know how to add it. Isn't there an easy way to play multiple sounds at the same time?

装配经理

Where can I find the solution explorer?

Use the Quick search in the menu bar:

使用快速搜索

It will tell you where to find the entry in the menu or which shortcut to use.

How do I add a reference?

Use the quick search in the menu bar:

再次使用快速搜索

It will tell you where to find the action in the menu.

"System.Windows.Media" is not found

Can't find System.Windows.Media namespace : it's part of PresentationCore.dll . Use the search box to find it quickly:

演示核心

If you want to play multiple sounds in C# simultaneously, you can refer to the following steps: First, right click on References in Solution Explorer an choose Add Reference. 在此处输入图像描述

Second, choose Windows Media Player you can search in the upper right corner. 在此处输入图像描述

Finally, you can use the following code:

using System;
using WMPLib;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var sound1 = new WindowsMediaPlayer();
            sound1.URL = @"path of sound1";
            var sound2 = new WindowsMediaPlayer();
            sound2.URL = @"path of sound2";
            Console.ReadLine();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM