在C#中播放声音-使用哪种方法?

在C#中播放声音-使用哪种方法?,c#,.net,visual-studio,audio,C#,.net,Visual Studio,Audio,我正在尝试编写一个小小的C#.net应用程序,当单击特定按钮时,它会随机播放文件夹中的声音 因此,我花了一些时间寻找正确的方法来实现这一点。在我的脑海中,“只是播放一个声音”似乎没有那么复杂,但有很多方法可以做到这一点 请纠正我现在要说的每一句话: 这是一种非常古老的方式,没有音量、多重声音或循环等控制 ,将文本注释发送到MCI,播放不同格式,支持Volumecontrol,甚至可以同时发送多个声音,发送字符串命令而不是使用类不是很方便,可能需要一个格式良好的包装类(任何人?:D) 类,支持异

我正在尝试编写一个小小的C#.net应用程序,当单击特定按钮时,它会随机播放文件夹中的声音

因此,我花了一些时间寻找正确的方法来实现这一点。在我的脑海中,“只是播放一个声音”似乎没有那么复杂,但有很多方法可以做到这一点

请纠正我现在要说的每一句话:

  • 这是一种非常古老的方式,没有音量、多重声音或循环等控制
  • ,将文本注释发送到MCI,播放不同格式,支持Volumecontrol,甚至可以同时发送多个声音,发送字符串命令而不是使用类不是很方便,可能需要一个格式良好的包装类(任何人?:D)
  • 类,支持异步,无需进一步操作,无卷,无循环
  • 类,支持甚至超过音频、.NET4.0、音量,但需要一些“特殊处理”才能同时处理多个声音
  • ,似乎支持声音所能想象的一切,代码看起来太复杂,无法“简单地播放声音”
好吧,就是这样,也许我错过了一个,但总的来说。。。我还没被一个说服

我想启动、停止、暂停自定义.wav.mp3声音,不仅仅是在彼此之后,有时甚至是在同一时间,在它们运行的过程中改变音量,可能还有一些静音功能来停止所有声音。听起来很容易,但不是吗

感谢您提前提供的任何提示


Harry

我想说,
MediaPlayer
类是最健壮的,也是最简单的。我做了一些音频/视频播放,你几乎可以用它做任何事情。由于您需要播放多个文件
SoundPlayer
是一个明显的选择,因此您可能可以同时播放这两种文件。 但是,您可以有多个MediaPlayer实例并播放不同的声音


您似乎已经完成了这项研究,基本上可以最好地回答您的问题。

您要求在加载指定声音文件的情况下启动新的Windows Media player进程是否更有意义? 您可以使用System.Diagnostics.Process类完成此操作

Process.Start("wmplayer.exe", "C:\\myPath\\mySound.mp3");

您可以在WPF中使用媒体播放器控件,该控件非常容易使用,并且可以根据您的需要进行自定义

如果您只是使用windows窗体,您可以使用windows media player ActiveX控件,并对其进行一些自定义,以拥有自己的用户界面,用于播放/暂停/前进/后退等


与上述两个选项相比,开发DirectX对一个人来说没有什么困难。

我扩展了我在这里找到的一个类: 并使其易于使用多种声音,保持可控

这几乎是不言自明的,但是可能有人在找到这个线程时使用这段代码会安全一些,或者可能有一些主要的东西可以改进它:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace someNameHere
{
    public class MP3Player
    {
        private string Pcommand, FName,alias;
        private bool Opened, Playing, Paused, Loop,
                     MutedAll, MutedLeft, MutedRight;
        private int rVolume, lVolume, aVolume,
                    tVolume, bVolume, VolBalance;
        private ulong Lng;
        private long Err;
        private static int counter = 0;
        public static List<MP3Player> currentlyActive = new List<MP3Player>();
        public static List<MP3Player> lastFiveActive = new List<MP3Player>();

        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand,
                StringBuilder strReturn, int iReturnLength,
                IntPtr hwndCallback);

        public MP3Player()
        {
            Opened = false;
            Pcommand = "";
            FName = "";
            Playing = false;
            Paused = false;
            Loop = false;
            MutedAll = MutedLeft = MutedRight = false;
            rVolume = lVolume = aVolume =
                      tVolume = bVolume = 1000;
            Lng = 0;
            VolBalance = 0;
            Err = 0;
            counter++;
            alias = "alias" + counter.ToString();
            currentlyActive = cleanUpActive();
            currentlyActive.Add(this);
        }

         ~MP3Player()
        {
            currentlyActive.Remove(this);
        }


        private List<MP3Player> cleanUpActive()
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            cachedList = (from c in cachedList where c.AudioLength == c.CurrentPosition select c).ToList();

            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.Stop();
                eachSound.Close();
            }

            return (from c in currentlyActive where c.AudioLength != c.CurrentPosition select c).ToList();
        }

         public static void playRandomFromFolder(string relpath, bool checkIfInLast)
         {
             try
             {
                 string currentdir = Environment.CurrentDirectory;
                 string[] Files = Directory.GetFiles(currentdir + relpath);
                 Random randNum = new Random();
                 List<string> list_paths = (from c in lastFiveActive select c.FileName).ToList();

                 string randomFile = "";
                 int i = 0;
                 while (true)
                 {
                     int zufall = randNum.Next(0, Files.Length);
                     if (!list_paths.Contains(Files[zufall]) || i > 10)
                     {
                         randomFile = Files[zufall];
                         break;
                     }

                 }

                 MP3Player playIt = new MP3Player();
                 playIt.Open(randomFile);
                 playIt.Play();
             }
             catch (Exception err)
             {
               //  MessageBox.Show(err.Message);
             }

         }



        #region AllActiveFunctions

        public static void stopAllActive()
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.Stop();
            }

        }
        public static void pauseAllActive()
        {
            foreach (MP3Player eachSound in currentlyActive)
            {
                eachSound.Pause();
            }

        }
        public static void playAllActive()
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.Play();
            }

        }
        public static void setVolumeAllActive(int i)
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.VolumeAll = i;
            }

        }
        public static void setVolumeLeft(int i)
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.VolumeLeft = i;
            }

        }
        public static void setVolumeRight(int i)
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.VolumeRight = i;
            }

        }
        public static void setVolumeTreble(int i)
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.VolumeTreble = i;
            }

        }
        public static void setVolumeBass(int i)
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.VolumeBass = i;
            }

        }

        public static void setBalance(int i)
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            foreach (MP3Player eachSound in cachedList)
            {
                eachSound.Balance = i;
            }

        }

        public static void stopLast()
        {
            List<MP3Player> cachedList = new List<MP3Player>(currentlyActive);
            cachedList.Last().Stop();
        }
        #endregion

        #region Volume
        public bool MuteAll
        {
            get
            {
                return MutedAll;
            }
            set
            {
                MutedAll = value;
                if (MutedAll)
                {
                    Pcommand = "setaudio " + alias + " off";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
                else
                {
                    Pcommand = "setaudio " + alias + " on";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }

        }

        public bool MuteLeft
        {
            get
            {
                return MutedLeft;
            }
            set
            {
                MutedLeft = value;
                if (MutedLeft)
                {
                    Pcommand = "setaudio " + alias + " left off";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
                else
                {
                    Pcommand = "setaudio " + alias + " left on";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }

        }

        public bool MuteRight
        {
            get
            {
                return MutedRight;
            }
            set
            {
                MutedRight = value;
                if (MutedRight)
                {
                    Pcommand = "setaudio " + alias + " right off";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
                else
                {
                    Pcommand = "setaudio " + alias + " right on";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }

        }

        public int VolumeAll
        {
            get
            {
                return aVolume;
            }
            set
            {
                if (Opened && (value >= 0 && value <= 1000))
                {
                    aVolume = value;
                    Pcommand = String.Format("setaudio " + alias + "" +
                               " volume to {0}", aVolume);
                    if ((Err = mciSendString(Pcommand, null, 0,
                                          IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }
        }

        public int VolumeLeft
        {
            get
            {
                return lVolume;
            }
            set
            {
                if (Opened && (value >= 0 && value <= 1000))
                {
                    lVolume = value;
                    Pcommand = String.Format("setaudio " + alias + "" +
                               " left volume to {0}", lVolume);
                    if ((Err = mciSendString(Pcommand, null, 0,
                                           IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }
        }

        public int VolumeRight
        {
            get
            {
                return rVolume;
            }
            set
            {
                if (Opened && (value >= 0 && value <= 1000))
                {
                    rVolume = value;
                    Pcommand = String.Format("setaudio" +
                               " " + alias + " right volume to {0}", rVolume);
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }
        }

        public int VolumeTreble
        {
            get
            {
                return tVolume;
            }
            set
            {
                if (Opened && (value >= 0 && value <= 1000))
                {
                    tVolume = value;
                    Pcommand = String.Format("setaudio " + alias + "" +
                                             " treble to {0}", tVolume);
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }
        }

        public int VolumeBass
        {
            get
            {
                return bVolume;
            }
            set
            {
                if (Opened && (value >= 0 && value <= 1000))
                {
                    bVolume = value;
                    Pcommand = String.Format("setaudio " + alias + " bass to {0}",
                                             bVolume);
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                }
            }
        }

        public int Balance
        {
            get
            {
                return VolBalance;
            }
            set
            {
                if (Opened && (value >= -1000 && value <= 1000))
                {
                    VolBalance = value;
                    if (value < 0)
                    {
                        Pcommand = "setaudio " + alias + " left volume to 1000";
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                        Pcommand = String.Format("setaudio " + alias + " right" +
                                                 " volume to {0}", 1000 + value);
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                    }
                    else
                    {
                        Pcommand = "setaudio " + alias + " right volume to 1000";
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                        Pcommand = String.Format("setaudio " + alias + "" +
                                   " left volume to {0}", 1000 - value);
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                    }
                }
            }
        }
        #endregion

        #region Main Functions

        public string FileName
        {
            get
            {
                return FName;
            }
        }

        public bool Looping
        {
            get
            {
                return Loop;
            }
            set
            {
                Loop = value;
            }
        }

        public void Seek(ulong Millisecs)
        {
            if (Opened && Millisecs <= Lng)
            {
                if (Playing)
                {
                    if (Paused)
                    {
                        Pcommand = String.Format("seek " + alias + " to {0}", Millisecs);
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                    }
                    else
                    {
                        Pcommand = String.Format("seek " + alias + " to {0}", Millisecs);
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                        Pcommand = "play " + alias + "";
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                    }
                }
            }
        }

        private void CalculateLength()
        {
            StringBuilder str = new StringBuilder(128);
            mciSendString("status " + alias + " length", str, 128, IntPtr.Zero);
            Lng = Convert.ToUInt64(str.ToString());
        }

        public ulong AudioLength
        {
            get
            {
                if (Opened) return Lng;
                else return 0;
            }
        }

        public void Close()
        {
            if (Opened)
            {
                Pcommand = "close " + alias + "";
                if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                    OnError(new ErrorEventArgs(Err));
                Opened = false;
                Playing = false;
                Paused = false;
                OnCloseFile(new CloseFileEventArgs());
            }
        }

        public void Open(string sFileName)
        {
            if (!Opened)
            {
                Pcommand = "open \"" + sFileName +
                           "\" type mpegvideo alias " + alias + "";
                if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                    OnError(new ErrorEventArgs(Err));
                FName = sFileName;
                Opened = true;
                Playing = false;
                Paused = false;
                Pcommand = "set " + alias + " time format milliseconds";
                if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                    OnError(new ErrorEventArgs(Err));
                Pcommand = "set " + alias + " seek exactly on";
                if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                    OnError(new ErrorEventArgs(Err));
                CalculateLength();
                OnOpenFile(new OpenFileEventArgs(sFileName));
            }
            else
            {
                this.Close();
                this.Open(sFileName);
            }
        }

        private void stackLastFive(MP3Player latest)
        {
            if (lastFiveActive.Count() > 5)
            {
              lastFiveActive.Reverse();
              lastFiveActive = lastFiveActive.Take(4).ToList();
            }
            lastFiveActive.Add(latest);

        }

        public void Play()
        {
            if (Opened)
            {
                if (!Playing)
                {
                    Playing = true;
                    Pcommand = "play " + alias + "";
                    if (Loop) Pcommand += " REPEAT";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                    OnPlayFile(new PlayFileEventArgs());
                }
                else
                {
                    if (!Paused)
                    {
                        Pcommand = "seek " + alias + " to start";
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                        Pcommand = "play " + alias + "";
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                        OnPlayFile(new PlayFileEventArgs());
                    }
                    else
                    {
                        Paused = false;
                        Pcommand = "play " + alias + "";
                        if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                            OnError(new ErrorEventArgs(Err));
                        OnPlayFile(new PlayFileEventArgs());
                    }
                }
                stackLastFive(this);
            }
        }

        public void Pause()
        {
            if (Opened)
            {
                if (!Paused)
                {
                    Paused = true;
                    Pcommand = "pause " + alias + "";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                    OnPauseFile(new PauseFileEventArgs());
                }
                else
                {
                    Paused = false;
                    Pcommand = "play " + alias + "";
                    if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                    OnPlayFile(new PlayFileEventArgs());
                }
            }
        }

        public void Stop()
        {
            if (Opened && Playing)
            {
                Playing = false;
                Paused = false;
                Pcommand = "seek " + alias + " to start";
                if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                    OnError(new ErrorEventArgs(Err));
                Pcommand = "stop " + alias + "";
                if ((Err = mciSendString(Pcommand, null, 0, IntPtr.Zero)) != 0)
                    OnError(new ErrorEventArgs(Err));
                currentlyActive.Remove(this);
                OnStopFile(new StopFileEventArgs());
            }
        }

        public ulong CurrentPosition
        {
            get
            {
                if (Opened && Playing)
                {
                    StringBuilder s = new StringBuilder(128);
                    Pcommand = "status " + alias + " position";
                    if ((Err = mciSendString(Pcommand, s, 128, IntPtr.Zero)) != 0)
                        OnError(new ErrorEventArgs(Err));
                    return Convert.ToUInt64(s.ToString());
                }
                else return 0;
            }
        }

        #endregion

        #region Event Handling

        public delegate void OpenFileEventHandler(Object sender,
                             OpenFileEventArgs oea);

        public delegate void PlayFileEventHandler(Object sender,
                             PlayFileEventArgs pea);

        public delegate void PauseFileEventHandler(Object sender,
                             PauseFileEventArgs paea);

        public delegate void StopFileEventHandler(Object sender,
                                         StopFileEventArgs sea);

        public delegate void CloseFileEventHandler(Object sender,
                                         CloseFileEventArgs cea);

        public delegate void ErrorEventHandler(Object sender,
                                         ErrorEventArgs eea);

        public event OpenFileEventHandler OpenFile;

        public event PlayFileEventHandler PlayFile;

        public event PauseFileEventHandler PauseFile;

        public event StopFileEventHandler StopFile;

        public event CloseFileEventHandler CloseFile;

        public event ErrorEventHandler Error;

        protected virtual void OnOpenFile(OpenFileEventArgs oea)
        {
            if (OpenFile != null) OpenFile(this, oea);
        }

        protected virtual void OnPlayFile(PlayFileEventArgs pea)
        {
            if (PlayFile != null) PlayFile(this, pea);
        }

        protected virtual void OnPauseFile(PauseFileEventArgs paea)
        {
            if (PauseFile != null) PauseFile(this, paea);
        }

        protected virtual void OnStopFile(StopFileEventArgs sea)
        {
            if (StopFile != null) StopFile(this, sea);
        }

        protected virtual void OnCloseFile(CloseFileEventArgs cea)
        {
            if (CloseFile != null) CloseFile(this, cea);
            if (currentlyActive.Contains(this))
            {
                currentlyActive.Remove(this);
            }
        }

        protected virtual void OnError(ErrorEventArgs eea)
        {
            if (Error != null) Error(this, eea);
        }


    }

    public class OpenFileEventArgs : EventArgs
    {
        public OpenFileEventArgs(string filename)
        {
            this.FileName = filename;
        }
        public readonly string FileName;
    }

    public class PlayFileEventArgs : EventArgs
    {
        public PlayFileEventArgs()
        {

        }
    }

    public class PauseFileEventArgs : EventArgs
    {
        public PauseFileEventArgs()
        {
        }
    }

    public class StopFileEventArgs : EventArgs
    {
        public StopFileEventArgs()
        {
        }
    }

    public class CloseFileEventArgs : EventArgs
    {
        public CloseFileEventArgs()
        {
        }
    }

    public class ErrorEventArgs : EventArgs
    {
        public ErrorEventArgs(long Err)
        {
            this.ErrNum = Err;
        }

        public readonly long ErrNum;
    }
     #endregion
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Runtime.InteropServices;
使用System.IO;
名称空间someNameHere
{
公共类MP3Player
{
私有字符串Pcommand,FName,别名;
二等兵布尔打开,播放,暂停,循环,
MutedAll,MutedLeft,MutedRight;
私家车,私家车,私家车,
tVolume、bVolume、VolBalance;
私人乌龙液化天然气;
私人长犯;
专用静态整数计数器=0;
public static List currentlyActive=新列表();
public static List lastFiveActive=新列表();
[DllImport(“winmm.dll”)]
私有静态外部长mciSendString(字符串strCommand,
StringBuilder strReturn,int-iReturnLength,
IntPtr hwndCallback);
公共MP3Player()
{
开放=假;
Pcommand=“”;
FName=“”;
玩=假;
暂停=错误;
循环=假;
MutedAll=MutedLeft=MutedRight=false;
rVolume=lVolume=aVolume=
tVolume=bVolume=1000;
液化天然气=0;
VolBalance=0;
误差=0;
计数器++;
alias=“alias”+counter.ToString();
currentlyActive=cleanUpActive();
currentlyActive.Add(这个);
}
~MP3Player()
{
当前激活。移除(此);
}
私有列表清理活动()
{
列表缓存列表=新列表(currentlyActive);
cachedList=(从cachedList中的c开始,其中c.AudioLength==c.CurrentPosition选择c);
foreach(缓存列表中的MP3plyer每个声音)
{
eachSound.Stop();
eachSound.Close();
}
返回(从currentlyActive中的c开始,其中c.AudioLength!=c.CurrentPosition选择c).ToList();
}
公共静态void文件夹(字符串relpath,bool checkIfInLast)
{
尝试
{
字符串currentdir=Environment.CurrentDirectory;
string[]Files=Directory.GetFiles(currentdir+relpath);
Random randNum=新的Random();
List_path=(从lastFiveActive中的c选择c.FileName).ToList();
字符串randomFile=“”;
int i=0;
while(true)
{
int zufall=randNum.Next(0,Files.Length);
如果(!list|u path.Contains(Files[zufall])|i>10)
{
随机文件=文件[zufall];
打破
}
}
MP3Player playIt=新的MP3Player();
打开(随机文件);
playIt.Play();
}
捕获(异常错误)
{
//MessageBox.Show(错误消息);
}
}
#再