14 Replies - 9591 Views - Last Post: 30 December 2008 - 12:31 AM Rate Topic: -----

#1 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Need Help to add sound to my game

Posted 29 December 2008 - 12:39 PM

I want to add a wave file to my game where if the player guesses a wrong letter a sound will play. I have created an array of buttons for my built in key board and the game is fully functional. but im not too sure as how to go about to add a wave file to my game, where and how is the question. any assistance would be greatly appreciated. here is my complete code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

public class JavaHangMan extends JFrame{

	//declaring variables before starting.......
	private JButton myButton[];
	private JLabel myLabel[];
 	private String myAlpha[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
 	private String myWords[] = {"HAPPY", "PROGRAMMING", "JAVA", "GRADUATE", "EQUINOX", "IRONY", "KINETIC", "HANGMAN", "CARTOON", "CHARACTER"};
 	private JLabel thisLabel[];
 	private JMenuItem help;
 	private JMenuItem quit;
 	private JMenuItem about;
 	private JLabel lbl1;
 	private JLabel lbl2;
 	private String GuessWord;
 	private JMenuItem start;
 	private String images[] = {"Gallow.gif", "1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif"};
 	private ImageIcon images1[];
 	private int numOfGuesses=0;
	private String Fname;
	private JLabel lbl3;
	private int score=0;
	private int Maxguesses = 6;
	private int numRight=0;
	private JLabel lbl4;
	
	public static void main(String[] args) {
		
   	//Creating Frame....
		JavaHangMan frame = new JavaHangMan();
 	   	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 	   	frame.setSize(800, 700);
		frame.setVisible(true);
		}
	
	//constructor for the game.......
	 public JavaHangMan(){
		
		super ("HangMan");
		Container pane = getContentPane();
		pane.setLayout(null);
	
	//creating a message box to enter the players name and display it to the screen.......
		lbl2 = new JLabel();
		Fname = JOptionPane.showInputDialog(null, "Welcome To HangMan, Please Enter Your Name:");
		lbl2.setText(Fname);
		lbl2.setBounds(500, 20, 450, 250);
		pane.add(lbl2);
		
		
		
	// assigning the image array......
		images1 = new ImageIcon[7];
		for(int i = 0; i <7; i++)
		{
		images1[i] = new ImageIcon(images[i]); 
		}
		
	//score display
		
		lbl3 = new JLabel("Player's Score: " + score);
		lbl3.setBounds(500, 60, 450, 250);
		pane.add(lbl3);
	
	//Display Start up msg
		lbl4 = new JLabel("To Start Game Go To File And Select Start");
		lbl4.setBounds(500, 60, 450, 300);
		pane.add(lbl4);
		
	// setting the image to the game of the gallow.........added to the start file with setVisible down the page....
		lbl1 = new JLabel();	
				ImageIcon icon = new ImageIcon("Gallow.gif");
				lbl1.setBounds(100, 20, 450, 450);
				lbl1.setIcon(icon);
				pane.add(lbl1);
				
				lbl1.setVisible(false);
				//pane.setBackground(Color.green);
		
		
	//Creating a menu bar and top level menus.....
			handler myHandler = new handler();
		
			JMenuBar menuBar = new JMenuBar();
			JMenu fileMenu = new JMenu("File");
		
			fileMenu.setMnemonic('F');
		
			JMenu optionsMenu = new JMenu("Options");
			optionsMenu.setMnemonic('O');
			menuBar.add(fileMenu);
			menuBar.add(optionsMenu);
			this.setJMenuBar(menuBar);
			
			start = new JMenuItem("Start", 'S');
			fileMenu.add(start);
			start.addActionListener(myHandler);
		
			quit = new JMenuItem("Quit", 'Q');
			fileMenu.add(quit);
			quit.addActionListener(new handler());
			
			about = new JMenuItem("About", 'A');
			optionsMenu.add(about);
			about.addActionListener(new handler());
			
			help = new JMenuItem("Help", 'H');
			optionsMenu.add(help);
			help.addActionListener(new handler());
			
			
			
			
   	//create the keyboard for choosing letters...an array of letters and buttons was created and positioned.....
			myButton = new JButton[26];
			for (int i=0; i<26; i++)
			{
			myButton[i]=new JButton(myAlpha[i]);
			if(i<10)
			{
			myButton[i].setBounds(100 +(i*55), 550, 50, 20);
			}
			else if(i>9 && i <20)
			{
			myButton[i].setBounds(100 +((i-10)*55), 575, 50, 20);
			}
			else
			{
			myButton[i].setBounds(200 +((i-20)*55), 600, 50, 20);
			}
			pane.add(myButton[i]);
			myButton[i].addActionListener(myHandler);
			myButton[i].setEnabled(false);	
			}
			
	//Creating a label to show the words in the array and the underscores beneath them......
			myLabel = new JLabel[20];
			thisLabel = new JLabel[20];
			for( int i=0; i<20; i++){
				myLabel[i] = new JLabel(" ");
				thisLabel[i] = new JLabel("_");
				myLabel[i].setBounds(140 +(i*25), 475, 20, 20);
				thisLabel[i].setBounds(140 + (i*25), 500, 20, 20);
				pane.add(myLabel[i]);
				pane.add(thisLabel[i]);
				myLabel[i].setVisible(false);
				thisLabel[i].setVisible(false);
				
				}
			
			
	}
	
	
	
	// creating a method for start game........
		public void StartGame(){
		
	//Creating a method to call a random word from the Word array......
			
				for(int i=0; i < myButton.length; i++)
				{
				myButton[i].setEnabled(true);
				
				}
				
				numOfGuesses = 0; 
				numRight = 0;
				
				Initalize();
				Random randomWords = new Random();
				int face;
				face = 1 + randomWords.nextInt(9);
				GuessWord = myWords[face];
				lbl1.setVisible(true);
				lbl2.setVisible(true);
				for (int i = 0; i < GuessWord.length(); i++ ){
				thisLabel[i].setVisible(true);
				myLabel[i].setText("" + GuessWord.charAt(i));
				lbl4.setVisible(false);
				}
			}
			
			
	//creates a method called Validate that sets the letter visible if guessed properly
		public void Validate(String s)
		{
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				numRight++;//numRight incriments by 1
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;
					
				}
				
			}
			if (flag == false)
			{
				numOfGuesses++;//numOfGuesses incriments by 1
				lbl1.setIcon(images1[numOfGuesses]);
				
				if (numOfGuesses == Maxguesses)//if NumOfGuess increments to 6 then show a message you lose....
				{
				for(int i=0; i < myButton.length; i++)
				{
				myButton[i].setEnabled(false);
				
				}
				
				JOptionPane.showMessageDialog(null, "You Lose, Sorry Try Again!\nPlease Go To The File Menu And Click Start\nTo Receive Another Word!");
				
				}
			}
			else 
				
			{
			
			if (numRight == GuessWord.length())//if numRight increments to the number of char in the word then show message you win...
				{
				for(int i=0; i < myButton.length; i++)
				{
				myButton[i].setEnabled(false);
				}
				JOptionPane.showMessageDialog(null, "Congratulations, You Win!\nPlease Go To The File Menu And Click Start\nTo Receive Another Word!");
				
				}
			}
			
		}
	
	
	//creates a method to hide everything till game has started
		public void Initalize(){
			for( int i = 0; i < 20; i++)
			{
				thisLabel[i].setVisible(false);
				myLabel[i].setVisible(false);
			}
				lbl1.setVisible(false);
				  
				lbl1.setIcon(images1[0]);	  
		}

		
	//Creating an ActionListener
		class handler implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			
	//created an ActionListener to read what buttons are pressed for the keyboard on screen.....
			
			for (int i=0; i<26; i++){
				if (e.getSource() == myButton[i])
				{
				Validate(myAlpha[i]);
				myButton[i].setEnabled(false);
				}
			}
			
	//start game ActionListener calling the StartGame Method....
			
			if (e.getSource()== start)
				{
				StartGame();
				}	
				
	//created an ActionListener to exit out of the application....
			if(e.getSource() == quit)
			{
			
			JOptionPane.showMessageDialog(null, "GoodBye, \nThank You For Playing HangMan!");
			System.exit(0);
			}
	
	//created an ActionListener to display a message on how to play the game......
			else if (e.getSource() == help)
			{
			JOptionPane.showMessageDialog(null, "How To Play HangMan:\nFirst go to File and click Start your word will appear in\na _ _ _ format and guess the word before your man gets Hung!\nGuess the word by choosing letters on the screen with your mouse as you choose\nif you get letters wrong your man will get a head, then a body added and so forth,\nonce he is a full body you have lost the game, you have 6 tries. \nGood Luck!");
			}
			else if (e.getSource() == about)
			{
			JOptionPane.showMessageDialog(null, "Created By George Michael For Java Programming Course");
			}
			
		}	
	}	
		
}


Is This A Good Question/Topic? 0
  • +

Replies To: Need Help to add sound to my game

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 01:30 PM

In order to play a .wav sound in java you can use this simple code:
play(getCodeBase(), "YourSoundFile.wav");


add this code to where the player guesses a wrong letter and i am sure it will work ;)
hope this helps, good luck in you game ;)
Was This Post Helpful? 0
  • +
  • -

#3 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 01:41 PM

View Postayman_mastermind, on 29 Dec, 2008 - 12:30 PM, said:

In order to play a .wav sound in java you can use this simple code:
play(getCodeBase(), "YourSoundFile.wav");


add this code to where the player guesses a wrong letter and i am sure it will work ;)
hope this helps, good luck in you game ;)





Should i declare the audio file as a global variable first? tand thank you very much


View PostRoemeo, on 29 Dec, 2008 - 12:36 PM, said:

View Postayman_mastermind, on 29 Dec, 2008 - 12:30 PM, said:

In order to play a .wav sound in java you can use this simple code:
play(getCodeBase(), "YourSoundFile.wav");


add this code to where the player guesses a wrong letter and i am sure it will work ;)
hope this helps, good luck in you game ;)





Should i declare the audio file as a global variable first,? cuz put in the code and the name of the file and it says cant find symbol for getCodeBase and thank you very much

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 04:09 PM

The trick given will work with an Applet but not in an application like you have
You will have to use a workaround

To be put in your instance variables
java.applet.AudioClip clip = java.applet.Applet.newAudioClip(new java.net.URL("file.wav"));



to be put in your actionPerformed() method
		clip.play( );


This post has been edited by pbl: 29 December 2008 - 04:09 PM

Was This Post Helpful? 1
  • +
  • -

#5 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 04:40 PM

View Postpbl, on 29 Dec, 2008 - 03:09 PM, said:

The trick given will work with an Applet but not in an application like you have
You will have to use a workaround

To be put in your instance variables
java.applet.AudioClip clip = java.applet.Applet.newAudioClip(new java.net.URL("file.wav"));



to be put in your actionPerformed() method
		clip.play( );



ok ive tried the code you had given me but i havent been able to get it to work i keep getting an error
unreported exception java.net.MalformedURLException; must be caught or declared to be thrown.

did i maybe declare them wrong did i put them in the wrong places? im still a beginner especially at audio and video for java.

ive decided to put the code here cuz i wanna have the right and weong anser to lay a sound my validate method is called through my action performed.

public void Validate(String s)
		{
			boolean flag = false;
			
			
		for (int i = 0; i < GuessWord.length(); i++)
			{
				
				if(GuessWord.charAt(i)== s.charAt(0))
				{
				numRight++;//numRight incriments by 1
				score++;
				lbl3.setText(" Player's Score: " + score);
				myLabel[i].setVisible(true);
				flag = true;
					
				}
				
			}
			if (flag == false)
			{
				numOfGuesses++;//numOfGuesses incriments by 1
				lbl1.setIcon(images1[numOfGuesses]);
				
				if (numOfGuesses == Maxguesses)//if NumOfGuess increments to 6 then show a message you lose....
				{
				for(int i=0; i < myButton.length; i++)
				{
				myButton[i].setEnabled(false);
				java.applet.AudioClip clip = java.applet.Applet.newAudioClip(new java.net.URL("Argh.wav"));
				clip.play();
				}
				
				JOptionPane.showMessageDialog(null, "You Lose, Sorry Try Again!\nPlease Go To The File Menu And Click Start\nTo Receive Another Word!");
				
				}
			}
			else 
				
			{
			
			if (numRight == GuessWord.length())//if numRight increments to the number of char in the word then show message you win...
				{
				for(int i=0; i < myButton.length; i++)
				{
				myButton[i].setEnabled(false);
				
				}
				JOptionPane.showMessageDialog(null, "Congratulations, You Win!\nPlease Go To The File Menu And Click Start\nTo Receive Another Word!");
				
				}
			}
			
		}


can you please show me how to set up what you showed me
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 06:21 PM

Oups... a lot trickier than I though
Stupid constructor of URL that needs more than a file name

OK alternatives you can:

Solution 1:
String classDirectory = getClass().getResource("Validate.class").toString();
then you will have a valid, almost URL
on Windows will have something like file:/C:/dir/subdir/Validate.class
then you will have to scan to the last / remove the "Validate.class" and add the name of your .wav file

Solution 2:
String classDirectry = System.getProperty("user.dir");
then (in Windows) you'll have soimething like C:\Dir\SubDir and you'll have to change the \ to /
prefix by "file:/" and append "/file.wav"

But I can't beleive there is not an easier way to do that... let me search unless an URL expert shows up

Have you tried: Google java sound application ?

This post has been edited by pbl: 29 December 2008 - 06:23 PM

Was This Post Helpful? 0
  • +
  • -

#7 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 06:23 PM

View Postpbl, on 29 Dec, 2008 - 05:21 PM, said:

Oups... a lot trickier than I though
Stupid constructor of URL that needs more than a file name

OK alternatives you can:

Solution 1:
String classDirectory = getClass().getResource("Validate.class").toString();
then you will have a valid, almost URL
on Windows will have something like file:/C:/dir/subdir/Validate.class
then you will have to scan to the last / remove the "Validate.class" and add the name of your .wav file

Solution 2:
String classDirectry = System.getProperty("user.dir");
then (in Windows) you'll have soimething like C:\Dir\SubDir and you'll have to change the \ to /
prefix by "file:/" and append "/file.wav"

But I can't beleive there is not an easier way to do that... let me search unless an URL expert shows up



ok thank you very much ill try those out see what i can do, but if anyone or if you find an easier way plz let me know

This post has been edited by Roemeo: 29 December 2008 - 07:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 08:16 PM

OK to translate a filename (current directory) into an URL

	File file = new File("soundFile.wav");
	URI uri = file.toURI();
	URL url = null;
	try {
		url = uri.toURL();
		System.out.println("URL: " + url);
	}
	catch(Exception e) {
		System.out.println("Exception: " + e);
	}



but my trick with applet.newAudioClip() does not seem to work... still working on it
Was This Post Helpful? 0
  • +
  • -

#9 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 08:25 PM

View Postpbl, on 29 Dec, 2008 - 07:16 PM, said:

OK to translate a filename (current directory) into an URL

	File file = new File("soundFile.wav");
	URI uri = file.toURI();
	URL url = null;
	try {
		url = uri.toURL();
		System.out.println("URL: " + url);
	}
	catch(Exception e) {
		System.out.println("Exception: " + e);
	}



but my trick with applet.newAudioClip() does not seem to work... still working on it


ok thanks for helping ill be waiting for more news on this problem. if i come up with anything i will post here thanks again
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 08:41 PM

This does not work... and I really don't know why

	File file = new File("newmail.wav");
	URI uri = file.toURI();
	java.applet.AudioClip clip = null;
	try {
		URL url = uri.toURL();
		clip = java.applet.Applet.newAudioClip(url);
	}
	catch(Exception e) {
		System.out.println("Exception: " + e);
	}

	if(clip != null) {
		System.out.println("Playing the clip");
		clip.play();
		try {
			Thread.sleep(5000);
		}
		catch (Exception e) {}
		System.out.println("Done");
	}



Kya, JeroenFM, Gloin, G00se where are you ?

This post has been edited by pbl: 29 December 2008 - 10:19 PM

Was This Post Helpful? 0
  • +
  • -

#11 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 08:43 PM

What is the error that comes up?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 08:47 PM

View PostRoemeo, on 29 Dec, 2008 - 07:43 PM, said:

What is the error that comes up?

no error and the catch catches nothing and the clip is not null as a println shows
Was This Post Helpful? 0
  • +
  • -

#13 Roemeo  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 153
  • Joined: 08-December 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 08:51 PM

well tomorrow i can ask one of my friends who has done almost the same project and i can see what he says. ill post the answer here tomorrow morn that way it can help us all. thanks for everything i really appreciate all the hard work you have put into this for me.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Need Help to add sound to my game

Posted 29 December 2008 - 09:18 PM

IT WORKS....
I had a typo in my filename

Was 100% sure it should work... and I got stopped by a beginner's mistake
I picked up he first .wav file I could find on my PC "newemail.wav" and type "newmail.wav"

The previous posted code should work and here is one copied over the net that display (and play) the audioclip with details
but I prefer my version that hacks the Applet newAudioClip() method. It has the avantage of being able to keep the AudioClip in an instance variable and read it only once

import java.io.*;
import java.net.*;
import javax.sound.sampled.*;


public class TestCsv {

	TestCsv() {
		File file = new File("newemail.wav");
		URI uri = file.toURI();
		java.applet.AudioClip clip = null;
		try {
			URL url = uri.toURL();
			clip = java.applet.Applet.newAudioClip(url);
		}
		catch(Exception e) {
			System.out.println("Exception: " + e);
		}

		if(clip != null) {
			System.out.println("Playing the clip");
			clip.play();
			try {
				Thread.sleep(1000);
			}
			catch (Exception e) {}
			System.out.println("Done");
		}
		System.out.println("Playing the complicated way");
		play("newemail.wav");
		System.out.println();
	}

	void play(String file) {

		try{
//			get an AudioInputStream
			AudioInputStream ais = AudioSystem.getAudioInputStream(new File(file));
//			get the AudioFormat for the AudioInputStream
			AudioFormat audioformat = ais.getFormat();
			System.out.println("Format: " + audioformat.toString());
			System.out.println("Encoding: " + audioformat.getEncoding());
			System.out.println("SampleRate:" +   audioformat.getSampleRate());
			System.out.println("SampleSizeInBits: " + audioformat.getSampleSizeInBits());
			System.out.println("Channels: " + audioformat.getChannels());
			System.out.println("FrameSize: " + audioformat.getFrameSize());
			System.out.println("FrameRate: " + audioformat.getFrameRate());
			System.out.println("BigEndian: " + audioformat.isBigEndian());
//			ULAW format to PCM format conversion
			if ((audioformat.getEncoding() == AudioFormat.Encoding.ULAW) || 
					(audioformat.getEncoding() == AudioFormat.Encoding.ALAW)){ 
				AudioFormat newformat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
						audioformat.getSampleRate(),
						audioformat.getSampleSizeInBits() * 2,
						audioformat.getChannels(),
						audioformat.getFrameSize() * 2,
						audioformat.getFrameRate(),true);
				ais = AudioSystem.getAudioInputStream(newformat,ais);
				audioformat = newformat;
			}

//			checking for a supported output line
			DataLine.Info datalineinfo = new DataLine.Info (SourceDataLine.class,audioformat);
			if (!AudioSystem.isLineSupported(datalineinfo)){
				System.out.println("Line matching " + datalineinfo + " is not supported.");

			}else{
				System.out.println("Line matching " + datalineinfo + " is supported.");
//				opening the sound output line
				SourceDataLine sourcedataline = (SourceDataLine)AudioSystem.getLine(datalineinfo);
				sourcedataline.open(audioformat);
				sourcedataline.start();
//				Copy data from the input stream to the output data line
				int framesizeinbytes = audioformat.getFrameSize();
				int bufferlengthinframes   = sourcedataline.getBufferSize() / 8;
				int bufferlengthinbytes = bufferlengthinframes * framesizeinbytes;
				byte[] sounddata = new byte[bufferlengthinbytes];
				int numberofbytesread = 0;
				while ((numberofbytesread = ais.read(sounddata)) != -1){
					sourcedataline.write(sounddata,0,numberofbytesread);
				}
			}

		}catch(LineUnavailableException lue){
			System.err.println("LineUnavailableException: " +   lue.getMessage());
		}catch(UnsupportedAudioFileException uafe){
			System.err.println("UnsupportedAudioFileException: " +   uafe.getMessage());
		}catch(IOException ioe){
			System.err.println("IOException: " +   ioe.getMessage());
		}
	}

	static public void main(String[] arg)
	{
		new TestCsv();		
	}
}



*Edited to add: I am surprised thow that the newAudioClip didn't returned an error because "file was not found"

This post has been edited by pbl: 29 December 2008 - 10:23 PM

Was This Post Helpful? 1
  • +
  • -

#15 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Need Help to add sound to my game

Posted 30 December 2008 - 12:31 AM

Thanks very much pbl, for your help, i didnt know that the code i provided only works on applets, afterall i havent used it yet except in applets.
You method in playing sound works great pbl,thanks very much, but i found also an alternative method using sun.audio package, here is a sample, enjoy:
import  sun.audio.*;//import the sun.audio package
import  java.io.*;
InputStream in = new FileInputStream(Filename);// open an input stream  to the audio file.
AudioStream as = new AudioStream(in);// create an audioStream object from the input stream
AudioPlayer.player.start(as);// Use the static class member "player" from class AudioPlayer to play clip
AudioPlayer.player.stop(as);// to stop the audio


i hope this helps :)

This post has been edited by ayman_mastermind: 30 December 2008 - 12:35 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1