我无法播放使用MediaRecorder录制的文件 [英] File that I recorded with MediaRecorder can't be played
问题描述
我正在使用MediaRecoder录制声音,但在录制完成后无法播放。我尝试使用Google Play音乐,ES媒体播放器,甚至将其上传到电脑,并尝试用Winamp打开它。什么都不玩!
I'm using MediaRecoder to record sound, but after I'm done recording it can't be played. I tried with Google Play Music, ES Media Player, even uploaded it to pc and tried opening it with Winamp. Nothing plays it!
//AUDIO RECORDER
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
recorder.setOutputFile(externalOutputPath);
}
else
{
storagePath = Environment.getDataDirectory().getAbsolutePath();
recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
}
甚至尝试在按钮点击的情况下在应用程序中打开它:
Even tried opening it inside the application with button click:
public void testPlay (View v) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(externalOutputPath);
mediaPlayer.prepare();
mediaPlayer.start();
}
但这会导致应用程序崩溃。但这不是主要问题,主要问题是我无法播放文件。编码或其他什么问题?
But that crashes the application. But that is not the main problem, the main problem is the fact that there's no way I can play the file. Is there something wrong with encoding or something?
我也尝试将它从.mp3更改为.3gp并且它不起作用。然后我也尝试删除.mp3和.3gp,只留下'test'作为名称,然后它甚至不把它识别为音频文件。
I also tried to change it from .mp3 to .3gp and it didn't work. Then I also tried removing .mp3 and .3gp, leaving just 'test' as a name and then it didn't even recognize it as an audio file.
哦,当应用程序崩溃时,如果有人想要logcat:
Oh, and if anyone wants the logcat when the application crashes:
07-31 16:51:43.953: E/AndroidRuntime(26918): java.lang.IllegalStateException: Could not execute method of the activity
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.lang.reflect.InvocationTargetException
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.io.IOException: setDataSourceFD failed.: status=0x80000000
但同样,app崩溃不是问题。首先,我想解决无法播放音频文件的问题。但如果你知道为什么会崩溃我也会很感激!
But again, app crashing isn't the problem at the moment. First I want to solve the problem why the audio file can't be played. But if you have any idea why it crashes I would also appreciate it!
推荐答案
我仍然不确定是什么问题,但我认为我停止录制的方式有问题(整个尝试,抓住事情)。我只是重写了整个代码并将MediaRecorder放在两种不同的方法中。 startRecording()和stopRecording()方法。现在它完美无缺!
I'm still not sure what was the problem, but I think there was something wrong with the way I stopped recording (the whole try, catch thing). I just rewrote the whole code and put the MediaRecorder in two different methods. startRecording() and stopRecording() method. And now it works perfectly!
startRecording()
startRecording()
public void startRecording (){
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
recorder.setOutputFile(externalOutputPath);
}
else
{
storagePath = Environment.getDataDirectory().getAbsolutePath();
recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
}
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopRecording()
stopRecording()
public void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
这篇关于我无法播放使用MediaRecorder录制的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!