录音

发布于 2024-10-06 18:42:31 字数 532 浏览 2 评论 0原文

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

不幸的是,开发指南中提供的示例抛出了许多异常。

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

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

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

我花了很多时间试图让它发挥作用,但没有取得多大成功:(

如果有人可以向我展示如何实现音频录制,我将非常感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二手情话 2024-10-13 18:42:31

它有用于录制音频的示例代码


 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 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));
}
停顿的约定 2024-10-13 18:42:31

您可以尝试这段代码,对我来说效果很好:

MediaRecorder recorder;

void startRecording() throws IOException 
{
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
String fileName = "audio_" + timeStampFormat.format(new Date())
+ ".mp4";
recorder = new MediaRecorder();  
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setOutputFile("/sdcard/"+fileName);
recorder.prepare(); 
recorder.start();
}

protected void stopRecording() {
recorder.stop();
recorder.release();
}

You can try this code, for me it works well:

MediaRecorder recorder;

void startRecording() throws IOException 
{
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
String fileName = "audio_" + timeStampFormat.format(new Date())
+ ".mp4";
recorder = new MediaRecorder();  
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setOutputFile("/sdcard/"+fileName);
recorder.prepare(); 
recorder.start();
}

protected void stopRecording() {
recorder.stop();
recorder.release();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文