从android

时间:2016-12-02 20:43:50

标签: 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));

但这似乎不能正常工作。生成的图像是错误的,播放的声音与文件的实际情况完全不同。

这是该库中函数的主体,它获取输入流并将其转换为字节数组。

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;
}

我想要处理并传递到该库的声音文件是由具有以下配置的MediaRecorder创建的

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

0 个答案:

没有答案