This discussion is archived
9 Replies Latest reply: Aug 9, 2010 3:11 PM by Miguel Angel RSS

Method :line.write(audioBytes, 0, audioBytes.length-position) blocks

Miguel Angel Newbie
Currently Being Moderated
I did this method to rewind a real recording.
 public void rewind() throws IOException, LineUnavailableException {
        int numRetro = 0;
        int numBytesRead=0;
        int nBytesWritten = 0;
        pauseRecording();
        SourceDataLine line = null;
        DataLine.Info info  = new DataLine.Info(SourceDataLine.class, frtData);
        byte audioBytes[] = dataCapturada.toByteArray();   
        ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
        AudioInputStream audioMemo = new AudioInputStream(bais, frtoData,
                           audioBytes.length/frtoData.getFrameSize());      
        byte  abData = new byte[audioBytes.length];
        try {
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(frtoData);
        } catch (LineUnavailableException ex) {

        }
        line.start();
        numRetro = (int) (frtoData.getFrameRate() * frtoData.getFrameSize());
        if(position ==0){  //position is a global variable
            position = numRetro;
        }else{
            position = position+numRetro;
        }
        numBytesRead= audioMemo.read(abData, 0,abData.length-position);
        if(abData.length>position){
            nBytesWritten = line.write(audioBytes, 0, audioBytes.length-position);
            dataCaptured.reset();          //dataCaptured is a global variable of ByteArrayOutputStream
            dataCaptured.write(abData, 0, numBytesRead);
        }
With this i am able to cut the "dataCaptured" a "numRetro positions" and then overwrite it wit the new data.
The problem is that this method: line.write(audioBytes, 0, audioBytes.length-position)blocks until it finishes. I used this method to enable the user to listen what he has record until a certain position. The rewind button its pressed until the method line.write... finishes.
How can i make this to not block so the user can press the rewind button and not to wait so much to press it again to continue rewind it and listen at the same time.