【发布时间】:2016-12-05 22:38:51
【问题描述】:
我正在尝试从 android 设备上本地文件系统中的文件获取音频输入流。
这样我就可以使用这个库来显示音频文件的波形。
项目中的例子就是这样使用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);
【问题讨论】:
-
请提供minimal reproducible example,包括使用
new File(filePath)的代码,并完整解释“似乎无法正常工作”的含义。 -
我基本上需要从文件中获取音频输入流。
-
文件是在resources目录下还是在下载?
-
@DoronYakovlev-Golani 该文件由媒体记录器创建。所以它已经在本地文件系统中(没有下载,也没有在资源中)。我可以用 MediaPlayer 正确播放它,没有任何问题。我只需要获取它的“音频输入流”并将其提供给该库。
-
你是如何获得文件路径的,那么具体的错误是什么?
标签: android