Playing Audio in .Net / C#

守給你的承諾、 提交于 2019-12-02 22:31:37

I have used this sample in several projects with good results. It is basically a .Net wrapper for Windows Waveform Audio API using P/Invoke. Other choices:

I have created a class that can play audio given Stream derivate as an input. So if you are able to pack your sound-generator into the Stream compatible interface, it could be suitable for you.

How I did it - I used unmanaged waveOut* methods from old Windows multimedia API, and handled the playback from there.

Other options - that I am aware of - use waveOut directly, from this: http://windowsmedianet.sourceforge.net/ or write your own DirectShow source filter, but that might be too complicated, since it has to be written in c++.

If you are interested in giving my component a try, I can make it available to you at no charge, since I need it beta tested (I only used it in several of my projects).

EDIT:

Since there are 6 upvotes to the question, I am offering my component free of charge (if you find useful) here: http://dl.dropbox.com/u/10020780/SimpleAudioPlayer.zip

Maybe you can reflect on it :)

Tom Wright

I use Audiere to accomplish this and find it works very well.

It's a C++ lib really, but there are a set of bindings available for C#.

For more info, see the question I asked.

You should have a look at FMOD which allows this kind of operation and much more. It is also cross platform which can be interested if you are also working on a mac.

Alvas.Audio has 3 audio players: Player

        player.FileName = "123.mp3";
        player.Play();

PlayerEx

        public static void TestPlayerEx()
        {
            PlayerEx plex = new PlayerEx();
            plex.Done += new PlayerEx.DoneEventHandler(plex_Done);
            Mp3Reader mr = new Mp3Reader(File.OpenRead("in.mp3"));
            IntPtr format = mr.ReadFormat();
            byte[] data = mr.ReadData();
            mr.Close();
            plex.OpenPlayer(format);
            plex.AddData(data);
            plex.StartPlay();
        }

        static void plex_Done(object sender, DoneEventArgs e)
        {
            if (e.IsEndPlaying)
            {
                ((PlayerEx)sender).ClosePlayer();
            }
        }

and RecordPlayer

        public static void TestRecordPlayer()
        {
            RecordPlayer rp = new RecordPlayer();
            rp.PropertyChanged += new PropertyChangedEventHandler(rp_PropertyChanged);
            rp.Open(new Mp3Reader(File.OpenRead("in.mp3")));
            rp.Play();
        }

        static void rp_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case RecordPlayer.StateProperty:
                    RecordPlayer rp = ((RecordPlayer)sender);
                    if (rp.State == DeviceState.Stopped)
                    {
                        rp.Close();
                    }
                    break;
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!