Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why can't I play sounds?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Why can't I play sounds?

    I am making a simple game and I want to play sounds this is what I have, but it is not working! Any help is appreciated.
    package Game;
     
    import javax.swing.JFrame;
     
    public class Main{
     
    	private static final long serialVersionUID = 1L;
     
    	private final int HEIGHT = 240 ;
    	private final int WIDTH = (HEIGHT / 8 ) * 6;
    	private final int SCALE = 3;
     
    	private boolean load = false;
     
    	public static void main (String [] args){
    		new Main();
    	}
     
    		public Main(){
     
     
    				try {
    					java.applet.AudioClip clip =
    					java.applet.Applet.newAudioClip(
    					new java.net.URL("file://c:/res/hl2.wav")); //Won't work
    					clip.play();
    					} catch (java.net.MalformedURLException murle) {
    					System.out.println(murle);
    					}
     
     
    			JFrame f = new JFrame("Half Life 3");
     
    			//f.add(new Board());
     
    			f.setSize((HEIGHT - 3) * SCALE , WIDTH * SCALE);
    			f.setLocationRelativeTo(null);
    			f.setVisible(true);
    			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			f.setResizable(false);
    		}
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why can't I play sounds?

    Try rewriting the code to be an applet and see if when executed as an applet it plays a sound.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Why can't I play sounds?

    Quote Originally Posted by Norm View Post
    Try rewriting the code to be an applet and see if when executed as an applet it plays a sound.
    how do I do that? I am an absolute beginner to GUI. Can you please provide me with an easy tutorial!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Why can't I play sounds?

    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Why can't I play sounds?

    Quote Originally Posted by sakonpure6 View Post
    I am making a simple game and I want to play sounds this is what I have, but it is not working!
    I have not tested your code and at this moment I have no time for this. But I can tell you a thing that is worth to know: the .wav file format is a quite complex format with many possible bitrates and codecs/compressions. It's very possible that your sound file format is one of those not supported by the Java framework.

    See WAV - Wikipedia, the free encyclopedia, you'll find a list of some formats with samples. Try these samples to verify if it's your code that is wrong or your sound file.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. [SOLVED] Creating sounds from numerical inputs (How do I do this?)
    By karmichaun in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 07:49 PM
  2. My app audio sounds different when running the app?
    By TP-Oreilly in forum Android Development
    Replies: 1
    Last Post: January 29th, 2012, 08:23 AM
  3. Creating a class to play sounds.
    By Archibold9 in forum Java Theory & Questions
    Replies: 8
    Last Post: December 21st, 2011, 06:14 PM
  4. Sounds to play during action performed
    By smithyboy in forum Object Oriented Programming
    Replies: 2
    Last Post: November 20th, 2010, 04:16 PM
  5. image and sounds mp3 database
    By Stefan_Lam in forum JDBC & Databases
    Replies: 0
    Last Post: May 7th, 2010, 05:30 AM