Android:使用MediaRecorder录制后播放媒体文件

时间:2013-01-08 16:30:47

标签: java android audio

播放我刚刚使用MediaRecord课程录制的内容时出现问题。

代码:

                    try {
                        audioSample = File.createTempFile("sample", ".wav", PlayScreen.this.getCacheDir());
                    } catch (IOException e) {
                        Toast.makeText(PlayScreen.this, "ERROR: unable to create temp audio file!" + e.toString(), Toast.LENGTH_LONG).show();
                        return;
                    }

                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    recorder.setOutputFile(audioSample.getAbsolutePath());
                    try {
                        recorder.prepare();
                    } catch (Exception e) {
                        Toast.makeText(PlayScreen.this, "RECORDER ERROR: " + e.toString(), Toast.LENGTH_LONG).show();
                        Log.e("PLAYSCREEN", e.toString());
                        return;
                    }
                    recorder.start();

那是录音。我认为它有效,因为我没有发现任何错误;)

现在播放代码:

                   try {
                        mp.setDataSource(audioSample.getAbsolutePath());
                        mp.prepare();
                        mp.start();
                    } catch (Exception e) {
                        Toast.makeText(PlayScreen.this, "PLAYBACK TEST FAIL: " + e.toString(), Toast.LENGTH_LONG).show();
                    }

我抓住这个例外:java.io.IOException: prepare failed

任何想法可能会发生在这里?我猜测setDataSource电话不正确。

编辑 - 来自stackTrace:

01-08 12:35:35.609: W/System.err(24730): java.io.IOException: Prepare failed.: status=0xFFFFFFFC
01-08 12:35:35.609: W/System.err(24730):    at android.media.MediaPlayer.prepare(Native Method)
01-08 12:35:35.609: W/System.err(24730):    at com.jsx.audiotech.PlayScreen$1.onClick(PlayScreen.java:125)
01-08 12:35:35.609: W/System.err(24730):    at android.view.View.performClick(View.java:2461)
01-08 12:35:35.609: W/System.err(24730):    at android.view.View$PerformClick.run(View.java:8890)
01-08 12:35:35.609: W/System.err(24730):    at android.os.Handler.handleCallback(Handler.java:587)
01-08 12:35:35.609: W/System.err(24730):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 12:35:35.609: W/System.err(24730):    at android.os.Looper.loop(Looper.java:123)
01-08 12:35:35.609: W/System.err(24730):    at android.app.ActivityThread.main(ActivityThread.java:4627)
01-08 12:35:35.613: W/System.err(24730):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 12:35:35.613: W/System.err(24730):    at java.lang.reflect.Method.invoke(Method.java:521)
01-08 12:35:35.613: W/System.err(24730):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-08 12:35:35.613: W/System.err(24730):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-08 12:35:35.617: W/System.err(24730):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我建议下载创建的.wav文件(使用DDMS很容易通过模拟器),并确保您有一个有效的.wav文件。我假设你在MediaRecorder上调用.stop可能是在一个事件中。如果没有,您可能正在尝试准备MediaRecorder仍然打开的音频文件。