java – Record and Play the same sound simultaneously on Android-ThrowExceptions

Exception or error:

I am working on App for Android devices which will play currently recorded sounds. So far I have this piece of code

        val startButton: Button = findViewById(R.id.startButton)
        val stopButton: Button = findViewById(R.id.stopButton)
        val playButton: Button = findViewById(R.id.playButton)
        stopButton.isEnabled = false
        playButton.isEnabled = false

        outputFile = Environment.getExternalStorageDirectory().absolutePath + "/recording.aac"

        val file = File(outputFile)

        audioRecorder = MediaRecorder()


        audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC)
        audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
        audioRecorder.setAudioEncodingBitRate(128000)
        audioRecorder.setAudioSamplingRate(44100)
        audioRecorder.setOutputFile(file)


        startButton.setOnClickListener(View.OnClickListener {

            audioRecorder.prepare()
            audioRecorder.start()
            startButton.isEnabled = false
            stopButton.isEnabled = true

        })

        stopButton.setOnClickListener(View.OnClickListener {

            audioRecorder.stop()
            audioRecorder.release()
            stopButton.isEnabled = false
            playButton.isEnabled = true
            startButton.isEnabled = true

        })

        playButton.setOnClickListener(View.OnClickListener {

            var mediaPlayer = MediaPlayer()

            mediaPlayer.setDataSource(outputFile)
            mediaPlayer.prepare()
            mediaPlayer.start()


        })

I was thinking of making a thread or asynctask, but I think there is a problem with the fact that the file I want to play during the recording is being used or does not exist yet (Correct me if I am wrong).

Is there any simple solution for the “Recording and playing the same sound simultaneously”?

App should work like a MIC

EDIT: I want to capture recording into a some kind of variable, manipulate with the sound(polarity adjusting) and play it right away simultaneously

Thanks

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *