无法录制音频

时间:2013-03-12 11:40:33

标签: android audio

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

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)

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

2 个答案:

答案 0 :(得分: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(Native   方法)

从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引起的问题(例如,不兼容/无效的输出格式,对AudioSource的许可等)。

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

  3. 确认prepare()明确解决。

答案 1 :(得分: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;
    }