我是 Android 新手,我正在尝试构建一个应用程序来记录音频数据。我正在使用带有 ADT 插件的 Eclipse Galileo IDE。我的应用程序针对的是 Andriod 2.1 平台。
不幸的是example开发指南中提供的抛出许多异常。
例如:
获取代码使用的 MIME 类型recorder.getMimeContentType()
。但我的版本中不存在这个方法MediaRecorder
class.
我在网上和这个论坛上进行了搜索,并提出了一两个替代方案,展示了如何录制音频并将其放入现有文件中。但理想情况下,我希望开发指南中给出的代码能够工作。
或者如果我可以录制音频并将其直接存储在字节数组中,那就更好了?
我花了很多时间试图让它发挥作用,但没有取得多大成功:(
如果有人可以向我展示如何实现录音,我将非常感激。
它有用于录制音频的示例代码
b1=(Button)findViewById(R.id.button1);
b2=(Button) findViewById(R.id.button2);
mr=new MediaRecorder();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
b1.setEnabled(false);
b2.setEnabled(true);
b2.requestFocus();
start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try{
b1.setEnabled(true);
b2.setEnabled(false);
b1.requestFocus();
stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
processaudiofile();
}
});
b2.setEnabled(false);
b1.setEnabled(true);
}
protected void start() throws Exception
{
mr.setAudioSource(MediaRecorder.AudioSource.MIC);
mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (audiofile == null) {
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("Record", ".mp3", sampleDir);
}
catch (IOException e)
{
Log.e("abc","sdcard access error");
return;
}
}
mr.setOutputFile(audiofile.getAbsolutePath());
mr.prepare();
mr.start();
}
protected void stop() throws Exception{
mr.stop();
mr.release();
}
protected void processaudiofile() {
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);
// this does not always seem to work cleanly....
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)