问题
I'm trying to play some .m4a files, and I understand that JAAD only supports decoding AAC, but there are songs that I am able to get the sourceDataLine from, and then when I go to try to play them, I get behavior like this:
We read: 1024 bytes.
We read: 512 bytes.
We read: -1 bytes.
When running this:
// read from the input
bytesRead = audioInputStream.read(tempBuffer, 0, tempBuffer.length);
System.out.println("We read: " + bytesRead + " bytes.");
until bytesRead == -1
For this particular file, I'm getting the AudioFormat baseformat to be this: MPEG1L1 48000.0 Hz, unknown bits per sample, mono, unknown frame size, 125.0 frames/second.
Then the AudioFormat decodedFormat to be this: PCM_SIGNED 48000.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian
I use these line of code to make the conversion:
AudioFormat baseFormat = audioInputStream.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
Am I doing something wrong here? I don't fully understand that that second line really does, but it's been working just fine for decoding MP3 files using the MP3SPI.
I'd really appreciate any guidance here.
回答1:
Is the problem that your file is only showing 1024 + 512 bytes in length? (That would be awfully short for an audio file!) Or is the question about File Formats? I've seen some people run into a problem when they try to decode an mp3 of a .wav file that happens to be incompatible with the range of wav file formats supported by Java.
I'm assuming the "second line" you refer to is the creation of a new FileFormat, yes? That is simply the act of making a new Format based upon the one being decoded from your inputstream. Presumably, you will use the new format in your playback.
The point of the new format is probably to ensure the data will be played with a format compatible with your system. It says: (1) that no matter the incoming encoding format, Signed PCM will be used. (2) the same sample rate will be used (my system only supports 44100, am surprised to see yours allowing 48000). (3) 16-bits will be the new number of bits per sample, regardless of the number of bits used in the original original file. (4) the same number of channels will be used as the original file. (5) the number of channels * 2 will be deemed to be the frame size (makes sense, given 16 bits per sample) (6) the same frames per second rate (7) the byte order will be little-endian, regardless of the input file order.
You can look into the API of this constructor (some good documentation) under "Constructor Detail" at this link: http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioFormat.html
If you need to convert some of the audio data in order to allow playback, then things get more involved. Have you read through this tutorial on audio file & format conversion? http://docs.oracle.com/javase/tutorial/sound/converters.html
I hope this is at least of partial help. I haven't used JAAD myself, so I can understand if this post isn't very helpful.
来源:https://stackoverflow.com/questions/10369221/javasound-not-playing-m4a-files-through-jaad-an-spi