7

With C#, How do I play (Pause, Forward...) a sound file (mp3, ogg)? The file could be on the hard disk, or on the internet.

Is there any library or Class out there that can ease me the work ?

Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
Black Horus
  • 1,043
  • 8
  • 13
  • Good Question! This sort of thing is something that so many people seem to take for granted. – theo Sep 24 '08 at 20:20
  • @ theo, I concur with you. I am actually looking for answer to similar question. – ThN Sep 30 '11 at 18:44

5 Answers5

7

If you don't mind including Microsoft.VisualBasic.dll in your project, you can do it this way:

var audio = new Microsoft.VisualBasic.Devices.Audio();
audio.Play("some file path");

If you want to do more complex stuff, the easiest way I know of is to use the Windows Media Player API. You add the DLL and then work with it. The API is kind of clunky, but it does work; I've used it to make my own music player wrapper around Windows Media Player for personal use. Here are some helpful links to get you started:

Building a Web Site with ASP .NET 2.0 to Navigate Your Music Library

Windows Media Object Model

Let the Music Play!

EDIT:

Since I wrote this, I've found an easier way, if you don't mind including WPF classes in your code. WPF (.NET 3.0 and forward) has a MediaPlayer class that's a wrapper around Windows Media Player. This means you don't have to write your own wrapper, which is nice since, as I mentioned above, the WMP API is rather clunky and hard to use.

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
4

I would recommend the BASS Library. It can play both filebased music files and streaming content. There is also a .NET wrapper available.

Magnus Johansson
  • 28,010
  • 19
  • 106
  • 164
  • Yeah, I saw that. There's even a Codeproject article on nBass ( a class wrapper of Bass). It's easy how it's done, but he do use an old version of Bass (v1.8), I'm looking for the version he used, hope I can download it. – Black Horus Sep 24 '08 at 20:40
2

Alvas.Audio has RecordPlayer class with these possibilities:

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

        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;
            }
        }
Basil
  • 205
  • 1
  • 5
0

use PlaySound API call

Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
0

There's a media player control - basically what Media Player uses. You can put that in your program and there's an API you can use to control it. I think it's the best quick solution.

kokos
  • 43,096
  • 5
  • 36
  • 32