Facebook
From Bilal Tahir, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 361
  1. Public void startAudioCapture()
  2.  
  3. {
  4.  
  5.     val config = AudioPlaybackCaptureConfiguration.Builder(mediaProjection!!)
  6.  
  7.             .addMatchingUsage(AudioAttributes.USAGE_MEDIA) // TODO provide UI options for inclusion/exclusion
  8.  
  9.             .build()
  10.  
  11.  
  12.         /**
  13.  
  14.          * Using hardcoded values for the audio format, Mono PCM samples with a sample rate of 8000Hz
  15.  
  16.          * These can be changed according to your application's needs
  17.  
  18.          */
  19.  
  20.         val audioFormat = AudioFormat.Builder()
  21.  
  22.             .setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
  23.  
  24.             .setSampleRate(8000)
  25.  
  26.             .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
  27.  
  28.             .build()
  29.  
  30.  
  31.         audioRecord = AudioRecord.Builder()
  32.  
  33.             .setAudioFormat(audioFormat)
  34.  
  35.             // For optimal performance, the buffer size
  36.  
  37.             // can be optionally specified to store audio samples.
  38.  
  39.             // If the value is not specified,
  40.  
  41.             // uses a single frame and lets the
  42.  
  43.             // native code figure out the minimum buffer size.
  44.  
  45.             .setBufferSizeInBytes(BUFFER_SIZE_IN_BYTES)
  46.  
  47.             .setAudioPlaybackCaptureConfig(config)
  48.  
  49.             .build()
  50.  
  51.  
  52.         audioRecord!!.startRecording()
  53.  
  54.         audioCaptureThread = thread(start = true) {
  55.  
  56.             val outputFile = createAudioFile()
  57.  
  58.             Log.d(LOG_TAG, "Created file for capture target: ${outputFile.absolutePath}")
  59.  
  60.             writeAudioToFile(outputFile)
  61.  
  62.         }
  63.  
  64. }
  65.  
  66.