将MediaRecorder .3gpp转换为Byte,然后检索回.3gp无效

时间:2018-10-09 03:57:12

标签: java android mediarecorder bytearrayoutputstream

将3gpp(原始)文件转换为字节并将字节取回3gpp(2nd)文件时,我遇到了一个很奇怪的问题。

第二个3gpp文件是我不能使用MediaPlayer播放,也无法在传输到计算机上时播放。另外,第二3gpp文件的大小比原始3gpp小。

1。这就是我初始化MediaRecorder的方式

String voiceStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + File.separator  + "media" + File.separator + "voices" + File.separator + "temp.3gpp";
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(voiceStoragePath);

2。录制完成后,这就是我将其转换为字节数组的方式

File file = new File(voiceStoragePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];

for (int readNum; (readNum = fis.read(b)) != -1;) {
    bos.write(b, 0, readNum);
}

byte[] bytes = bos.toByteArray();

3。现在将字节保存到.3gpp文件中

String voiceStoragePath2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + File.separator  + "media" + File.separator + "voices" + File.separator + "temp2.3gpp";
File path = new File(voiceStoragePath2);
FileOutputStream fos = new FileOutputStream(path);
fos.write(bytes);
fos.close();

4。设备文件资源管理器

enter image description here

5。使用MediaPlayer播放

mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(this);

Log:E / MediaPlayerNative:错误(1,-2147483648)

1 个答案:

答案 0 :(得分:0)

嗯,那件事发生了。显而易见,在将3gpp转换为字节数组之前,必须先停止MediaRecorder 。仍然不敢相信我是多么想念那个。

mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;

byte[] bytes = convert(voiceStoragePath);