[英]Getting different bytes from the same audio file
我正在嘗試將音頻文件轉換為標准格式並讀取字節。
我要做的第一件事是:
static void main(String [] args)
{
File file = new File("/path/to/my/file.mp3");
AudioInputStream source = AudioSystem.getAudioInputStream(file);
AudioFormat sourceFormat = source.getFormat();
AudioFormat convertFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
sourceFormat.getSampleRate(),
16,
sourceFormat.getChannels(),
sourceFormat.getChannels() * 2,
sourceFormat.getSampleRate(),
false);
AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, source);
AudioFormat targetFormat = new AudioFormat(
44100,
8,
1,
true,
true);
AudioInputStream target = AudioSystem.getAudioInputStream(targetFormat, convert1AIS);
byte[] buffer = new byte[4096];
//Here come strange things
target.read(buffer,0, buffer.length);
}
每次我啟動程序時,緩沖區包含不同的字節,即前10個字節,我在調試器中看到的字節永遠都不相同。 怎么了?
我添加了以下庫:jl1.0.1.jar,jtransform-2.4.jar,mp3spi1.9.5.jar,tritonus_remaining-0.3.6.jar,tritonus_share.jar
操作系統。 Ubuntu 14.04
Java版本“ 1.8.0_111” Java™SE運行時環境(內部版本1.8.0_111-b14)Java HotSpot(TM)64位服務器VM(內部版本25.111-b14,混合模式)
核心是: 不能保證對read的調用將始終從輸入中讀取完全相同的字節數。
相反,該read方法將讀回的字節數返回給您。 因此,您始終需要一個循環 ,一直循環讀取,直到真正讀取了所有預期的字節為止!
編輯:給出您的評論:您正在此處進行音頻轉換 ; 所以我的猜測是:這個過程根本不是確定性的。 換句話說:該MP3文件的實際輸入字節將始終相同; 但是您的代碼對此進行了轉換; 如前所述; 這些轉換是不確定的; 因此,每次運行都會產生略有不同的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.