我有一个调谐器应用程序并使用 Audio Record 对象来收听麦克风。我想了解它适用于大多数设备和 Android 版本(甚至是 Android 6),但我收到了三星 S7 手机客户的错误报告。我在这里查看了类似的帖子以避免此错误,这对我有很大帮助。但现在我无法理解为什么只有 S7 手机在其他 Android 6 设备上运行良好时才会出现此错误。
PS我没有S7手机,所以我没有机会尝试一些东西。我只是想知道是否有人有类似的问题以及 S7 手机可能有什么问题。有任何想法吗?
@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);
}
}
}
日志猫:
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)