录制和播放音频不起作用

时间:2014-04-13 22:16:01

标签: android audio android-mediaplayer audio-recording android-audiorecord

我制作了一个简单的测试应用程序,它有四个按钮,开始录制,停止录制,播放和停止。

问题是我无法听到任何录音,而且我甚至不确定录音是否已被保存。

这是我的活动代码

public class MainActivity extends Activity {

    private MediaPlayer mediaPlayer;
    private MediaRecorder recorder;
    private String OUTPUT_FILE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OUTPUT_FILE = Environment.getExternalStorageDirectory()+"/audiorecorder.3gpp";
    }

    public void buttonTapped (View view){
        switch(view.getId()){

        case R.id.btn_start_recording:
            try{
                beginRecording();
            } catch (Exception e){
                e.printStackTrace();
            }

        case R.id.btn_stop_recording:
            try{
                stopRecording();
            } catch (Exception e){
                e.printStackTrace();
            }

        case R.id.btn_play:
            try{
                playRecording();
            } catch (Exception e){
                e.printStackTrace();
            }

        case R.id.btn_stop:
            try{
                stopPlayback();
            } catch (Exception e){
                e.printStackTrace();
            }       

        }
    }

    private void stopPlayback() {
        if(mediaPlayer != null)
            mediaPlayer.stop();
    }

    private void playRecording() throws Exception{
        ditchMediaPlayer();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(OUTPUT_FILE);
        mediaPlayer.prepare();
        mediaPlayer.start();
    }

    private void ditchMediaPlayer() {
        if(mediaPlayer != null){
            try{
                mediaPlayer.release();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    private void stopRecording() {
        if(recorder != null)
            recorder.stop();
    }

    private void beginRecording() throws Exception {
        ditchMediaRecorder();
        File outFile = new File(OUTPUT_FILE);

        if(outFile.exists())
            outFile.delete();

        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        recorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());
        recorder.setAudioEncodingBitRate(16);
        recorder.setAudioSamplingRate(44100);
        recorder.setOutputFile(OUTPUT_FILE);
        recorder.prepare();
        recorder.start(); 
    }

    private void ditchMediaRecorder() {
        //if we have the recorder, release it
        if(recorder != null)
            recorder.release();
    }

}

我遇到的错误类型

04-13 15:07:56.618: W/System.err(2542): java.io.IOException: Prepare failed.: status=0x1
04-13 15:07:56.618: W/System.err(2542):     at android.media.MediaPlayer.prepare(Native Method)
04-13 15:07:56.618: W/System.err(2542):     at com.appulse.audiomate.MainActivity.playRecording(MainActivity.java:70)
04-13 15:07:56.628: W/System.err(2542):     at com.appulse.audiomate.MainActivity.buttonTapped(MainActivity.java:46)
04-13 15:07:56.628: W/System.err(2542):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 15:07:56.628: W/System.err(2542):     at java.lang.reflect.Method.invoke(Method.java:515)
04-13 15:07:56.628: W/System.err(2542):     at android.view.View$1.onClick(View.java:3818)
04-13 15:07:56.628: W/System.err(2542):     at android.view.View.performClick(View.java:4438)
04-13 15:07:56.628: W/System.err(2542):     at android.view.View$PerformClick.run(View.java:18422)
04-13 15:07:56.628: W/System.err(2542):     at android.os.Handler.handleCallback(Handler.java:733)
04-13 15:07:56.628: W/System.err(2542):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-13 15:07:56.628: W/System.err(2542):     at android.os.Looper.loop(Looper.java:136)
04-13 15:07:56.628: W/System.err(2542):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 15:07:56.628: W/System.err(2542):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 15:07:56.628: W/System.err(2542):     at java.lang.reflect.Method.invoke(Method.java:515)
04-13 15:07:56.628: W/System.err(2542):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 15:07:56.628: W/System.err(2542):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 15:07:56.628: W/System.err(2542):     at dalvik.system.NativeStart.main(Native Method)
04-13 15:07:56.628: E/MediaPlayer(2542): stop called in state 0
04-13 15:07:56.628: E/MediaPlayer(2542): error (-38, 0)
04-13 15:07:56.628: E/MediaPlayer(2542): Error (-38,0)

1 个答案:

答案 0 :(得分:0)

你在这里错过了几件事,在每个案例中添加了break语句,在函数buttonTapped中,

public void buttonTapped (View view){
    switch(view.getId()){

    case R.id.btn_start_recording:
        try{
            beginRecording();
        } catch (Exception e){
            e.printStackTrace();
        }
        break;

    case R.id.btn_stop_recording:
        try{
            stopRecording();
        } catch (Exception e){
            e.printStackTrace();
        }
        break;

    case R.id.btn_play:
        try{
            playRecording();
        } catch (Exception e){
            e.printStackTrace();
        }
        break;

    case R.id.btn_stop:
        try{
            stopPlayback();
        } catch (Exception e){
            e.printStackTrace();
        } 
        break;

    }
}

并在清单文件中添加这些权限。

<uses-permission android:name="android.permission.RECORD_AUDIO" />    
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>