美文网首页
录音工具类

录音工具类

作者: 不略 | 来源:发表于2022-09-14 20:56 被阅读0次
<uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  /**
     * 权限申请
     */
    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] permissions = new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE};
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, permissions, 200);
                    return;
                }
            }
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requestCode == 200) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivityForResult(intent, 200);
                    return;
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 200) {
            checkPermission();
        }
    }
private MediaRecorder recorder;
private void startRecord(){
        Log.i("TAG","录音开始==================>");
        if (recorder==null){
//            File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"sound");
            File dir = getExternalFilesDir(Environment.DIRECTORY_MUSIC +"/sound");
            Log.i("TAG","dir================>"+dir);
            if (!dir.exists()){
                dir.mkdirs();

            }
            if(file == null){
                file=new File(dir+"/"+String.valueOf(System.currentTimeMillis())+".mp3");
            }

            Log.w("TAG","file === " + file.getPath());
            if (!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            recorder =new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//输入源通过话筒录音;
            recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//输出格式
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);//音频编码
            recorder.setOutputFile(file.getAbsolutePath());//设置写出文件;
            Log.i("TAG","getAbsolutePath===================>>>>"+file.getAbsolutePath());
            try {
                recorder.prepare();
                recorder.start();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
private void endRecord(){
        try {
            if (recorder!=null){
                recorder.stop();
                recorder.release();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            recorder = null;
            try {
                String base64 = FileUtils.encoderBase64File(file);
                Log.w("TAG","base64 ==== " + base64);
                boolean b = FileUtils.base64ToFile(base64, file.getPath());
                Log.w("TAG","================>>>>>>"+b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

相关文章

网友评论

      本文标题:录音工具类

      本文链接:https://www.haomeiwen.com/subject/dxplortx.html