Results 1 to 3 of 3
  1. #1
    VTechJava is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Applying Sound to Java Game

    I'm currently coding a game in java for a course I'm taking. I have no experience with java aside from that which I have gained while coding this game. At this point, I have the map layout and character animation as well as boundaries and level switching and things like that. Now I would like to add a simple background music file that will run continuously. It doesn't really need to be anything fancy, I have the file and at the moment I don't want to do anything fancy with it. I've looked at some of the tutorials online and I haven't found anything that seems to work with my code. I would much appreciate help in this manner.

  2. #2
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: Applying Sound to Java Game

    Hi there, okay so first import the following:
    Java Code:
    import sun.audio.*;
    import java.io.*;
    now use this:
    Java Code:
    public void music(){
    			AudioPlayer MGP = AudioPlayer.player;
    			AudioStream BGM;
    			AudioData MD;
    			ContinuousAudioDataStream loop = null;
    			
    			try{
    				InputStream test = new FileInputStream("FILELOCATION"); // make sure it is .wav
    				BGM = new AudioStream(test);
    	            AudioPlayer.player.start(BGM);
    			}
    			catch (Exception error){
    				error.printStackTrace();
    			}
    			
    			MGP.start(loop);
    		}

  3. #3
    VTechJava is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Re: Applying Sound to Java Game

    Quote Originally Posted by leeroijenkiins View Post
    Hi there, okay so first import the following:
    Java Code:
    import sun.audio.*;
    import java.io.*;
    now use this:
    Java Code:
    public void music(){
    			AudioPlayer MGP = AudioPlayer.player;
    			AudioStream BGM;
    			AudioData MD;
    			ContinuousAudioDataStream loop = null;
    			
    			try{
    				InputStream test = new FileInputStream("FILELOCATION"); // make sure it is .wav
    				BGM = new AudioStream(test);
    	            AudioPlayer.player.start(BGM);
    			}
    			catch (Exception error){
    				error.printStackTrace();
    			}
    			
    			MGP.start(loop);
    		}
    I tried this and it gave me this error "Access restriction: The type AudioPlayer is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar" I think I don't have access to the AudioPlayer, AudioData, and AudioStream classes, but I don't know how to add access to them. Is there a specific version of Java I need to download? Thanks for your help!

Similar Threads

  1. Applying Aspectj in EJB 2.0 without using Spring
    By hgupta1204 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 04-25-2012, 09:59 AM
  2. Where can I create sound for a computer game?
    By rajkobie in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 10:07 AM
  3. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 01:02 PM
  4. Applying Vista Buttons for Java JButton
    By viswanadh7 in forum AWT / Swing
    Replies: 4
    Last Post: 03-13-2009, 12:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •