Android - MediaRecorder start()抛出IllegalStateException start-failed-2147483648

时间:2015-02-16 12:00:32

标签: android

我正在开发一款需要录制音频的应用

这是我的部分代码

private MediaRecorder myAudioRecorder;
private String outputFile = null;
private Button start, stop, play;

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

    start = (Button) findViewById(R.id.button1);
    stop = (Button) findViewById(R.id.button2);
    play = (Button) findViewById(R.id.button3);

    stop.setEnabled(false);
    play.setEnabled(false);

    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myrecording.3gp";;

    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(outputFile);

}


public void start(View view) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
    try {
        myAudioRecorder.prepare();
        myAudioRecorder.start();

    } catch (IllegalStateException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }
    start.setEnabled(false);
    stop.setEnabled(true);
    Toast.makeText(getApplicationContext(), "Recording Started", Toast.LENGTH_LONG).show();

}

上面的代码抛出了IllegalStateException。我已经在AndroidManifest.xml中输入了所有权限,我非常确定AndroidManifest.xml中没有任何错误。

我已经阅读了几个解决方案,但没有一个工作。我该如何解决这个问题?

public void stop(View view) {

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

    stop.setEnabled(false);
    play.setEnabled(true);

    Toast.makeText(getApplicationContext(), "Audio Recorded successfully", Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void play(View view) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {

    MediaPlayer m = new MediaPlayer();
    m.setDataSource(outputFile);
    m.prepare();
    m.start();

    Toast.makeText(getApplicationContext(), "Audio Playing", Toast.LENGTH_LONG).show();

}

}

0 个答案:

没有答案