Java Applet - Not Playing Sound? <Solved>

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 6344 Views - Last Post: 19 December 2011 - 09:46 PM Rate Topic: -----

#1 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Java Applet - Not Playing Sound? <Solved>

Posted 13 December 2011 - 05:42 PM

Hi, I have a simple game that I am trying to utilize sound in, and I think I have done it all correctly, but alas, sound does not play. I've looked at tutorials on Google and YouTube, but none have worked or fixed my problem. Any help?

NOTE: Please don't talk about the useless keylisteners or other things, I have plans for them (even though I'm not too sure what they are).

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


 
public class Exercitus extends Applet implements Runnable {
	private static final long serialVersionUID = 1L;
	
	Thread t1;
	
	private int num1 = (int) (Math.random()*163);
	private int num2 = (int) (Math.random()*163);
	
	int objposx = num1;
	int objposy = num2;
	int playerX = 100;
	int playerY = 100;
	int score = 0;
	
	public AudioClip point;
	
	public void init() {
		addKeyListener(hello);
		addMouseMotionListener(new AL());
		setFocusable(true);
		point = getAudioClip(getCodeBase(), "Sounds/point.wav");
	}
	
	public void paint(Graphics g) {
		//BORDER RECTS
		Rectangle r1 = new Rectangle(0, 0, 10, 200);
        Rectangle r2 = new Rectangle(0, 0, 200, 10);
        Rectangle r3 = new Rectangle(190, 0, 10, 200);
        Rectangle r4 = new Rectangle(0, 190, 200, 10);
        Rectangle ob1 = new Rectangle(45, 60, 45, 20);
        Rectangle ob2 = new Rectangle(70, 110, 28, 45);
        Rectangle ob3 = new Rectangle(140, 110, 15, 15);
        
		//BORDER
		g.fillRect(r1.x, r1.y, 10, 200);
		g.fillRect(r2.x, r2.y, 200, 10);
		g.fillRect(r3.x, r3.y, 200, 200);
		g.fillRect(r4.x, r4.y, 120, 10);
		
		//SCORE
		g.drawString("Score: " + score, 130, 200);
		
		//OBSTACLES
		g.fillRect(45, 60, 45, 20);
		g.fillRect(70, 110, 28, 45);
		g.fillRect(140, 110, 15, 15);
		
		//PLAYER RECT
		Rectangle r5 = new Rectangle(playerX, playerY, 25, 25);
		//PLAYER
		g.fillRect(r5.x, r5.y, 25, 25);
		
		//OBJECTIVE RECT
		Rectangle r6 = new Rectangle(objposx, objposy, 10, 10);
		//OBJECTIVE
		g.fillOval(r6.x, r6.y, 10, 10);
		
		//STATEMENTS
		if (playerX >= 165){
			playerX = 165;
			repaint();
		}
		if (playerY >= 165){
			playerY = 165;
			repaint();
		}
		if (playerX <= 10){
			playerX = 10;
			repaint();
		}
		if (playerY <= 10){
			playerY = 10;
			repaint();
		}
		if (objposx >= 163) {
			objposx = (int) (Math.random()*163);
			objposy = (int) (Math.random()*163);
            repaint();
		}
		if (objposx <= 12) {
			objposx = (int) (Math.random()*163);
			objposy = (int) (Math.random()*163);
            repaint();
		}
		if (objposy >= 163) {
			objposx = (int) (Math.random()*163);
			objposy = (int) (Math.random()*163);
            repaint();
		}
		if (objposy <= 12) {
			objposx = (int) (Math.random()*163);
			objposy = (int) (Math.random()*163);
            repaint();
		}
		//OBJECTIVE INTERSECTIONS
		if (r6.intersects(r5)) {
			point.play();
			score++;
			objposx = (int) (Math.random()*163);
			objposy = (int) (Math.random()*163);
            repaint();
		}
		if (r6.intersects(ob3) || r6.intersects(ob1) || r6.intersects(ob2)) {
			objposx = (int) (Math.random()*163);
			objposy = (int) (Math.random()*163);
            repaint();
		}
		//PLAYER INTERSECTIONS
		if (r5.intersects(ob1) || r5.intersects(ob2) || r5.intersects(ob3)) {
			g.drawString("You lost with a score of: " + score, 30, 30);
			repaint();
			try {
				Thread.sleep(64000000000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	//MOVEMENT
	public KeyListener hello = new KeyListener() {
		@SuppressWarnings("unused")
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			repaint();
		}
		@SuppressWarnings("unused")
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
		}
		@SuppressWarnings("unused")
		public void keyTyped(KeyEvent e) {
			int key = e.getKeyCode();
			repaint();
		}
	};
	
	public void start() {
	}
	public void stop() {
		
	}
	public void destroy() {
		
	}
	public class AL extends MouseAdapter {
		@Override
        public void mouseMoved(MouseEvent e){
            playerX = e.getX()-12;
            playerY = e.getY()-12;
            repaint();
        }
    }
	Runnable r = new Runnable() {
	public void run() {
		try {
		      while (true) {
		        Thread.sleep(50L);
		      }
		    } catch (InterruptedException iex) {}
	}
	};

	@Override
	public void run() {}
	}

This post has been edited by pbl: 19 December 2011 - 09:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Applet - Not Playing Sound? <Solved>

#2 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Not Playing Sound? <Solved>

Posted 13 December 2011 - 09:25 PM

point = getAudioClip(getCodeBase(), "Sounds/point.wav");
Are you sure it is read ?
If you are using an AppletViewer add a System.out.println(point); to see if it is null

and please create you Rectangle only once in your init() method and store them in your instance variables... no need to create 7 new Rectangle objects every time paint() is called
Was This Post Helpful? 0
  • +
  • -

#3 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 14 December 2011 - 08:15 PM

Quote

point = getAudioClip(getCodeBase(), "Sounds/point.wav");
Are you sure it is read ?
If you are using an AppletViewer add a System.out.println(point); to see if it is null


Sorry, I'm only about 6 months into Java and haven't taken any classes (I'm teaching myself), and what do you mean by "using an AppletViewer"?

Sorry...
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Not Playing Sound? <Solved>

Posted 14 December 2011 - 08:27 PM

How do you run your program ?
Was This Post Helpful? 0
  • +
  • -

#5 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 14 December 2011 - 10:24 PM

View Postpbl, on 14 December 2011 - 08:27 PM, said:

How do you run your program ?

I use Eclipse... Are you talking about compilers needed if using NetBeans?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Not Playing Sound? <Solved>

Posted 14 December 2011 - 11:33 PM

So in Eclipse, do

point = getAudioClip(getCodeBase(), "Sounds/point.wav");
System.out.println("My clip is: " + point);

and see it it prints

My clip is: null

in the console window at the bottom of your screen
Was This Post Helpful? 0
  • +
  • -

#7 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 15 December 2011 - 07:23 PM

Unfortunately, every single place I put the code, even in public static void main(String[] args) {} gave me an error, so I can't fin out. :sad3:

This is the error I get: "Cannot make a static reference to the non-static method getAudioClip(URL, String) from the type Applet"
Was This Post Helpful? 0
  • +
  • -

#8 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 15 December 2011 - 07:46 PM

hehe... I tried putting it in the "init" method and it worked, the output was: My clip is: sun.applet.AppletAudioClip@89fbe3
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Not Playing Sound? <Solved>

Posted 15 December 2011 - 10:47 PM

So it means it has been read correctly but...
what are you doing with a main() methid in an Applet ?
Was This Post Helpful? 0
  • +
  • -

#10 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 15 December 2011 - 11:11 PM

I was just playing around with the idea that data could be temporarily stored and used later in the game through the console, but back to the problem... still not playing sound?

By the way, my code has changed a bit, and I am leaving the sound alone, I am currently working on other functions, but nevermind that. Tomorrow I will give you an update on the code to see if anything is affecting the sound or funcionality of my program.
Was This Post Helpful? 0
  • +
  • -

#11 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 15 December 2011 - 11:21 PM

*Sigh* I'll just give you the code now, with half-done stuff in it...

public class Exercitus extends Applet implements Runnable {
	private static final long serialVersionUID = 1L;
	
	public static void main(String[] args) {
		System.out.println("My clip is: " + points);
	}
	

	Thread t1;
	Thread t2;
	
	private int num1 = (int) (Math.random()*265);
	private int num2 = (int) (Math.random()*265);
	
	int objposx = num1;
	int objposy = num2;
	int playerX = 100;
	int playerY = 100;
	int score = 0;
	int movingobx = 200;
	int movingoby = 110;
	int playersizex = 25;
	int playersizey = 25;
	boolean MOUSE_CLICKED = false;
	
	public static AudioClip points;
	
	public void init() {
		addKeyListener(hello);
		addMouseMotionListener(new AL());
		addMouseListener(wonderful);
		setFocusable(true);
		points = getAudioClip(getCodeBase(), "point.wav");
		System.out.println("My clip is: " + points);
	}
	
	@SuppressWarnings("static-access")
	public void paint(Graphics g) {
		//BORDER RECTS
		Rectangle r1 = new Rectangle(0, 0, 10, 200);
        Rectangle r2 = new Rectangle(0, 0, 275, 10);
        Rectangle r3 = new Rectangle(270, 0, 10, 200);
        Rectangle r4 = new Rectangle(0, 190, 200, 10);
        Rectangle ob1 = new Rectangle(45, 60, 45, 20);
        Rectangle ob2 = new Rectangle(70, 110, 28, 45);
        Rectangle ob3 = new Rectangle(140, 110, 15, 15);
        Rectangle ob4 = new Rectangle(movingobx, movingoby, 20, 16);
        
		//BORDER
		g.fillRect(r1.x, r1.y, 10, 200);//LEFT
		g.fillRect(r2.x, r2.y, 200, 10);//TOP
		g.fillRect(r3.x, r3.y, 10, 200);//RIGHT
		g.fillRect(r4.x, r4.y, 180, 10);//BOTTOM
		
		//SCORE AND TIME
		g.drawString("Score: " + score, 215, 200);
		
		//OBSTACLES
		g.fillRect(45, 60, 45, 20);
		g.fillRect(70, 110, 28, 45);
		g.fillRect(140, 110, 15, 15);
		g.fillRect(ob4.x, ob4.y, 40, 26);
		
		//PLAYER RECT
		Rectangle r5 = new Rectangle(playerX, playerY, playersizey, playersizey);
		//PLAYER
		g.fillRect(r5.x, r5.y, playersizex, playersizey);
		
		//OBJECTIVE RECT
		Rectangle r6 = new Rectangle(objposx, objposy, 10, 10);
		//OBJECTIVE
		g.fillOval(r6.x, r6.y, 10, 10);
		
		//STATEMENTS
		if (playerX >= 245){
			playerX = 245;
			repaint();
		}
		if (playerY >= 165){
			playerY = 165;
			repaint();
		}
		if (playerX <= 10){
			playerX = 10;
			repaint();
		}
		if (playerY <= 10){
			playerY = 10;
			repaint();
		}
		if (objposx >= 265) {
			objposx = (int) (Math.random()*265);
			objposy = (int) (Math.random()*265);
            repaint();
		}
		if (objposx <= 12) {
			objposx = (int) (Math.random()*265);
			objposy = (int) (Math.random()*265);
            repaint();
		}
		if (objposy >= 163) {
			objposx = (int) (Math.random()*265);
			objposy = (int) (Math.random()*265);
            repaint();
		}
		if (objposy <= 12) {
			objposx = (int) (Math.random()*265);
			objposy = (int) (Math.random()*265);
            repaint();
		}
		
		//OBJECTIVE INTERSECTIONS
		if (r6.intersects(r5)) {
			score++;
			objposx = (int) (Math.random()*265);
			objposy = (int) (Math.random()*265);
            repaint();
		}
		if (r6.intersects(ob3) || r6.intersects(ob1) || r6.intersects(ob2) || r6.intersects(ob4)) {
			objposx = (int) (Math.random()*265);
			objposy = (int) (Math.random()*265);
            repaint();
		}
		//PLAYER INTERSECTIONS
		if (r5.intersects(ob1) || r5.intersects(ob2) || r5.intersects(ob3) || r5.intersects(ob4)) {
			g.drawString("You lost with a score of: " + score, 30, 30);
			g.drawString("The game will restart in 5 seconds...", 20, 43);
			repaint();
			try {
				t1.sleep(5000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			objposx = (int) (Math.random()*265);
			objposy = (int) (Math.random()*265);
			score = 0;
		}
	}
	
	//SPECIAL
	public MouseListener wonderful = new MouseListener() {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			playersizex = 15;
			playersizey = 15;
			repaint();
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {}

		@Override
		public void mouseExited(MouseEvent arg0) {}
		
		@Override
		public void mousePressed(MouseEvent e) {}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			playersizex = 25;
			playersizey = 25;
			repaint();
		
		}
	};
	//MOVEMENT
	public KeyListener hello = new KeyListener() {
		@SuppressWarnings("unused")
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			repaint();
		}
		@SuppressWarnings("unused")
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
		}
		@SuppressWarnings("unused")
		public void keyTyped(KeyEvent e) {
			int key = e.getKeyCode();
			repaint();
		}
	};
	
	public void start() {
	}
	public void stop() {
		
	}
	public void destroy() {
		
	}
	public class AL extends MouseAdapter {
		@Override
        public void mouseMoved(MouseEvent e){
            playerX = e.getX()-12;
            playerY = e.getY()-12;
            repaint();
        }
    }
	Runnable r = new Runnable() {
	@SuppressWarnings("static-access")
	public void run() {
		try {
		      while (true) {
		    	  repaint();
		    	  t1.sleep(1L);
		      }
		    } catch (InterruptedException iex) {}
	}
	};
	public void run() {}
	}

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Not Playing Sound? <Solved>

Posted 16 December 2011 - 07:44 PM


        if (r6.intersects(r5)) {
                        // <--- insert a println() here to make sure you pass there
            point.play();


Was This Post Helpful? 0
  • +
  • -

#13 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 17 December 2011 - 11:19 AM

Yes, I definitely pass there as Everything works, when the square hits the ball, I get a point, and the ball assumes a new position, and "Look at me!" appears in the console (thats what I told it to print).
Was This Post Helpful? 0
  • +
  • -

#14 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java Applet - Not Playing Sound? <Solved>

Posted 17 December 2011 - 03:35 PM

If you want, here is my video of the game in action: http://www.youtube.c...h?v=Z2fK3q2PLkM
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java Applet - Not Playing Sound? <Solved>

Posted 17 December 2011 - 05:14 PM

View Posthunterb100, on 17 December 2011 - 01:19 PM, said:

Yes, I definitely pass there as Everything works,

Does not seem that everything works or you wouldn't be posting here
You added a println() and you see the output of the println() ?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2