录音 [英] Audio Recording

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

问题描述

我在Android的一个新手,我试图建立一个应用程序来记录音频数据。我使用的Eclipse IDE伽利略用ADT插件。而我的应用程序针对性的的Andr​​iod 2.1系统平台。

不幸的是,例如中的开发手册中引发许多例外。

例如: 获取MIME类型code使用 recorder.getMimeContentType()。但这种方法并不在我的版本 MediaRecorder的类存在。

我已经在网上搜索,以及这个论坛,并提出了展示了如何录制音频,并将其放置在现有文件中的一个或两个备选方案。但是,理想我想在开发手册中的code工作。

甚至更好,如果我可以录制音频,并直接存储在一个字节数组?

我花了很多时间试图得到这个工作,但不是很成功:(

我真的AP preciate如果有人能告诉我如何实现录音。

解决方案

它例如code录制音频


  B1 =(按钮)findViewById(R.id.button1);
    B2 =(按钮)findViewById(R.id.button2);
    MR =新MediaRecorder();
    b1.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            尝试{
            b1.setEnabled(假);
            b2.setEnabled(真正的);
            b2.requestFocus();

                开始();
            }赶上(例外五){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
        }
    });
    b2.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            尝试{
            b1.setEnabled(真正的);
            b2.setEnabled(假);
            b1.requestFocus();

                停止();
            }赶上(例外五){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
            processaudiofile();


        }
    });

    b2.setEnabled(假);
    b1.setEnabled(真正的);
 }


  保护无效的start()抛出异常
{
   mr.setAudioSource(MediaRecorder.AudioSource.MIC);
   mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
   mr.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
   如果(audiofile的== NULL){
          文件sampleDir = Environment.getExternalStorageDirectory();

          尝试 {
              的AudioFile = File.createTempFile(记录,MP3,sampleDir);
          }
          赶上(IOException异常E)
          {
              Log.e(ABC,SD卡访问错误);
              返回;
          }
  }

     mr.setOutputFile(audiofile.getAbsolutePath());

   先生prepare()。
    mr.start();


}


 保护无效的stop()抛出异常{
mr.stop();
mr.release();
}



保护无效processaudiofile(){
    ContentValues​​值=新ContentValues​​(4);
    长电流= System.currentTimeMillis的();

    values​​.put(MediaStore.Audio.Media.TITLE,音频+ audiofile.getName());
    values​​.put(MediaStore.Audio.Media.DATE_ADDED,(int)的(电流/ 1000));
    values​​.put(MediaStore.Audio.Media.MIME_TYPE,音频/ 3GPP);
    values​​.put(MediaStore.Audio.Media.DATA,audiofile.getAbsolutePath());
    ContentResolver的ContentResolver的= getContentResolver();

    乌里基地= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    乌里newUri = contentResolver.insert(基地,价值观);

    //这并不总是似乎工作干净....
    sendBroadcast(新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,newUri));
}
 

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.

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.

解决方案

It has example code for recording an audio


 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天全站免登陆