File that I recorded with MediaRecorder can't be played

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

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?

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.

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

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!

回答1:

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()

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()

public void stopRecording() {     if (null != recorder) {         recorder.stop();         recorder.reset();         recorder.release();          recorder = null;     } }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!