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 8 of 8

Thread: Audio In My Program

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Audio In My Program

    Hi, i would really appreciate any help on this one. I am trying to get my java game created with the ACM package to play sound effects. I have all the .wav files i need and added to the package but have tried many things to get them to play in the program but nothing seems to work.
    I have tried for example :

    AudioClip hit = new AudioClip("hit.wav") ;
    hit.play();

    AudioClip hit = MediaTools.loadAudioClip("hit.wav");
    hit.play();

    AudioClip = getAudioClip("hit.wav");

    MediaClip hit = new MediaClip("hit.wav");


    does anyone have any idea what to do please? thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Audio In My Program

    You could download the source for this program, which plays a song in the background: Little Bird, Fly Away
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Audio In My Program

    I need to get the game to make a sound effect everytime the ball hits off something etc. its just not happening for me.. any ideas??

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Audio In My Program

    Quote Originally Posted by Fordy252 View Post
    I need to get the game to make a sound effect everytime the ball hits off something etc. its just not happening for me.. any ideas??
    Did you look at the code in the source files I linked? It only takes maybe 3 lines to play a sound.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Fordy252 (February 5th, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Audio In My Program

    ya i tried it.. but it still doesn't work for some reason.. thanks anyway.

  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Audio In My Program

    Can you please post your code.

    When you say Kevins code doesn't work, does it give an error? Does any sound play at all?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  8. #7
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Audio In My Program

    this is the method i use.. and i call it from the main program by putting startSound() wherever i need the sound effect. no sound plays and come many errors below in the console when it tries to run this.

    // play sound effect 
    	public void startSound(){
    		try{
    			AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource("hit.wav"));
    			sound = AudioSystem.getClip();
    			sound.open(stream);
    			sound.start();
    		}
    		catch(Exception e){
    			e.printStackTrace();
    			sound = null;
    		}
    	}
        }
    Last edited by KevinWorkman; February 7th, 2011 at 07:47 AM. Reason: added highlight tags

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Audio In My Program

    Quote Originally Posted by Fordy252 View Post
    this is the method i use.. and i call it from the main program by putting startSound() wherever i need the sound effect. no sound plays and come many errors below in the console when it tries to run this.
    When you post code, it should be in the form of an SSCCE so that we can copy, paste, and run it. Also, use the code or highlight tags to preserve formatting. I've edited your post to do so.

    What are these "many errors"? We can't guess at what's going on- you have to be very specific. Copy and paste the whole stack trace, along with the SSCCE that demonstrates the problem, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Audio In My Program
    By Fordy252 in forum Java Applets
    Replies: 0
    Last Post: January 29th, 2011, 01:27 PM
  2. Audio Source Separation
    By swami in forum Java Theory & Questions
    Replies: 0
    Last Post: July 26th, 2010, 02:36 AM
  3. Do I need to close an audio clip?
    By m2oswald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2010, 01:20 AM
  4. Help with java audio samples please!
    By Reg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2010, 08:41 AM
  5. sun.audio
    By bguy in forum Java SE APIs
    Replies: 3
    Last Post: November 12th, 2009, 12:17 AM