XNA Creators Club Online
Page 1 of 1 (17 items)
Sort Posts: Previous Next

? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

Last post 5/19/2009 2:02 PM by Goffmog. 16 replies.
  • 11/12/2008 12:52 AM

    ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    ???

    I've seen some posts around, but, I didin't see anything specific enough yet ... (or, maybe I missed it) ...

    Is there any Code/Tutorial/Demo for the New XNA 3 "Simple Sound API" ?

    Specifically I want to use a Small MP3 file to create background music (with infinite looping) ...

    Any Links / Hints / etc ?

    I'd like to add it to an existing solution I already have in XNA 3.

    ???


    http://games.archor.com
  • 11/12/2008 2:50 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    There's a few things you have to do to make this work. The first is to make sure you're loading your MP3 file as a "Song" object.

    So somewhere in your class declare an object like this.

                Song mySong;

    Then in your LoadContent method, load that song like this.

                mySong = Content.Load<Song>("myFavoriteMP3");


    Finally, you have to play your song through the media player and then set the media player to repeat. You do that like this.

                MediaPlayer.Play(mySong);
                MediaPlayer.IsRepeating = true;


    And that should be all you need to get your song to repeat. Hope that helps!





  • 11/12/2008 8:52 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    I'll give it a Try . . .

    Thanks !

    http://games.archor.com
  • 2/7/2009 6:36 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    George,

    I tried your suggestion and get a "ContentLoadException was unhandled" message. Any thoughts on why that is coming up?

    Thanks,
    Brian
  • 2/7/2009 5:01 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    Are you sure you're creating an XNA 3.0 project? When you look at the mp3 file in your Content folder what Content Processor is it set to use in it's Properties (should be set to Song).
  • 2/7/2009 10:15 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    It works now, thanks for the help George! I think there were two problems - the first is that I was using a .wav that was previously an .mp3. The other is that I had the .mp3 loaded into Content>Audio instead of just into the Content method.

    Do you happen to have any links for good audio tutorials? I've been searching high and low and haven't found one that really covers the fundamentals well.
  • 2/7/2009 11:48 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    For using the SoundEffect and Music content objects I just used the Help documentation installed with the XNA framework. The documentation is actually pretty good for everything about the XNA framework. I'm not really sure if there's any tutorials out there, I haven't seen a lot about audio covered in the community over the years. Maybe some others can pop in and offer some specific tutorials they've found useful, but off the top of my head I can't think of one.
  • 2/9/2009 5:05 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    George Clingerman:
    There's a few things you have to do to make this work. The first is to make sure you're loading your MP3 file as a "Song" object.

    So somewhere in your class declare an object like this.

                Song mySong;

    Then in your LoadContent method, load that song like this.

                mySong = Content.Load<Song>("myFavoriteMP3");


    Finally, you have to play your song through the media player and then set the media player to repeat. You do that like this.

                MediaPlayer.Play(mySong);
                MediaPlayer.IsRepeating = true;


    And that should be all you need to get your song to repeat. Hope that helps!



    I've tried this, but for some reason I just can't get the Song class to work, it all loads properly during compilation, but when the game runs no music is heard.

    SoundEffects work flawlessly, it's only when I try to use mp3 and Song that I get this problem, any ideas how to get it to work?

    /Temaran
  • 2/9/2009 5:29 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    Have you tried other MP3s to see if it's a problem with the file you're currently trying to use? If the problem persists, you could post a sample project for others to look at to see if they spot anything that might be wrong in your code.
  • 2/9/2009 5:45 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    I've tried different bitrates, I've tried using example projects on the web as well, and I always get nothing, I was thinking it might be some default value in the MediaPlayer or something, when it works for you guys it just works automatically?

    It's not something like it "only works on Zune" either I guess?

    I'm using a pretty old laptop(about 4yrs old), so there might be a hardware problem as well, I just don't know...
    I don't have any problems playing mp3s normally though.

    XNA 3.0, MSVS 2008

    what I do is something like this:

    Song coldComfort;

    in content load:

    1:   coldComfort = content.Load<Song>("Audio/ColdComfort");
    2:   MediaPlayer.Play(coldComfort);


    then I run the program, it compiles wonderfully, and... nothing is to be heard, at all :<

    what I'm doing right now, is using SoundEffects for music as well, which seems like a waste if you're actually able to use mp3s :<

    class SoundHandler
        {
            SoundEffect gameTheme1;

            public SoundHandler(ContentManager content)
            {
                gameTheme1 = content.Load<SoundEffect>("Music/ColdComfort");
            }

            /*
             *BGM IDs:
             *0 main screen
             *1 game BGM1
             *2 game BGM2
             *3 game BGM3
             */
            public void PlayBackgroundMusic(int id)
            {
                switch(id)
                {
                    case 0:
                        break;
                    case 1:
                        gameTheme1.Play(0.5f, 0, 0, true);
                        break;
                    default:
                        break;
                }
            }
        }


    only that one loaded right now though, so it looks a bit wierd, but you get the point. :)
    And this works flawlessly.
  • 4/13/2009 5:00 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    Maybe you could try to install the .net 1.1, but I should say, it is difficult to be uninstalled.

    I got the same problem when I removed the .net 1.1, I am not sure if it is all because of the .net 1.1, as I don't want to install it again, it is stupid to install kinds and kinds of .net frameworks, and falling into troubles when you want to remove them.

    And also, I can say the song processor is as stupid as windows live. You can never imagine your 2.0M WMA file will become 8.0M when it's loaded into your project, but song processor does.

    I will recommend you to use the dx audiovideoplayback to play musics(midi, mp3...but not wma), and also, videos.

    Or DirectSound.
  • 4/14/2009 10:17 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    Hi all!

    I also have a little problem with this "simple sound api".

    I did everything right (I think):
    Declared and used objects just as George wrote it. My mp3s are is the right Folder, I wrote the right path into the Content.Load method, content importer is set to mp3, content processor is set to song, asset name, filename ... everything is in its place. I still get the "File Not Found" Exception.

    Any ideas?

    thx,
    Tenshiko
  • 4/15/2009 2:34 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    I'm having the same problem as Tamaran
  • 4/15/2009 2:41 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    Can you supply your code?  It sounds like you have everything right - remember the example shown above references an Audio subfolder under the Content folder - did you create an Audio folder as well or adjust the path as stated?
  • 4/16/2009 7:52 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    Tenshiko:
    Hi all!

    I also have a little problem with this "simple sound api".

    I did everything right (I think):
    Declared and used objects just as George wrote it. My mp3s are is the right Folder, I wrote the right path into the Content.Load method, content importer is set to mp3, content processor is set to song, asset name, filename ... everything is in its place. I still get the "File Not Found" Exception.

    Any ideas?

    thx,
    Tenshiko

    Check if you have these codes in your project:

    Content.RootDirectory = "Content";
  • 4/18/2009 7:00 AM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?

    Are you guys trying to play something in the pipeline or off your HD?


  • 5/19/2009 2:02 PM In reply to

    Re: ? How to use XNA 3 "Simple Sound API" for mp3 Looping background music ?


    Maybe I can help, I just stumbled across this post looking for an example of how XNA handles background music and implemented George's method, which worked first time.

    I am declaring the song as a public member of the Game class, and a boolean called MusicIsPlaying
    In the Initialise method I set MusicIsPlaying to false
    In the Content Load method I load the song using Content.Load<Song>("songname"); (without the .mp3!)
    In the Update method, if MusicIsPlaying is false I invoke MediaPlayer.Play(song), I set MediaPlayer.IsRepeating to true and set MusicIsPlaying
    to true.
    In the Draw method I set the background black because cornflower blue reminds me too much of Managed DirectX.

    And it just works.

    It took longer to write this post!

    The one key thing I did differently was to call MediaPlayer.Play() in the Update rather than the Content Load.

    You might not even need to set a flag, I just assumed that each call to MediaPlayer.Play() begins the mp3 from the beginning - there's probably a property on MediaPlayer that tells you if it's currently playing too (I didn't check, I'm at work and didn't want to have an XNA project open for too long!)

    Hope some of that helped someone.



    Temaran:
    I've tried different bitrates, I've tried using example projects on the web as well, and I always get nothing, I was thinking it might be some default value in the MediaPlayer or something, when it works for you guys it just works automatically?

    It's not something like it "only works on Zune" either I guess?

    I'm using a pretty old laptop(about 4yrs old), so there might be a hardware problem as well, I just don't know...
    I don't have any problems playing mp3s normally though.

    XNA 3.0, MSVS 2008

    what I do is something like this:

    Song coldComfort;

    in content load:

    1:   coldComfort = content.Load<Song>("Audio/ColdComfort");
    2: MediaPlayer.Play(coldComfort);


    then I run the program, it compiles wonderfully, and... nothing is to be heard, at all :<

    what I'm doing right now, is using SoundEffects for music as well, which seems like a waste if you're actually able to use mp3s :<

    class SoundHandler
        {
            SoundEffect gameTheme1;

            public SoundHandler(ContentManager content)
            {
                gameTheme1 = content.Load<SoundEffect>("Music/ColdComfort");
            }

            /*
             *BGM IDs:
             *0 main screen
             *1 game BGM1
             *2 game BGM2
             *3 game BGM3
             */
            public void PlayBackgroundMusic(int id)
            {
                switch(id)
                {
                    case 0:
                        break;
                    case 1:
                        gameTheme1.Play(0.5f, 0, 0, true);
                        break;
                    default:
                        break;
                }
            }
        }


    only that one loaded right now though, so it looks a bit wierd, but you get the point. :)
    And this works flawlessly.
Page 1 of 1 (17 items) Previous Next