我是靠谱客的博主 冷艳糖豆,最近开发中收集的这篇文章主要介绍Android录制声音(一)Recorder,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

再看Android SDK的录制声音的例子时看见文章比较短就翻译了一下,请大家提出宝贵意见:

 

Android SDK docsguidetopicsmediaaudio-capture.html 的翻译

音频录制

Android多媒体框架支持对常见音频的录制和编码,如果硬件支持,可以使用 MediaRecorder APIs 来非常方便的编写音频程序。

通过学习这篇文档,你可编写应用程序通过设备的麦克来录制声音,并且保持和播放声音。

  注意:Android模拟器不支持录制声音的功能,必须有实际设备。

 

运行录制声音程序

   音频的录制要比音频或者视频的播放要复杂,但是相对也比较简单,课遵循以下步骤:

1.      创建MediaRecorder实例: android.media.MediaRecorder

2.      设置音频来源:MediaRecorder.setAudioSource();

3.      设置输出音频格式:MediaRecorder.setOutputFormat();

4.      设置输出文件名:MediaRecorder.setOutputFile();

5.      设置输出编码格式:MediaRecorder.setAudioEncorder();

6.      在开始录制之前调用MediaRecorder.prepare();

7.      开始录制声音,调用:MediaRecorder.start();

8.      停止录制声音,调用:MediaRecorder.stop();

9.      当录制完声音的时候,调用MediaRecorder.release()来释放占用的相关资源

 

例子:录制音频并播放

下面的例子展示了怎样录制并播放声音

这个APP需要添加两个权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.RECORD_AUDIO" />

/*
* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to record audio. These permissions must be set in the
* application's AndroidManifest.xml file, with something like:
*
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.RECORD_AUDIO" />
*
*/

package com.android.audiorecordtest;

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;


public classAudioRecordTestextendsActivity
{
   
private staticfinalString LOG_TAG="AudioRecordTest";
   
private staticString mFileName=null;

   
private RecordButton mRecordButton=null;
   
private MediaRecorder mRecorder=null;

   
private PlayButton   mPlayButton=null;
   
private MediaPlayer   mPlayer=null;

   
private void onRecord(boolean start){
       
if (start){
            startRecording
();
       
} else{
            stopRecording
();
       
}
   
}

   
private void onPlay(boolean start){
       
if (start){
            startPlaying
();
       
} else{
            stopPlaying
();
       
}
   
}

   
private void startPlaying(){
        mPlayer
= newMediaPlayer();
       
try {
            mPlayer
.setDataSource(mFileName);
            mPlayer
.prepare();
            mPlayer
.start();
       
} catch(IOException e){
           
Log.e(LOG_TAG,"prepare() failed");
       
}
   
}

   
private void stopPlaying(){
        mPlayer
.release();
        mPlayer
= null;
   
}

   
private void startRecording(){
        mRecorder
= newMediaRecorder();
        mRecorder
.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder
.setOutputFile(mFileName);
        mRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

       
try {
            mRecorder
.prepare();
       
} catch(IOException e){
           
Log.e(LOG_TAG,"prepare() failed");
       
}

        mRecorder
.start();
   
}

   
private void stopRecording(){
        mRecorder
.stop();
        mRecorder
.release();
        mRecorder
= null;
   
}

   
class RecordButtonextendsButton{
       
boolean mStartRecording=true;

       
OnClickListener clicker=newOnClickListener(){
           
public void onClick(View v){
                onRecord
(mStartRecording);
               
if (mStartRecording){
                    setText
("Stop recording");
               
} else{
                    setText
("Start recording");
               
}
                mStartRecording
=!mStartRecording;
           
}
       
};

       
public RecordButton(Context ctx){
           
super(ctx);
            setText
("Start recording");
            setOnClickListener
(clicker);
       
}
   
}

   
class PlayButtonextendsButton{
       
boolean mStartPlaying = true;

       
OnClickListener clicker=newOnClickListener(){
           
public void onClick(View v){
                onPlay
(mStartPlaying);
               
if (mStartPlaying){
                    setText
("Stop playing");
               
} else{
                    setText
("Start playing");
               
}
                mStartPlaying
=!mStartPlaying;
           
}
       
};

       
public PlayButton(Context ctx){
           
super(ctx);
            setText
("Start playing");
            setOnClickListener
(clicker);
       
}
   
}

   
public AudioRecordTest(){
        mFileName
= Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName
+= "/audiorecordtest.3gp";
   
}

   
@Override
   
public void onCreate(Bundle icicle){
       
super.onCreate(icicle);

       
LinearLayout ll = newLinearLayout(this);
        mRecordButton
= new RecordButton(this);
        ll
.addView(mRecordButton,
           
new LinearLayout.LayoutParams(
               
ViewGroup.LayoutParams.WRAP_CONTENT,
               
ViewGroup.LayoutParams.WRAP_CONTENT,
               
0));
        mPlayButton
= newPlayButton(this);
        ll
.addView(mPlayButton,
           
new LinearLayout.LayoutParams(
               
ViewGroup.LayoutParams.WRAP_CONTENT,
               
ViewGroup.LayoutParams.WRAP_CONTENT,
               
0));
        setContentView
(ll);
   
}

   
@Override
   
public void onPause(){
       
super.onPause();
       
if (mRecorder!=null){
            mRecorder
.release();
            mRecorder
= null;
       
}

       
if (mPlayer!=null){
            mPlayer
.release();
            mPlayer
= null;
       
}
   
}
}

 

这是SDK的中带的代码,我已经在自己的手机上成功运行没有任何错误!

最后

以上就是冷艳糖豆为你收集整理的Android录制声音(一)Recorder的全部内容,希望文章能够帮你解决Android录制声音(一)Recorder所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部