通过 MediaRecorder 录制音频
Posted
技术标签:
【中文标题】通过 MediaRecorder 录制音频【英文标题】:Record audio via MediaRecorder 【发布时间】:2012-06-12 22:35:39 【问题描述】:我正在尝试,但出现错误,我尝试更改所有内容,但没有任何效果。最近两个小时我试图找到错误,我也使用了 Log 类,我发现它在调用 recorder.start() 方法时发生了错误。可能是什么问题?
public class AudioRecorderActivity extends Activity
MediaRecorder recorder;
File audioFile = null;
private static final String TAG = "AudioRecorderActivity";
private View startButton;
private View stopButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
startButton = findViewById(R.id.start);
stopButton = findViewById(R.id.stop);
setContentView(R.layout.main);
public void startRecording(View view) throws IOException
startButton.setEnabled(false);
stopButton.setEnabled(true);
File sampleDir = Environment.getExternalStorageDirectory();
try
audioFile = File.createTempFile("sound", ".3gp", sampleDir);
catch(IOException e)
Toast.makeText(getApplicationContext(), "SD Card Access Error", Toast.LENGTH_LONG).show();
Log.e(TAG, "Sdcard access error");
return;
recorder = new MediaRecorder();
recorder.setAudiosource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(audioFile.getAbsolutePath());
recorder.prepare();
recorder.start();
public void stopRecording(View view)
startButton.setEnabled(true);
stopButton.setEnabled(false);
recorder.stop();
recorder.release();
addRecordingToMediaLibrary();
protected void addRecordingToMediaLibrary()
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audioFile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int)(current/1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath());
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();
这是 xml 布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_
android:layout_
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:onClick="startRecording"
android:text="Start Recording" />
<Button
android:id="@+id/stop"
android:layout_
android:layout_
android:layout_alignLeft="@+id/start"
android:layout_below="@+id/start"
android:layout_marginTop="41dp"
android:enabled="false"
android:onClick="stopRecording"
android:text="Stop Recording" />
</RelativeLayout>
我添加了对 AndroidManifest 文件的权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.isuru.audiorecorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AudioRecorderActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>
这是我得到的错误,
06-11 18:27:44.548: E/AndroidRuntime(765): java.lang.IllegalStateException: Could not execute method of the activity
06-11 18:27:44.548: E/AndroidRuntime(765): at android.view.View$1.onClick(View.java:2072)
我需要录制高质量的音频。
谢谢!
【问题讨论】:
你能多发些日志猫错误 你在findViewById()
之后打电话给setContentView()
。之前调用它,因为你的视图还没有膨胀。
【参考方案1】:
我没有花时间去寻找你的代码,但是这段代码可以工作:
public class ExampleActivity extends Activity implements OnClickListener
private Button startButton;
private Button stopButton;
private MediaRecorder mediaRecorder;
private File audioFile;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
startButton = (Button) findViewById(R.id.button1);
startButton.setOnClickListener(this);
startButton.setText("start");
stopButton = (Button) findViewById(R.id.button2);
stopButton.setOnClickListener(this);
stopButton.setEnabled(false);
stopButton.setText("stop");
audioFile = new File(Environment.getExternalStorageDirectory(),
"audio_test4.3gp");
// this process must be done prior to the start of recording
private void resetRecorder()
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioEncodingBitRate(16);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setOutputFile(audioFile.getAbsolutePath());
try
mediaRecorder.prepare();
catch (IllegalStateException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
@Override
public void onClick(View v)
switch (v.getId())
case R.id.button1:
mediaRecorder = new MediaRecorder();
resetRecorder();
mediaRecorder.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
break;
case R.id.button2:
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
startButton.setEnabled(true);
stopButton.setEnabled(false);
break;
@Override
protected void onPause()
super.onPause();
if (mediaRecorder != null)
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
【讨论】:
如果你要投反对票,至少有礼貌地说出原因。否则,谁能从错误中吸取教训?以上是关于通过 MediaRecorder 录制音频的主要内容,如果未能解决你的问题,请参考以下文章
Android MediaRecorder 录制视频出现的问题及解决
Android-MediaRecorder-音频录制-警告-W/MediaRecorder(13811): mediarecorder went away with unhandled events(