媒体记录中的Android非法状态异常

媒体记录中的Android非法状态异常,android,android-emulator,Android,Android Emulator,在下面的代码中,有两个按钮,一个用于开始录制,另一个用于停止录制。我一运行代码,就发生了illegalStateException,但emulator的sd卡上存储了一个具有给定名称的文件。下面是代码: package com.example.try_music; import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.ContentRes

在下面的代码中,有两个按钮,一个用于开始录制,另一个用于停止录制。我一运行代码,就发生了illegalStateException,但emulator的sd卡上存储了一个具有给定名称的文件。下面是代码:

    package com.example.try_music;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

private Button btnStart;
private Button btnStop;

File audioFile = null;
MediaRecorder recorder;

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

    btnStart = (Button)findViewById(R.id.btnStart);
    btnStop = (Button)findViewById(R.id.btnStop);

}

public void startRecording(View view) throws IllegalStateException, IOException
{
    btnStart.setEnabled(false);
    btnStop.setEnabled(true);

    File fileSample = Environment.getExternalStorageDirectory();
    try {
        audioFile = File.createTempFile("my_sound", ".3gp", fileSample);
    } catch (IOException e) {           
        Log.e("myerrortag", "sdcard access error");
    }

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
    recorder.setOutputFile(audioFile.getAbsolutePath());
    recorder.reset();
    recorder.prepare();
    recorder.start();
}
public void stopRecording(View view)
{
    btnStart.setEnabled(true);
    btnStop.setEnabled(false);
    recorder.stop();
    recorder.release();
    addRecordingToMediaLibrary();
}

public void addRecordingToMediaLibrary() {
    ContentValues values = new ContentValues(4);
    long current = System.currentTimeMillis();
    values.put(MediaStore.Audio.Media.TITLE, "audio"+audioFile.getName());
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int)(current/1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE,"audio/3gpp");
    values.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath());
    ContentResolver contentResolver = getContentResolver();

    Uri uriBase = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Uri newUri  = contentResolver.insert(uriBase, values);

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
    Toast.makeText(getApplicationContext(), "added file", Toast.LENGTH_SHORT).show();       
}

}
以下是logcat条目:

05-27 16:50:49.453: E/AndroidRuntime(23804): java.lang.IllegalStateException: Could not execute method of the activity

我已经添加了所有必要的许可。我们将非常感谢您的帮助。提前感谢。

引发异常的行号?单击按钮或活动启动时从何处获取illegalStateException?
illegalStateException:无法执行活动的方法
显然意味着您正试图通过创建活动实例来调用活动方法。因此,plz还可以共享您在活动中调用
startRecording
stopRecording
方法的位置。为什么要调用reset?@Pratik Error被抛出到recorder.start()。