我有一些关于 Xuggler API 的问题,
我想创建一个小类,一旦打开一个 mp3 文件,它就会从中获取原始字节,但是,有多少?我想从原始文件中获取 1 秒的样本。
这是我当前的代码:
public byte[] getSamples(int iBytesQtty){
byte[] rawBytes = null;
/** Go to the correct packet */
while (this._inputContainer.readNextPacket(this._packet) >= 0){
System.out.println(this._packet.getDuration());
/** Once we have a packet, let's see if it belongs to the audio stream */
if (this._packet.getStreamIndex() == this._iAudioStreamId){
IAudioSamples samples = IAudioSamples.make(iBytesQtty, this._audioCoder.getChannels());
//System.out.println(">> " + samples.toString());
/** Because a packet can contain multiple set of samples (frames of samples). We may need to call
* decode audio multiple times at different offsets in the packet's data */
int iCurrentOffset = 0;
while(iCurrentOffset < this._packet.getSize()){
int iBytesDecoded = this._audioCoder.decodeAudio(samples, this._packet, iCurrentOffset);
iCurrentOffset += iBytesDecoded;
if (samples.isComplete()){
rawBytes = samples.getData().getByteArray(0, samples.getSize());
}
}
return rawBytes;
}
else{
/** Otherwise drop it */
do{}while(false); /** WTF? **/
}
}
return rawBytes; /** This will return null at this point */
}
任何提示将不胜感激,在此先感谢。
编辑:
实际上,这段小代码对我有用,但并非在所有情况下
Mp3File song = new Mp3File("sample.mp3");
byte[] buff = null;
int totalBytes = 0;
int iNumBytes = 1024;
while( (buff = song.getSamples(iNumBytes)) != null ){
totalBytes += buff.length;
// System.out.println("Length buff: " + buff.length + " TOTAL: " + totalBytes);
}
System.out.println("Bytes: " + totalBytes + " (" + totalBytes/1024 + " Kb.)");
float lengthSecs = (float) (totalBytes / ((1411*1000)/8));
float lengthMins = lengthSecs/60;
System.out.println("song duration: " + lengthMins + " mins. (" + lengthSecs + " secs.)");
我假设它实际上在内部转换为常规 WAV 文件的 mp3 文件,所以每秒我有 1411kbps 的速率。但正如我所说,它并不适用于所有情况。