Public void startAudioCapture() { val config = AudioPlaybackCaptureConfiguration.Builder(mediaProjection!!) .addMatchingUsage(AudioAttributes.USAGE_MEDIA) // TODO provide UI options for inclusion/exclusion .build() /** * Using hardcoded values for the audio format, Mono PCM samples with a sample rate of 8000Hz * These can be changed according to your application's needs */ val audioFormat = AudioFormat.Builder() .setEncoding(AudioFormat.ENCODING_PCM_FLOAT) .setSampleRate(8000) .setChannelMask(AudioFormat.CHANNEL_IN_MONO) .build() audioRecord = AudioRecord.Builder() .setAudioFormat(audioFormat) // For optimal performance, the buffer size // can be optionally specified to store audio samples. // If the value is not specified, // uses a single frame and lets the // native code figure out the minimum buffer size. .setBufferSizeInBytes(BUFFER_SIZE_IN_BYTES) .setAudioPlaybackCaptureConfig(config) .build() audioRecord!!.startRecording() audioCaptureThread = thread(start = true) { val outputFile = createAudioFile() Log.d(LOG_TAG, "Created file for capture target: ${outputFile.absolutePath}") writeAudioToFile(outputFile) } }