Results 1 to 4 of 4
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Post Can't hear the wav file im playing :( (using AudioSystem)

    Hai!
    Im trying to learn how to use javas sound package..
    Even tho it's alot to read I'v read to the point where they espect me to be able to play a clip (my wav file)
    Now, It doesnt work for me :(

    Everything works it gives no error, but I still dont hear anything, and my speakers work..

    I suppose that I have forgot to "connect" the audiofile with the mixer and the mixer with a useable speaker..
    Can you please help me? I'v re-read lots of things but can't make this work :(

    Heres my code, I tried to document it (not too much, not to little):
    PHP Code:
    import java.io.File;
    import java.io.InputStream;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.Mixer;
    import javax.sound.sampled.TargetDataLine;
    
    
    public class MainClass {
    	public static void main(String args[]){
    		//get mixer
    		Mixer.Info[] info2 = AudioSystem.getMixerInfo();
    		for (int i = 0; i<info2.length;i++){
    			//System.out.println(info2[i]);
    		}
    		Mixer myMixer = AudioSystem.getMixer(info2[0]);
    		//got mixer	
    		
    		//get speaker
    		AudioFormat format = getAudioFormat();
    		TargetDataLine line;
    		DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); // format is an AudioFormat object
    		if (!AudioSystem.isLineSupported(info)) {
    		    // Handle the error.
    		    }
    		    // Obtain and open the line.
    		try {
    		    line = (TargetDataLine) AudioSystem.getLine(info);
    		    line.open(format);
    		} catch (LineUnavailableException ex) {
    		   	// Handle the error.
    		    //... 
    		}
    		//got speaker
    		
    		//get clip
    		InputStream inputStream;
    		String path = "C:\\Users\\Therese\\Desktop\\";
    		File soundFile = new File(path+"we.wav");
    		Clip line2;
    		 try {
    	        inputStream = AudioSystem.getAudioInputStream(soundFile);
    			AudioInputStream format2 = new AudioInputStream(inputStream ,getAudioFormat() , 0);
    			DataLine.Info info3 = new DataLine.Info(Clip.class, format2.getFormat()); // format is an AudioFormat object
    			if (!AudioSystem.isLineSupported(info3)) {
    			    // Handle the error.
    				System.out.print("line is not supported");
    			    }
    			    // Obtain and open the line.
    			try {
    				line2 = (Clip) AudioSystem.getLine(info3);
    			    line2.open(format2);
    			    line2.start(); //Play the song!
    			    System.out.print("playing");
    			    
    			} catch (LineUnavailableException ex) {
    				System.out.print("line is unavalible");
    			   	// Handle the error.
    			    //... 
    			}
    			
    		} catch (Exception e1) {
    		            e1.printStackTrace();
    	            
    	    }
    		//Should I do something here?
    
    
    	}
    	
    	private static AudioFormat getAudioFormat(){
    	    float sampleRate = 8000.0F;
    	    //8000,11025,16000,22050,44100
    	    int sampleSizeInBits = 16;
    	    //8,16
    	    int channels = 1;
    	    //1,2
    	    boolean signed = true;
    	    //true,false
    	    boolean bigEndian = false;
    	    //true,false
    	    return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);
    	  }//end getAudioFormat
    }
    Thanks for helping me out :D

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Aww :(
    Because no one knows (or dont like me) I gota use streaming..
    That is more laggy and more difficult..
    Ohwell, what can u do :/

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    I recently wrote a sound manager for the game i'm working on. I'm not sure if it will be helpful to you, but you might have a look and compare notes. Unfortunately for your case it doesn't play .wav files; i think it will only plays .ogg files. Also i'm using VorbisSPI sound libraries and i used their examples.

    SourceForge.net Repository - [legacyclone] View of /src/source/legacyclone/tools/SoundManager.java
    Last edited by mrmatt1111; 03-03-2010 at 07:08 PM.
    My Hobby Project: LegacyClone

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Tyvm :)
    I had a look at it, but it looks like your streaming music..
    I was looking for the non streaming version, but ohwell..
    If it is streaming I g2 learn it myself because I will work much with it :)
    But thanks anyways :D

Similar Threads

  1. Did you hear about the programmer who...
    By fgm1 in forum Entertainment
    Replies: 7
    Last Post: 01-06-2012, 10:03 AM
  2. Playing an Animated GIF
    By c0m4ndo45 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2009, 10:47 AM
  3. playing audio in J-App??
    By ashton in forum New To Java
    Replies: 3
    Last Post: 01-30-2009, 08:50 AM
  4. Re-playing sound
    By JSK in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-06-2008, 11:34 AM

Posting Permissions

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