I have a tuner app and using Audio Record object for listening mic. I want to understand it works fine for most devices and android versions (even Android 6) but I got error report from customers on Samsung S7 Phones. I looked at similar posts here to avoid this error and it helped me a lot. But now i cannot understand why only S7 phones gives this error while it s working fine on other Android 6 devices.
P.S. I dont have S7 phone so I couldn't have a chance to try something. I just wonder if anyone having similar issues and what could be the problem with S7 phones. Any ideas?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initAudioRecord(44100, 2048);
.....
}
public void initAudioRecord(int sampleRateInHz, int bufferSizeInBytes) {
AudioRecord audioRecord = AudioRecordInstance(sampleRateInHz, bufferSizeInBytes);
if (audioRecord.getState() != AudioRecord.STATE_UNINITIALIZED) {
if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
audioRecord.stop();
}
audioRecord.release();
audioRecord = AudioRecordInstance(sampleRateInHz, bufferSizeInBytes);
}
audioRecord.startRecording();
}
public AudioRecord AudioRecordInstance(int sampleRateInHz, int bufferSizeInBytes){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
return new AudioRecord.Builder()
.setAudioSource(MediaRecorder.AudioSource.MIC)
.setAudioFormat(new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(sampleRateInHz)
.setChannelMask(AudioFormat.CHANNEL_IN_MONO)
.build())
.setBufferSizeInBytes(2*bufferSizeInBytes)
.build();
}else{
return new AudioRecord(
MediaRecorder.AudioSource.MIC,
sampleRateInHz,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes * 2);
}
}
}
LogCat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tuner/com.tuner.TunerActivity}: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Finally I noticed that it is not crashing only on S7 phones but also some other Android 6.0 devices. And I realized my silly mistake that I forgot to handle new runtime permission design comes up with the marshmallow. Which makes me confused it is working on some other Android 6.0 devices. I guess those are the ones with permissions already granted for my app from device settings.
So I implemented runtime permissions inside my app for Marshmallow and now it is working.