2016-12-02 79 views
0

我想從Android設備上的本地文件系統中的文件獲取音頻輸入流。從Android上的語言環境文件獲取音頻輸入流

這是我可以使用這個庫來顯示音頻文件的波形。

https://github.com/newventuresoftware/WaveformControl/blob/master/app/src/main/java/com/newventuresoftware/waveformdemo/MainActivity.java#L125

在項目的示例使用rawResource像這樣

InputStream is = getResources().openRawResource(R.raw.jinglebells); 

該輸入流被以後轉換成字節數組,並傳遞給使用它描繪出波的圖像和聲音的某個地方。

但是當我做了

InputStream is = new InputFileSystem(new File(filePath)); 

但是,這似乎並沒有正常工作。生成的圖像是錯誤的,播放的聲音與文件的實際內容無關。

這是該庫中函數的主體,它獲取輸入流並將其轉換爲字節數組。由MediaRecorder創建具有以下配置的

MediaRecorder recorder = new MediaRecorder(); 
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
+0

請提供[MCVE],包括使用'新文件(文件路徑)'代碼,以及一個完整的解釋什麼「似乎沒有正常工作」的意思。 – CommonsWare

+0

我基本上需要從文件中獲取音頻輸入流。 – user1017674

+0

文件是否位於資源目​​錄中,或者正在下載它? –

回答

0

private short[] getAudioSample() throws IOException { 
    // If i replace this part with new FileInput(new File(filePath)) 
    // the generated "samples" from it does not work properly with the library. 
    InputStream is = getResources().openRawResource(R.raw.jinglebells); 
    byte[] data; 
    try { 
     data = IOUtils.toByteArray(is); 
    } finally { 
     if (is != null) { 
      is.close(); 
     } 
    } 
    ShortBuffer sb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer(); 
    short[] samples = new short[sb.limit()]; 
    sb.get(samples); 
    return samples; 
} 

,我想得到處理,並傳遞給該庫中的聲音文件基本上這庫需要PCM樣本。直接使用由MediaRecorder生成的3gp FileInputStream不會削減它。

我所做的是使用MediaExtractor和MediaCodec通過採樣將3gp音頻數據轉換爲PCM採樣。然後它被送入該圖書館。然後一切工作=)

編碼音頻數據的邏輯可以從這裏幾乎直接採取this awesome github repo