Java Tutorial – Add music/sound to java program: background music help

This tutorial will show you how to add background music or a sound file to your Java program. The “music” method I made in this program can be used to play any sound file from the system into a Java program.


Comments

22 Responses to “Java Tutorial – Add music/sound to java program: background music help”

  1. ryanharrison10 on October 26th, 2009 10:41 am

    awesome!

  2. BoringGamer on October 26th, 2009 10:22 pm

    thanks dude!! =]

  3. BoringGamer on October 27th, 2009 11:37 am

    I dont know whats going on but i got this warning when i tried compling the code.
    Sound.java:32: warning: sun.audio.AudioStream is Sun proprietary API and may be removed in a future release
    BMG = new AudioStream(new FileInputStream(“safari2.wav”));

  4. xxxcoolboyxxx on October 27th, 2009 12:31 pm

    I dont think a warning should matter to weather or not the program will work

  5. BoringGamer on October 27th, 2009 1:42 pm

    Thanks for letting me know, I found the problem. Instead of Data I put date and BGM to BMG..^^’

  6. donnchadhofoghlu on November 10th, 2009 6:03 pm

    thanks

  7. altisw5 on November 21st, 2009 8:13 am

    Thanks for EXCELENT tutorials.
    A quick question(without trying).
    If U delete the file(safari2), how come the program still run? Must be a catch error(file not found).

  8. joint08 on November 21st, 2009 11:53 am

    Great tutorial man, any one that uses netbeans?

    i put the mp3 file in the same folder as the main file but the mp3 file still cant be found

  9. xxxcoolboyxxx on November 21st, 2009 2:06 pm

    You can put the full path of the file instead of just putting the file name. That way the program will know exactly where to look. For example you can put “C:/Users/My Documents/Safari2.mp3″ instead of putting the file name

  10. joint08 on November 22nd, 2009 2:04 am

    Thanks man, ill try that :)

  11. Drace999 on November 23rd, 2009 2:02 am

    Very helpful thanks!

  12. joint08 on November 24th, 2009 9:57 am

    Any one that knows how to stop the loop?
    i have tried
    AudioPlayer.player.stop (continuousaudiostream);
    but it wont stop =/

  13. janneklouman on December 13th, 2009 5:12 pm

    Access restriction: The type AudioData is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar

    Why am I getting this error for everything involving AudioPlayer?

  14. schintus on December 14th, 2009 6:52 am

    In eclipse do this: Window -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rules) -> set it to ‘Warnings’

  15. 69elchupacabra69 on January 1st, 2010 10:21 am

    @joint08 You can put it outside the src folder.

  16. gleboff on January 12th, 2010 5:57 pm

    Not only will music not play on my first button, but for my second button I get this for the AudioData = MD;

    “The local variable MD is never read”

    Any help? thanks.

  17. gleboff on January 12th, 2010 9:46 pm

    scratch that, now I only have a problem with buttons 4-10 (i made a program with 10 buttons rather than 1)
    and I get errors on the button(#).addActionListner (action(#) ());
    saying that the action cannot be resolved as a type

    any help?

  18. MrHeyAlan on January 25th, 2010 9:40 pm

    Thank you! This tutorial was very helpful. Except, how do you make it so that it only plays the clip once and not as a loop.

  19. RodonJarino on March 8th, 2010 1:01 pm

    i cannot use this in java 1.6 it says restricted access on audio player stream and data and that it will be removed in a future realease

  20. BlackMace28 on March 17th, 2010 4:38 pm

    does java only support wav files?

  21. kidd093 on March 23rd, 2010 10:29 am

    what’s a good song for a puzzle game i’m making?

  22. TheDubTurbo on May 4th, 2010 3:27 pm

    Ive been making some awesome music with a program called DubTurbo. It has only just been released but i downloaded there trial. buydubturbo . com Definetly the cheapest music production software you can buy. BuyDubTurbo . com

Powered by Yahoo! Answers