声音录制 [英] Audio Recording

查看:24
本文介绍了声音录制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,我正在尝试构建一个应用程序来记录音频数据.我正在使用带有 ADT 插件的 Eclipse Galileo IDE.我的应用是针对 Andriod 2.1 平台的.

I am a newbie in Android and I was trying to build an app to record audio data. I am using Eclipse Galileo IDE with ADT plugin. And my app is targetted for the Andriod 2.1 platform.

不幸的是,开发指南中提供的示例引发了许多异常.

Unfortunately the example provided in the Dev Guide throws many exceptions.

例如:要获取 MIME 类型,代码使用 recorder.getMimeContentType().但是这个方法在我的 MediaRecorder 类版本中不存在.

For example: to get MIME type the code uses recorder.getMimeContentType(). But this method does not exist in my version of MediaRecorder class.

我已经在网上和这个论坛上进行了搜索,并提出了一两个替代方案,展示了如何录制音频并将其放置在现有文件中.但理想情况下,我希望开发指南中给出的代码能够正常工作.

I have searched online as well as this forum and came up with one or two alternatives which show how to record audio and place it in an EXISTING file. But ideally i want the code given in the dev guide to work.

或者如果我可以录制音频并将其直接存储在字节数组中会更好?

Or even better if i can record the audio and store it directly in a byte array?

我花了很多时间试图让它发挥作用,但收效甚微:(

I have spent a lot of time trying to get this to work but with not much success :(

如果有人能告诉我如何实现录音,我将不胜感激.

I would really appreciate if someone can show me how to achieve audio recording.

推荐答案

它有录制音频的示例代码

<小时>

 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));
}

这篇关于声音录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆