0

I have a problem with this method. In the following i have written a test code that reads an audio stream into a byte array (and does a convertion [i have mp3plugin.jar as external jar, if you should wonder why this works] and some other stuff, but i have only let that in to make the code executable). The problem is that the method seams not allways to do the same thing. Sometimes the AudioInputStream is getting smaller and sometimes it is not. My question is why?

package doej.test;

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class Test2 {

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        int totalFramesRead = 0;
        File fileIn = new File("D:/User/Music/eclipseTestfiles/test.mp3");
        try {
            AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(fileIn);
            AudioFormat sourceFormat = mp3Stream.getFormat();
            AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);
            AudioInputStream converted = AudioSystem.getAudioInputStream(convertFormat, mp3Stream);
            int bytesPerFrame = converted.getFormat().getFrameSize();
            int numBytes = 1024 * bytesPerFrame;
            byte[] audioBytes = new byte[numBytes];
            try {
                int numBytesRead = 0;

                //My simplified Test part
                System.out.println(converted.available());

Ok, so this is the original size of the audio stream. (Until here everything is fine)

                System.out.println("read " + converted.read(audioBytes));
                System.out.println(converted.available());

Why is my Stream getting smaller? (in the documentation they say it is reading into the array so i kinda understand that part)

                System.out.println("read " + converted.read(audioBytes));
                System.out.println(converted.available());

Why is my Stream not getting smaller, as it did the first time? (for this is the part i have no clue)

                while ((numBytesRead = converted.read(audioBytes)) != -1) {
                    System.out.println(converted.available());

Why is the Stream now getting smaller every eleven times? (here I don't understand the world anymore)

                }
                //End of my test part

            } catch (Exception ex) {
            }
            System.out.println("finished");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Finally this is the output i get:

6043688
read 4096
6035592
read 4096
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6035592
6029324
6029324
6029324
[...]
4444
4444
4444
0
0
0
0
0
0
0
0
0
0
0
0
finished

Thanks for your help.

John

4

0 回答 0