碰到的一些问题:
1. permission的问题,由于需要录音,同时需要把录音文件存入sdcard外部存储设备,需要两个权限。
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. 使用模拟器的时候,也许并没有sdcard外部设备,所以需要以下步骤在模拟器上模拟sdcard。
用“mksdcard 125M ./sdcard.img” 命令在当前文件夹下创建一个125m的镜像文件,
用“emulator -avd my_avd -sdcard ./sdcard.img” 命令
启动my_avd模拟器,同时mount sdcard.img镜像。
这样用“adb shell” 进入shell之后再sdcard目录下,可以写东西了。
3. 还有就是在MediaRecorder.setOutputFile(mPath); 如果mPath = "/sdcard/mvideo/a.3pg",不过sdcard文件夹中没有mvideo文件夹,那么就会爆filenotfindexception,所以在这个程序中用的是"/sdcard/a.3pg"路径。
java代码如下:
package test.exampleMediaRecordTest;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MediaRecordTestActivity extends Activity {
MediaRecorder mMediaRecorder;
Button mStartButton;
Button mEndButton;
MediaPlayer mMediaPlayer;
String mPath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaRecorder = new MediaRecorder();
mMediaPlayer = new MediaPlayer();
mPath = Environment.getExternalStorageDirectory().getAbsolutePath();
mPath += "/a.3pg";
mStartButton = (Button) findViewById(R.id.startbutton);
mEndButton = (Button) findViewById(R.id.stopbutton);
mStartButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setOutputFile(mPath);
mMediaRecorder.prepare();
mMediaRecorder.start();
}
catch(Exception e)
{
mMakeTextToast(e.toString(), true);
}
}
});
mEndButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
mMediaRecorder.stop();
mMediaPlayer.setDataSource(mPath);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
catch(Exception e)
{
mMakeTextToast(e.toString(), true);
e.printStackTrace();
}
}
});
}
public void mMakeTextToast(String str, boolean isLong)
{
if(isLong==true)
{
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
}
manifest.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.exampleMediaRecordTest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".MediaRecordTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/startbutton"
android:text = "Record"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/stopbutton"
android:text = "End"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>