How do you play MP3's in DirectSound?

So far, I've figured out how to make my program play wav files using the following code:

// "sound" is the name for my DirectSound device
DS.BufferDescription desc = new DS.BufferDescription();
desc.ControlEffects = true;
DS.SecondaryBuffer buffer = new DS.SecondaryBuffer( "laser.wav", desc, sound );
buffer.Play( 0, DS.BufferPlayFlags.Default );

But if I try to use an mp3 file as my file source (ie, "music.mp3"), directX throws an exception saying "argument does not fall within the specified range".

Why How can I get my program to play MP3 files I don't mind using wav files for sound effects, but if I use wavs for background music they're going to be enormous!




Answer this question

How do you play MP3's in DirectSound?

  • Ben0s

    Thread moved. In the future please check which forum you are posting to.

    This is your friendly reminder.


  • EricPaul

    why do u wanna use directsound for mp3.

    i would recommend directx.audiovideoplayback.

    Imports Microsoft.DirectX.AudioVideoPlayback


    Private dff As Audio

    and in formload u just choose a file and set it true
    dff = New Audio("D:\Musik\Wir sind Helden\1.mp3", True)
    now its already playing.

    don't know how many resources this needs. but i think its ok for just one backgroundmusikfile

  • Mudasir

    Well DirectShow is the media api for windows - however it is very poorly supported in managed code http://www.thezbuffer.com/articles/364.aspx - there are some altenatives there too.



  • Shailesh Saini

    OH and look into the XACT stuff thats just come over the Windows from box. Not sure if/how the compression works there but its worth looking at.

  • Steve Tyson

    How about this, then I got the following code off of an internet article...

    public class Player

    {

    private string Pcommand;

    private bool isOpen;

    [DllImport("winmm.dll")]

    private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);

    public Player()

    {

    }

    public void Close()

    {

    Pcommand = "close MediaFile";

    mciSendString(Pcommand, null, 0, IntPtr.Zero);

    isOpen=false;

    }

    public void Open(string sFileName)

    {

    Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";

    mciSendString(Pcommand, null, 0, IntPtr.Zero);

    isOpen = true;

    }

    public void Play(bool loop)

    {

    if(isOpen)

    {

    Pcommand = "play MediaFile";

    if (loop)

    Pcommand += " REPEAT";

    mciSendString(Pcommand, null, 0, IntPtr.Zero);

    }

    }

    }

    This code works perfectly in an ordinary Windows Form. But in my DirectX environment, I can't get it to make a sound! I call Play() and I just never get anything. I'm using directSound to play sound effects; could this be screwing it up

    Please; I've tried so many different solutions; what's it take to play background music from MP3 files!



  • mrodriguezc

    Raw DirectSound isn't capable of playing mp3-compressed audio. I recommend using DirectShow for this purpose, as it supports a wide variety of formats. You could also use a streaming dsound buffer and do the decompression yourself, but this is probably overkill in your situation.

    Note that I don't recommend using mp3 in your project in any case, since it is not a royalty-free format. Ogg Vorbis is a good alternative.

    By the way, this doesn't really belong to "graphics" :)


  • KCTami

    Sorry about the wayward post, man, I'd already posted a graphics question before and didn't think to check which category this post landed in! I'll remember this for future questions.

    I've never heard of DirectShow before; what is it used for Is there a good tutorial out there on the web somewhere that explains how to use it (preferrably in C#, but if it's C++ I'll read that too). Also, I've heard that a few DirectX libraries like DirectMusic and DirectPlay are being depricated; is DirectShow one of these or have I just never heard of it before

    Oh yeah, and could you explain "royalty free format" Like, do you have to pay someone just for using mp3's in your game, or something



  • Matt_chrs

    Okay, this is very strange... even my SDK in the F1 help menu says I should have access to the AudioVideoPlayback namespace, but I don't appear to have it.

    I'm using the december 2005 sdk; I thought this thing's been available since last august ...



  • AllanP

    Okay, as suggested, I'm going to try taking the "Ogg" road instead of the MP3 road. Does anyone have any good code samples or links on how to load and play an ogg file (And perferably control volume + looping, since this is for background music)

  • Ken Schall

    http://www.thezbuffer.com/articles/364.aspx :

    "The whole DirectShow product was moved into the platform SDK (download) and removed from the DirectX SDK in April 2003."

    So unless you have an earlier SDK or the platform SDK it won't be there. I think this is too early for the Web Installer to include it too.



  • How do you play MP3's in DirectSound?