当前位置:操作系统 > 安卓/Android >>

Android MediaRecorder实现暂停断点录音功能

最近研究了下MediaRecorder的录音功能,发现暂停之后,继续录音这个功能,网上参考的资料比较少,现在将自己的学习成果分享大家:

基本原理如下:MediaRecorder通过MIC录音,系统没有自带的pause功能,每次暂停录音,都会结束本次的录音。现在本人的设计思路是:MediaRecorder录音暂停时,保存这段所录下的音频A,继续录音后,再次暂停,保留录音音频B;以此类推直到最终的录音结束时,依次读取之前保存的A、B……的录音文件,并将其合并在一起。涉及的技术:文件的保存读取、音频的合并等

音频的合并:设置MediaRecorder的音频输出格式mMediaRecorder01.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
   mMediaRecorder01 .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);输出的是amr格式。amr的音频文件的文件头,相对来说是固定的6个字节的固定字符,A.amr文件和B.amr文件的合并,只需将B以字节流读取,去掉前6个字节,和A的字节流合并后保存,就实现了音频合并,不涉及复杂的音频编码问题。(MediaRecorder的音频输出格式比较多,有jpgg、MP3等之类的格式,合成的原理大同小异,只需要注意他们的音频文件头的格式就可以了。)

 

 

 

有图有真相:

 

\

\

\

\

 

 

 

 

 

[java]
public class EX07 extends Activity { 
    private ImageButton myButton1; 
    private ImageButton myButton2; 
    private ImageButton myButton3; 
    private ImageButton myButton4; 
    private Button myButton; 
    private ListView myListView1; 
    private String strTempFile = "YYT_"; 
    private File myRecAudioFile; 
    /**录音保存路径**/ 
    private File myRecAudioDir; 
    private File myPlayFile; 
    private MediaRecorder mMediaRecorder01; 
    private int mMinute; 
    private boolean xx=true; 
    /**存放音频文件列表**/ 
    private ArrayList<String> recordFiles; 
    private ArrayAdapter<String> adapter; 
    private TextView myTextView1; 
    /**文件存在**/ 
    private boolean sdcardExit; 
    /**是否停止录音**/ 
    private boolean isStopRecord; 
    /**按钮背景图片的标志位**/ 
    private boolean sigle = false; 
    private String length1 = null; 
     
    private  final String SUFFIX=".amr"; 
     
     
    /**暂停按钮**/ 
    Button buttonpause; 
     
     
    /**记录需要合成的几段amr语音文件**/ 
    private ArrayList<String> list; 
     
     
    int second=0; 
     
    int minute=0; 
     
    /**计时器**/ 
    Timer timer; 
     
     
    /**是否暂停标志位**/ 
    private boolean isPause; 
     
    /**在暂停状态中**/ 
    private boolean inThePause; 
     
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
         
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        //暂停标志位 为false  
        isPause=false; 
        //暂停状态标志位  
        inThePause=false; 
         
        //初始化list  
        list=new ArrayList<String>(); 
         
        //四个按钮  
        myButton1 = (ImageButton) findViewById(R.id.ImageButton01); 
        myButton2 = (ImageButton) findViewById(R.id.ImageButton02); 
        myButton3 = (ImageButton) findViewById(R.id.ImageButton03); 
        myButton4 = (ImageButton) findViewById(R.id.ImageButton04); 
        myButton = (Button) findViewById(R.id.myButton); 
        buttonpause=(Button)findViewById(R.id.mypuase); 
        myListView1 = (ListView) findViewById(R.id.ListView01); 
        myTextView1 = (TextView) findViewById(R.id.TextView01); 
        myButton2.setEnabled(false); 
        myButton3.setEnabled(false); 
        myButton4.setEnabled(false); 
         
        myPlayFile=null; 
 
        // 判断sd Card是否插入  
        sdcardExit = Environment.getExternalStorageState().equals( 
                android.os.Environment.MEDIA_MOUNTED); 
        // 取得sd card路径作为录音文件的位置  
        if (sdcardExit){ 
            String pathStr = Environment.getExternalStorageDirectory().getPath()+"/YYT"; 
            myRecAudioDir= new File(pathStr); 
            if(!myRecAudioDir.exists()){ 
                myRecAudioDir.mkdirs(); 
                Log.v("录音", "创建录音文件!" + myRecAudioDir.exists()); 
&nb

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,