I write a Android plugin for Unity3d which can record sound produced by the plugin's client (Unity3d game).
Android has AudioRecord API which I assume can accomplish the task. The problem, however, is that
Applications creating an AudioRecord instance need Manifest.permission.RECORD_AUDIO or the Builder will throw UnsupportedOperationException on build(), and the constructor will return an instance in state STATE_UNINITIALIZED.
Clearly, I don't want the plugin to add permission requirements to its client (unity game). So, I'm looking for another method to capture sound produced by my own application (in that case, it's unity3d game).
CodePudding user response:
There are a few other methods you can use to capture sound produced by your own application on Android:
Use the MediaRecorder class to record audio. The MediaRecorder class allows you to record audio from different audio sources, such as the microphone or the audio output of your application. You can use the setAudioSource method to set the audio source to be used for recording, and the setOutputFormat and setOutputFile methods to set the output format and file for the recorded audio.
Use the AudioTrack class to play and record audio. The AudioTrack class can be used to both play and record audio by using the MODE_STREAM mode. You can use the write method to write audio data to the AudioTrack instance, and the read method to read audio data from the AudioTrack instance.
Use the AudioManager class to redirect audio to a specific output. The AudioManager class allows you to redirect audio output to different audio devices. You can use the setRouting method to redirect audio to a specific audio output, such as a speaker or a Bluetooth device.
Keep in mind that, in all the cases, it is not necessary to request the RECORD_AUDIO permission.
Please note that, depending on the use case, the sound quality of the audio captured may not be ideal.