以下是我在应用程序中录制音频的代码
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)
请查看代码。我没有得到问题。提前致谢。