MediaRecorder IOException: prepare failed

后端 未结 6 423
北恋
北恋 2021-01-18 08:11

I want to use MediaRecorder to record voice, my code is:

 public void record(View v) {
       Log.d(TAG, \"record\");

    this.mediaRecorder.setAudioChannel         


        
6条回答
  •  花落未央
    2021-01-18 08:54

    Actually iam also faced the same problem in my application building time, and i got a perfect solution is that we should set all permissions like RECORD_AUDIO,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE, we should set these permissions AndroidManifest.xml and runtime also.i just pasting my code below, its kotlin code and running success

        //Here when the app opening time 
        
    
        class MainActivity: AppCompatActivity() {
    
            companion object{
                var permissionAccepted = false
                var permissions = arrayOf(android.Manifest.permission.RECORD_AUDIO,
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            }
    
    
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
    
                ActivityCompat.requestPermissions(this, permissions,CODE_PERMISSIONS)
    
                setContentView(R.layout.activity_main)
            }
    
            @RequiresApi(Build.VERSION_CODES.M)
            override fun onRequestPermissionsResult(
                requestCode: Int,
                permissions: Array,
                grantResults: IntArray
            ) {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults)
                when (requestCode) {
                    REQUEST_CODE_PERMISSIONS -> {
                        permissionAccepted = grantResults[0]
                                                    == PackageManager.PERMISSION_GRANTED                                     
                                && grantResults [1] == PackageManager.PERMISSION_GRANTED
                                && grantResults[2] == PackageManager.PERMISSION_GRANTED
                    }
                }
                if (!permissionAccepted){
                    requestPermissions(permissions, REQUEST_CODE_PERMISSIONS)
                }
            }
    
    
        }
    
        
    
        class VoiceRecordActivity: AppCompatActivity() {
    
            private var mediaRecorder: MediaRecorder? = null
    
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_voice_record)
    
                //i used touch listner
                audio_message_send.setOnTouchListener{ v, event ->
                        if (permissionAccepted)
                        {
                            //Log.e("TAG","permission accepted:$permissionAccepted")
                            if (event.action == MotionEvent.ACTION_DOWN) {
                                startRecording()
    
                            }
                            if (event.action == MotionEvent.ACTION_UP) {
                                stopRecording()
                            }
                        }else{
                            //Log.e("TAG","permission accepted:$permissionAccepted")
                            requestPermissions(permissions, REQUEST_CODE_PERMISSIONS)
                        }
                        false
                    }
            }
    
                private fun startRecording() {
                           mFile = Environment.getExternalStorageDirectory().absolutePath                       
                        +"/$formattedDate-FC00$random.aac"
                    if (mediaRecorder == null){
                    this.mediaRecorder = MediaRecorder().apply {
                        setOutputFile(mFile)
                        setAudioSource(MediaRecorder.AudioSource.MIC)
                        setAudioChannels(1)
                        setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS)
                        setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
    
                        //Log.e("TAG", "mFIle: $mFile")
    
                        try {
                            this.prepare()
                            this.start()
    
                        } catch (e: IOException) {
                            e.printStackTrace()
                            Log.e("TAG", "error: Prepare() failed")
                        } catch (e: InterruptedException) {
                            e.printStackTrace()
                        } catch (e: InterruptedException) {
                            e.printStackTrace()
                        }
                    }
                }else{
                    this.mediaRecorder!!.prepare()
                    this.mediaRecorder!!.start()
                }
            }
    
        private fun stopRecording() {
            mediaRecorder!!.apply {
                try {
                    stop()
                    release()
                }catch (e: IOException){
                    Log.e("TAG","error: stop${e.printStackTrace()}")
                }
                mediaRecorder = null
            }
        }
    
        override fun onStop() {
                super.onStop()
                if(mediaRecorder != null) {
                    mediaRecorder!!.release()
                    mediaRecorder = null
                }
            }
    }
    

提交回复
热议问题