android MediaRecorder实现录音功能 显示时间 计时

版权声明:转载请注明 谢谢 https://blog.csdn.net/bigsungod/article/details/70175599

初始化:


        if (recorder==null){
            recorder=new MediaRecorder();//初始化录音对象
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置录音的输入源(麦克)
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置音频格式(amr)
            createRecorderFile();//创建保存录音的文件夹

            recorder.setOutputFile("sdcard/recorder" + "/" + getCurrentTime() + ".AAC"); //设置录音保存的文件
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//设置音频编码
            try {
                recorder.prepare();//准备录音
            } catch (IOException e) {
                e.printStackTrace();
            }
        }





录音并计时:

    private void StartRecord(){
        if (recorder!=null){
            recorder.start(); //开始录音
            Count = 0;
            final Handler handler=new Handler();
            final Runnable runnable=new Runnable(){
                @Override
                public void run() {
                    if(Count==1800){//限时半小时
                        recorder.stop();
                    }
                    Count++;
                    time.setText(str);刷新time视图显示计时
   String str=showTimeCount((long)Count)+"/30:00"; 
handler.postDelayed(this, 1000);//每一秒刷新一次 }
 };
 runnable.run(); } }







停止录音:

 private void StopRecord(){
            if (recorder!=null){
                recorder.stop(); //停止录音
                recorder.release();//释放资源
                recorder = null;
           }
    }









其他:

//创建保存录音的目录
    private void createRecorderFile() {
        String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();

        String filePath=absolutePath+"/recorder";
        File file=new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }
    }
    //获取当前时间,以其为名来保存录音
    private String getCurrentTime(){
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        Date date=new Date(System.currentTimeMillis());
        String str=format.format(date);
        return str;

    }
    //将秒数转换成时间显示格式
    public String showTimeCount(long time) {
        String s = null;
        if(time<=59){
            s ="00:";
            return time<10 ? s+"0"+String.valueOf(time) : s+String.valueOf(time);
        }else{
            return (time%60 <10 ? s+"0"+String.valueOf(time) : s+String.valueOf(time))+":"+(time/60<10 ? s+"0"+String.valueOf(time) : s+String.valueOf(time));
        }
    }

}









目前遇到过的问题:

java.lang.IllegalStateException


原因:

1、当在本地录音资源已经开启的情况下(比如直播间录音、电话中途录音)再次实现录音时会因为硬件资源被占用

解决方案:找到该方法,释放正在使用的资源

2、没有开启权限

解决方案:

AndroidMainfest.xml添加

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





还有一个是关于recorder.stop()的
问题描述:java.lang.RuntimeException: stop failed.
解决方案:

        try {  
                //加上下面三个参数, 
                //报错为:RuntimeException:stop failed  
                recorder.setOnErrorListener(null);  
                recorder.setOnInfoListener(null);    
                recorder.setPreviewDisplay(null);  
                recorder.stop();  
            } catch (IllegalStateException e) {  
                // TODO: handle exception  
                Log.i("Exception", Log.getStackTraceString(e));  
            }catch (RuntimeException e) {  
                // TODO: handle exception  
                Log.i("Exception", Log.getStackTraceString(e));  
            }catch (Exception e) {  
                // TODO: handle exception  
                Log.i("Exception", Log.getStackTraceString(e));  
            }  








 
 
 
 
 
 
 

猜你喜欢

转载自blog.csdn.net/bigsungod/article/details/70175599