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

    Default Why does my sound "lag"?

    Hai!
    Im trying to stream audio.
    But I am real stuck when it comes to playing same file over eachother!
    Because right now, I can play audio.
    I got a method that loads audio, and one that plays it.
    and I can add the music when ever I want to the audio player.
    But If I load one music, then add ot tp the player twice
    then only the first added will play normaly, while the other will sorta "lag"
    only playing small 0.3 seconds parts of the whole song.

    Any idea what could usually cause this?
    Heres my soundObj getAudio method, I can publish the otherparts if needed
    but they take so much extra space:

    PHP Code:
    private static soundObj addAudio(Mixer myMixer, String string) throws UnsupportedAudioFileException, IOException {
    		 String path = "C:\\Users\\Therese\\Desktop\\SOUNDS\\";
    		 AudioInputStream stream = AudioSystem.getAudioInputStream(new File(path+string));
    	    soundObj sound = new soundObj();
    		try {
    			byte[] data;
    			int BUFSIZE = 128000;
    			AudioInputStream ais = stream;
    			data = new byte[(int)ais.getFrameLength() * stream.getFormat().getFrameSize()];
    			byte[] buf = new byte[BUFSIZE];
    			for (int i=0; i<data.length; i+=BUFSIZE) {
    			    int r = ais.read(buf, 0, BUFSIZE);
    			    if (i+r >= data.length) {
    			        r = i + r - data.length;
    			    }
    			    System.arraycopy(buf, 0, data, i, r);
    			}
    			ais.close();
    			
    			sound.data = data;
    		    ByteArrayInputStream byteArrayInputStream = new 
    		    ByteArrayInputStream(data);
    		    sound.byteArrayStream = byteArrayInputStream;
    
    		    sound.format = stream.getFormat();
    		    sound.frameLength = stream.getFrameLength();
    		    sound.frameSize = stream.getFormat().getFrameSize();
    			sound.soundName = "Sound:"+audioNum;
    			audioNum += 1;
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		   	// Handle the error.
    		    //... 
    		}
    		
    		
    		return sound;
    	}

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

    Default

    Okej..
    This is weird..
    Right now I do like this:
    playAudio(song.clone())
    sleep(1000)
    playAudio(song2)
    sleep(1000)
    playAudio(song)

    Now, there is no problem playing song.clone() (a clone of the song once loaded) nor song2, but when I come to playing song, the real song song that is, then it gives such a wierd sound, like im chocking or somthing (its me singing on the tapes, You dont wana hear XD)

    Why is that? I cant figure what they have incommon :(
    I guess to solve this the rest of the app is needed.
    This down here might seem much.
    But to run it, all you gotta do is change the values in the beggining of Test.java inside the call to "addAudio"
    and change the path value in the "addAudio" to your path to the files.
    Thanks alot if you take time helping me :o

    mixerTable.java:
    PHP Code:
    import java.util.Vector;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.Mixer;
    import javax.sound.sampled.SourceDataLine;
    
    
    public class mixerTable {
    	public static boolean isPlaying = false;
    	public static Mixer mixer;
    	static Vector<soundObj> sounds = new Vector<soundObj>();
    	
    	public static void playAudio(soundObj stream) {
    		if (isPlaying == false){
    			sounds.add(stream);
    			isPlaying = true;
    			System.out.println("Start player");
    			System.out.println("Adding Sound:"+stream.soundName);
    			new Thread(new audioPlayer()).start();
    		} else {
    			System.out.println("Adding Sound:"+stream.soundName);
    			sounds.add(stream);
    			System.out.println("CurrentSounds:");
    			for (int i = 0;i<sounds.size();i++){
    				System.out.println(sounds.elementAt(i).soundName);
    			}
    			
    		}
    	}
    
    }
    
    class audioPlayer implements Runnable {
    
    	@Override
    	public void run() {
    		int numBytesRead = 0;
    		byte[] buffer = new byte[128000];
    		while (mixerTable.isPlaying == true){
    	    	for (int i = 0;i<mixerTable.sounds.size();i++){
    	    		soundObj currentObj = mixerTable.sounds.elementAt(i);
    	    		try{
    	    			//AudioInputStream stream = currentObj.stream;
    	    			SourceDataLine line;
    	    			if (currentObj.gotLine == false){
    	    				SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, currentObj.format, ((int) currentObj.frameLength * currentObj.frameSize));
    		    			line = (SourceDataLine) mixerTable.mixer.getLine(info);
    		    		    line.open(currentObj.format);
    		    		    line.start();
    		    		    currentObj.gotLine = true;
    		    		    currentObj.line = line;
    	    			}
    	    			line = currentObj.line;
    					numBytesRead = currentObj.byteArrayStream.read(buffer,0,200);//stream.read(buffer,0,2000); //
    					
    					if (numBytesRead != -1 ){
    						line.write(buffer, 0, numBytesRead);
    					} else {
    						System.out.println("Finished playing: "+currentObj.soundName);
    						line.drain();
    						line.close();
    						mixerTable.sounds.remove(i);
    					}
    	    		} catch ( Exception e){
    	    			e.printStackTrace();
    	    		}
    		    }
    	    	if (mixerTable.sounds.size() < 1){
    	    		mixerTable.isPlaying = false;
    			}
    		}
    		try {
    			this.finalize();
    		} catch (Throwable e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    }
    soundObj.java:
    PHP Code:
    import java.io.ByteArrayInputStream;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.SourceDataLine;
    
    public class soundObj implements Cloneable{
    	AudioInputStream stream;
    	SourceDataLine line;
    	boolean gotLine = false;
    	String soundName = "";
    	byte[] data;
    	ByteArrayInputStream byteArrayStream;
    	public AudioFormat format;
    	public int frameSize;
    	public long frameLength;
    	
    	public Object clone(){ 
    		try{         
    			soundObj s = (soundObj) super.clone();
    			s.byteArrayStream = this.byteArrayStream;
    			s.data = this.data;
    			s.format = this.format;
    			s.frameLength = this.frameLength;
    			s.frameSize = this.frameSize;
    			s.gotLine = false;
    			s.line = null;
    			s.soundName = s.soundName+".Clone";
    			
    			return s;         
    		} catch( Exception e ){
    			return null; 
    		}      
    	} 
    }
    and the whole first part:
    Test.java:
    PHP Code:
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.IOException;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Mixer;
    import javax.sound.sampled.UnsupportedAudioFileException;
    
    public class Test {
    
    	private static int audioNum = 0;
    	public static void main(String[] argv) throws Exception {
    	 
    	  //get mixer
    	  Mixer.Info[] info2 = AudioSystem.getMixerInfo();
    	  Mixer myMixer;
    	  myMixer = AudioSystem.getMixer(info2[0]);
    	  soundObj stream = addAudio("1st.wav");
    	  soundObj stream2 = addAudio("we.wav");
    	  //soundObj stream3 = addAudio(myMixer,"lolo2.wav");
    	  mixerTable.mixer = myMixer;
    	  mixerTable.playAudio((soundObj) stream.clone());
    	  sleep(1000);
    	  mixerTable.playAudio(stream2);
    	  sleep(1000);
    	  mixerTable.playAudio(stream);
      }
    
    	private static soundObj addAudio(String string) throws UnsupportedAudioFileException, IOException {
    		 String path = "C:\\Users\\Therese\\Desktop\\SOUNDS\\";
    		 AudioInputStream stream = AudioSystem.getAudioInputStream(new File(path+string));
    	    soundObj sound = new soundObj();
    		try {
    			byte[] data;
    			int BUFSIZE = 128000;
    			AudioInputStream ais = stream;
    			data = new byte[(int)ais.getFrameLength() * stream.getFormat().getFrameSize()];
    			byte[] buf = new byte[BUFSIZE];
    			for (int i=0; i<data.length; i+=BUFSIZE) {
    			    int r = ais.read(buf, 0, BUFSIZE);
    			    if (i+r >= data.length) {
    			        r = i + r - data.length;
    			    }
    			    System.arraycopy(buf, 0, data, i, r);
    			}
    			ais.close();
    			
    			sound.data = data;
    		    ByteArrayInputStream byteArrayInputStream = new 
    		    ByteArrayInputStream(data);
    		    sound.byteArrayStream = byteArrayInputStream;
    		    //AudioInputStream realStream = AudioSystem.getAudioInputStream(byteArrayInputStream);
    			//sound.stream = realStream;
    		    sound.format = stream.getFormat();
    		    sound.frameLength = stream.getFrameLength();
    		    sound.frameSize = stream.getFormat().getFrameSize();
    			sound.soundName = "Sound:"+audioNum;
    			audioNum += 1;
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		   	// Handle the error.
    		    //... 
    		}
    		
    		
    		return sound;
    	}
    	
    	private static void sleep(int i) {
    		try{
    			Thread.currentThread().sleep(i);
    		  } catch (Exception e){}
    	}
    		
    
    }

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  3. Replies: 2
    Last Post: 10-08-2009, 11:28 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •