Is it possible to change the sampling rate while recording an Audio in Android using AudioRecord or MediaRecorder?
Both of these class requires to initialize first the sampling rates before recording an Audio, But I was wondering if I can change the sampling rate, let's say 8000 to 16000 and vis-a-vis, in the middle of recording.
What would you expect to happen when you change the sampling rate once it is recording? Setting the rate directly is not supported by AudioRecord, so that is a definite no.
Setting the rate directly with MediaRecorder is allowed, but is expected to be done before starting the recording. I would not expect all, if any, implementations of the Android OS to handle this.
Related
I am using MediaRecorder to record videos and want to change certain parameters like videoBitrate frameRate like this
profile.videoBitRate = 50000000;
But from Android Docs
MediaRecorder error if the output bit rate exceeds the encoder limit.
So how do I query about the supported bitrates and framerate? LIke above I have hardcoded the bitrate to 50mbps but it may exceed encoder limit on some devices.
I checked MediaRecorder docs and couldn't find any reference to this.
Is there a way I can get the supported bitrates and framerates for video recording using media recorder.
I have an app calling using WebRTC. But during a call, I need to record microphone. WebRTC has an object WebRTCAudioRecord to record audio but the audio file is so large (PCM_16bit). I want to record but to a smaller size.
I've tried MediaRecorder but it doesn't work because WebRTC is recorded and MediaRecorder does not have permission to record while calling.
Has anyone done this, or have any idea that could help me?
Webrtc is considered as comparatively much better pre-processing tool for Audio and Video.
Webrtc native development includes fully optimized native C and C++ classes, In order to maintain wonderful Speech Quality and Intelligibility of audio and video which is quite interesting.
Visit Reference Link: https://github.com/jitsi/webrtc/tree/master/examples regularly.
As Problem states;
I want to record but smaller size. I've tried MediaRecorder and it doesn't work because WebRtc is recorded and MediaRecorder has not permission to record while calling.
First of all, to reduce or minimize the size of your recorded data (audio bytes), you should look at different types of speech codecs which basically reduce the size of recorded data by maintaining sound quality at a level. To see different voice codecs, here are well-known speech codecs as follows:
OPUS
SPEEX
G7.11 (G-Series Speech Codecs)
As far as size of the audio data is concerned, it basically depends upon the Sample Rate and Time for which you record a chunk or audio packet.
Supppose time = 40ms ---then---> Reocrded Data = 640 bytes (or 320 short)
Size of recorded data is **directly proportional** to both Time and Sample rate.
Sample Rate = 8000 or 16000 etc. (greater the sample rate, greater would be the size)
To see in more detail visit: fundamentals of audio data representation. But Webrtc mainly process 10ms audio data for pre-processing in which packet size is reduced up to 160 bytes.
Secondly, If you want to use multiple AudioRecorder instances at a time, then it is practically impossible. As WebRtc is already recording from microphone then practically MediaRecorder instance would not perform any function as this answer depicts audio-record-multiple-audio-at-a-time. Webrtc has following methods to manage audio bytes such as;
1. Push input PCM data into `ProcessCaptureStream` to process in place.
2. Get the processed PCM data from `ProcessCaptureStream` and send to far-end.
3. The far end pushed the received data into `ProcessRenderStream`.
I have maintained a complete tutorial related to audio processing using Webrtc, you can visit to see more details; Android-Audio-Processing-Using-Webrtc.
There are two parts for the solution:
Get the raw PCM audio frames from webrtc
Save them to a local file in compressed size so that it can be played out later
For the first part you have to attach the SamplesReadyCallback while creating audioDeviceManager by calling the setSamplesReadyCallback method of JavaAudioDeviceModule. This callback will give you the raw audio frames captured by webrtc's AudioRecord from the mic.
For the second part you have to encode the raw frames and write into a file. Check out this sample from google on how to do it - https://android.googlesource.com/platform/frameworks/base/+/master/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenInternalAudioRecorder.java#234
Is Microphone sensitivity and sound measure for loudspeaker adjustable in android? If it can be , how can i do that (with which classes and methods)?
And how can i stimulate a phone call and send message when a little sound comes to phone?
For API 16 you can use AutomaticGainControl, the AudioEffect and the AudioRecord to adjust gain, sampling and other pre-processing audio recording features.
i want to know what is the difference between setting audio sample rate in Android AudioRecord class and Media record class? In audio record class we set sample rate while creating an object of the class like
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
SampleRateInHz, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
while in MediaRecorder class we set explicitly through a function call. i.e.
mrec.setAudioSamplingRate(samplingRate);
I tried both but effect of sample rate can only be seen while audio recording not while recording throug mediaRecorder class. I can not understand what is the differnce between both.
AS of android sdk documentation, MediaRecord is used to record audio and video. The recording control is based on a simple state machine. You will always use MediaRecord to record sounds unless you need to access the raw audio data and process them(eg, apply your own DSP effects).In this scenario you will use AudioRecord.setAudioSamplingRateof MediaRecord does the same as passing SampleRateInHz to the AudioRecord, setting the desired sampling rate.The higher the sampling rate the better sound quality and frequency range you will get.An ideal uman ear can hear sound from 20 to 20000 hz.If you set your sampling rate to 4100hz then your frequency response will equal to 4100/2 which is almost 20000hz.However the supported freq range by devices are different. you should always check if the device supports your desired frequency.
In order to check if a specific freq works, you may use the following piece of code:
int bufferSize = AudioRecord.getMinBufferSize(rate[i],AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
if bufferSize value is bigger than 0 then you the requency you provided is supported by device.
I am wondering if there is a way to change the sampling rate at which MediaPlayer plays data back. I'd like to tweak it and play data back at a slower rate than encoded. Thoughts?
This might not be doable with MediaPlayer, but for audio you can do it with SoundPool:
The playback rate can also be changed. A playback rate of 1.0 causes
the sound to play at its original frequency (resampled, if necessary,
to the hardware output frequency). A playback rate of 2.0 causes the
sound to play at twice its original frequency, and a playback rate of
0.5 causes it to play at half its original frequency. The playback rate range is 0.5 to 2.0.
You didn't mention this in your original question, but if you want to maintain pitch while changing the frequency, you'd need to provide a pitch-shift algorithm. Perhaps there's one in the android.media.audiofx package - there's a queryEffects() method that might return some type of pitch shifter.
For MediaPlayer, you might look at the attachAuxEffect method that would let you process the audio stream.