3

以下是我在应用程序中录制音频的代码

public class AudioRecordActivity extends Activity
  {
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;

private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;

private PlayButton   mPlayButton = null;
private MediaPlayer   mPlayer = null;
static int i=1;

private void onRecord(boolean start) {
    if (start) {
       i++;
       String name="AudioRecord"+i;
        startRecording(name);
    } else {
        stopRecording();
    }
}

private void onPlay(boolean start) {
    if (start) {
        startPlaying();
    } else {
        stopPlaying();
    }
}

private void startPlaying() {
    mPlayer = new MediaPlayer();
    try {
        mPlayer.setDataSource(mFileName);
        mPlayer.prepare();
        mPlayer.start();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }
}

private void stopPlaying() {
    mPlayer.release();
    mPlayer = null;
}

private void startRecording(String fileName) {

     File storageDir = new File(Environment
             .getExternalStorageDirectory(), "/audio/");
     storageDir.mkdir();

        mRecorder = new MediaRecorder();
        ContentValues values = new ContentValues(3);
        values.put(MediaStore.MediaColumns.TITLE, fileName);
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mRecorder.setOutputFile("/sdcard/sound/" + fileName);
        try {
            mRecorder.prepare();
        } catch (Exception e){
            e.printStackTrace();
        }

        final ProgressDialog mProgressDialog = new ProgressDialog(AudioRecordActivity.this);
        mProgressDialog.setTitle("Recording");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mProgressDialog.dismiss();
            mRecorder.stop();
            mRecorder.release();
            }
        });

        mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
            public void onCancel(DialogInterface p1) {
                mRecorder.stop();
                mRecorder.release();
            }
        });
        mRecorder.start();
        mProgressDialog.show();
    }

private void stopRecording() {
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

class RecordButton extends Button {
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                setText("Stop recording");
            } else {
                setText("Start recording");
            }
            mStartRecording = !mStartRecording;
        }
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    }
}

class PlayButton extends Button {
    boolean mStartPlaying = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onPlay(mStartPlaying);
            if (mStartPlaying) {
                setText("Stop playing");
            } else {
                setText("Start playing");
            }
            mStartPlaying = !mStartPlaying;
        }
    };

    public PlayButton(Context ctx) {
        super(ctx);
        setText("Start playing");
        setOnClickListener(clicker);
    }
}

public void AudioRecordTest() {
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";
}

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    LinearLayout ll = new LinearLayout(this);
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));
    mPlayButton = new PlayButton(this);
    ll.addView(mPlayButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));
    setContentView(ll);
}

@Override
public void onPause() {
    super.onPause();
    if (mRecorder != null) {
        mRecorder.release();
        mRecorder = null;
    }

    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
}

}

我得到的错误如下:

03-12 16:58:22.847: E/AndroidRuntime(459): FATAL EXCEPTION: main
03-12 16:58:22.847: E/AndroidRuntime(459): java.lang.IllegalStateException
03-12 16:58:22.847: E/AndroidRuntime(459):  at android.media.MediaRecorder.start(Native Method)
03-12 16:58:22.847: E/AndroidRuntime(459):  at org.audio.AudioRecordActivity.startRecording(AudioRecordActivity.java:105)
03-12 16:58:22.847: E/AndroidRuntime(459):  at org.audio.AudioRecordActivity.onRecord(AudioRecordActivity.java:39)
03-12 16:58:22.847: E/AndroidRuntime(459):  at org.audio.AudioRecordActivity.access$0(AudioRecordActivity.java:35)
03-12 16:58:22.847: E/AndroidRuntime(459):  at org.audio.AudioRecordActivity$RecordButton$1.onClick(AudioRecordActivity.java:120)
03-12 16:58:22.847: E/AndroidRuntime(459):  at android.view.View.performClick(View.java:2485)
03-12 16:58:22.847: E/AndroidRuntime(459):  at android.view.View$PerformClick.run(View.java:9080)
03-12 16:58:22.847: E/AndroidRuntime(459):  at android.os.Handler.handleCallback(Handler.java:587)
03-12 16:58:22.847: E/AndroidRuntime(459):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 16:58:22.847: E/AndroidRuntime(459):  at android.os.Looper.loop(Looper.java:123)
03-12 16:58:22.847: E/AndroidRuntime(459):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-12 16:58:22.847: E/AndroidRuntime(459):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 16:58:22.847: E/AndroidRuntime(459):  at java.lang.reflect.Method.invoke(Method.java:507)
03-12 16:58:22.847: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-12 16:58:22.847: E/AndroidRuntime(459):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-12 16:58:22.847: E/AndroidRuntime(459):  at dalvik.system.NativeStart.main(Native Method)

请查看代码。我没有得到问题。提前致谢。

4

2 回答 2

2

仔细看看下面的 LogCat:

03-12 16:58:22.847: E/AndroidRuntime(459): java.lang.IllegalStateException

03-12 16:58:22.847:E/AndroidRuntime(459):在 android.media.MediaRecorder.start(本机方法)

从上面的 logcat 中,我们意识到 start()抛出了IllegalStateException。考虑MediaRecorder需要注册以下内容(按顺序):

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare(); // not properly processed
recorder.start();   // IllegalStateException occurred

所以,

问题:

我们知道prepare()要么(1)没有被调用,要么(2)被调用并且有问题。

我的建议:

  1. start()如下放置prepare()

    try {
        mRecorder.prepare();
        mRecorder.start();      
    } catch (Exception e) {
        e.printStackTrace();
    }
    
  2. 检查您从 LogCat 获得的新警告和问题。可能的警告/问题是:

    (i) 由之前set的 s 引起的问题(例如不兼容/无效的输出格式、AudioSource 的权限等)。

    (ii) 由之前set的 s 引起的异常(例如FileNotFoundException等)。

  3. 确认prepare()清楚地解决。

于 2013-03-12T11:48:33.683 回答
0

感谢您的帮助..完成更正..

protected void startRecording() {
    // TODO Auto-generated method stub
    i++;
     mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
     mFileName += "/audiorecordtest"+i+".3gp";
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(mFileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

      try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show();


    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show();

    }

      recorder.start();
}

 private void stopRecording() {
     recorder.stop();
     recorder.release();
     recorder = null;
    }
于 2013-03-13T05:01:16.940 回答