import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button mStart;
Button mStop;
Button mPlay;
Button mPlayStop;
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStart = (Button)findViewById(R.id.strt);
mStart.setOnClickListener(this);
mStop = (Button)findViewById(R.id.stp);
mStop.setOnClickListener(this);
mPlay = (Button)findViewById(R.id.ply);
mPlay.setOnClickListener(this);
mPlayStop = (Button)findViewById(R.id.plystp);
mPlayStop.setOnClickListener(this);
mFileName = getFilesDir().getAbsolutePath();
mFileName += "/audiorecordtest1111";
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.strt:start();
break;
case R.id.stp: stop();
break;
case R.id.ply: play();
break;
case R.id.plystp:playstop();
break;
}
}
public void start(){
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setOutputFile(mFileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
recorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
recorder.start();
}
public void stop()
{
MediaRecorder recorder = new MediaRecorder();
recorder.stop();
recorder.release();
recorder = null;
}
public void play()
{
MediaPlayer mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
public void playstop()
{
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.release();
mPlayer = null;
}
}
LOGCAT
02-25 11:32:18.383: E/MediaRecorder(12854): start failed: -2147483648
02-25 11:32:18.403: E/AndroidRuntime(12854): FATAL EXCEPTION: main
02-25 11:32:18.403: E/AndroidRuntime(12854): java.lang.RuntimeException: start failed.
02-25 11:32:18.403: E/AndroidRuntime(12854): at android.media.MediaRecorder.start(Native Method)
02-25 11:32:18.403: E/AndroidRuntime(12854): at com.example.recorder.MainActivity.start(MainActivity.java:93)
02-25 11:32:18.403: E/AndroidRuntime(12854): at com.example.recorder.MainActivity.onClick(MainActivity.java:68)
02-25 11:32:18.403: E/AndroidRuntime(12854): at android.view.View.performClick(View.java:4202)
02-25 11:32:18.403: E/AndroidRuntime(12854): at android.view.View$PerformClick.run(View.java:17340)
02-25 11:32:18.403: E/AndroidRuntime(12854): at android.os.Handler.handleCallback(Handler.java:725)
02-25 11:32:18.403: E/AndroidRuntime(12854): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 11:32:18.403: E/AndroidRuntime(12854): at android.os.Looper.loop(Looper.java:137)
02-25 11:32:18.403: E/AndroidRuntime(12854): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-25 11:32:18.403: E/AndroidRuntime(12854): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 11:32:18.403: E/AndroidRuntime(12854): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 11:32:18.403: E/AndroidRuntime(12854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-25 11:32:18.403: E/AndroidRuntime(12854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-25 11:32:18.403: E/AndroidRuntime(12854): at dalvik.system.NativeStart.main(Native Method)