package com.example.audiorecordtest;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class AudioRecordTest extends Activity implements OnInfoListener {
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName;
long start_time;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
protected Uri newUri;
private MediaPlayer mPlayer = null;
TextView time;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void startRecording() {
try {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setMaxDuration(30000);
mRecorder.setOnInfoListener(this);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
time.setText("Recording...");
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
time.setText("");
mRecorder = null;
addRecordingToMediaLibrary();
finish();
}
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(3);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + mFileName);
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}
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);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
System.out.println("mFileName" + mFileName);
mFileName += "/recording-" + System.currentTimeMillis() + ".3gpp";
System.out.println("mFileName after appending" + mFileName);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
if (imageReturnedIntent != null) {
Uri audioUri = imageReturnedIntent.getData();
System.out.println("audioUri" + audioUri);
}
}
@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));
time = new TextView(this);
ll.addView(time, 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;
}
}
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
// TODO Auto-generated method stub
}
}
日志猫:
11-26 11:01:38.750: E/AndroidRuntime(23749): FATAL EXCEPTION: main
11-26 11:01:38.750: E/AndroidRuntime(23749): java.lang.NullPointerException
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.os.Parcel.readException(Parcel.java:1431)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.content.ContentResolver.insert(ContentResolver.java:864)
11-26 11:01:38.750: E/AndroidRuntime(23749): at com.example.audiorecordtest.AudioRecordTest.addRecordingToMediaLibrary(AudioRecordTest.java:114)
11-26 11:01:38.750: E/AndroidRuntime(23749): at com.example.audiorecordtest.AudioRecordTest.stopRecording(AudioRecordTest.java:101)
11-26 11:01:38.750: E/AndroidRuntime(23749): at com.example.audiorecordtest.AudioRecordTest.onRecord(AudioRecordTest.java:49)
11-26 11:01:38.750: E/AndroidRuntime(23749): at com.example.audiorecordtest.AudioRecordTest.access$0(AudioRecordTest.java:45)
11-26 11:01:38.750: E/AndroidRuntime(23749): at com.example.audiorecordtest.AudioRecordTest$RecordButton$1.onClick(AudioRecordTest.java:124)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.view.View.performClick(View.java:4084)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.view.View$PerformClick.run(View.java:16966)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.os.Handler.handleCallback(Handler.java:615)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.os.Looper.loop(Looper.java:137)
11-26 11:01:38.750: E/AndroidRuntime(23749): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-26 11:01:38.750: E/AndroidRuntime(23749): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 11:01:38.750: E/AndroidRuntime(23749): at java.lang.reflect.Method.invoke(Method.java:511)
11-26 11:01:38.750: E/AndroidRuntime(23749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-26 11:01:38.750: E/AndroidRuntime(23749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-26 11:01:38.750: E/AndroidRuntime(23749): at dalvik.system.NativeStart.main(Native Method)
11-26 11:06:37.210: I/Process(23749): Sending signal. PID: 23749 SIG: 9