Reputation: 21
Hello ladies and gentleman, could I please ask for little assistance here? I'm not really good with Android development and trying to create Capacitor plugin that streams audio recorded from mic over socket.io. All works fine it seems except time to time I see crash report which states:
releaseBuffer: mUnreleased out of range, !(stepCount:480 <= mUnreleased:128 <= mFrameCount:1440), BufferSizeInFrames:1440
I do understand the message, but having a hard time to figure out what to do with it, any help would be much appreciated.
The function that generates this error is below:
@PluginMethod()
public void startRecording(PluginCall call) {
try {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.RECORD_AUDIO}, AUDIO_RECORD_REQUEST_CODE);
call.reject("Permission is required for recording audio.");
return;
}
vibratePhone();
sendUniqueId = generateRandomString(10);
JSObject ret = new JSObject();
ret.put("companyId", companyId);
ret.put("name", name);
ret.put("userId", userId);
ret.put("uid", sendUniqueId);
socket.emit("speaking", ret);
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufferSize = AudioRecord.getMinBufferSize(pcmSampleRate, channelConfig, audioFormat);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, pcmSampleRate, channelConfig, audioFormat, minBufferSize);
if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
call.reject("Failed to initialize audio recording.");
return;
}
new Thread(() -> {
audioRecord.startRecording();
isRecording = true;
byte[] buffer = new byte[minBufferSize];
int chunksToSkip = 6; // Number of initial chunks to skip
int chunksProcessed = 0; // Counter for processed chunks
long startTime = System.currentTimeMillis();
long totalDataSent = 0;
while (isRecording && audioRecord != null && audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
int bytesRead = audioRecord.read(buffer, 0, buffer.length);
if (bytesRead > 0) {
chunksProcessed++; // Increment the chunks processed counter
if (chunksProcessed > chunksToSkip) {
byte[] actualData = Arrays.copyOf(buffer, bytesRead);
try {
if (actualData.length > 0) {
Constants.FrameSize frameSize2 = Constants.FrameSize.Companion.fromValue(buffer.length / 2);
byte[] encoded = codec.encode(actualData, frameSize2);
totalDataSent += encoded.length;
if (encoded != null) {
JSObject ret2 = new JSObject();
ret2.put("data", encoded);
ret2.put("frameSize", buffer.length / 2);
ret2.put("companyId", companyId);
ret2.put("name", name);
ret2.put("userId", userId);
socket.emit("send-chat-data", ret2);
} else {
JSObject errorLog = new JSObject();
errorLog.put("error", "CODEC.Encoded is null");
errorLog.put("type", "startRecording");
socket.emit("ptt-error-logging", errorLog);
}
long currentTime = System.currentTimeMillis();
if ((currentTime - startTime) >= 60000) { // 60,000 milliseconds = 1 minute
Log.d("DataSent", "Total data sent in last minute: " + (totalDataSent / 1024) + " KB");
// Reset counters
startTime = currentTime;
totalDataSent = 0;
}
}
} catch (Exception e) {
JSObject errorLog = new JSObject();
errorLog.put("error", e);
errorLog.put("type", "startRecording");
socket.emit("ptt-error-logging", errorLog);
stopRecordingFromSocket();
}
}
}
}
}).start();
call.success();
} catch (Exception e) {
JSObject errorLog = new JSObject();
errorLog.put("error", e);
errorLog.put("type", "startRecording-whole");
socket.emit("ptt-error-logging", errorLog);
stopRecordingFromSocket();
}
}
I have tried to manipulate the size of the buffer but that did not seems to help at all :( thank you all
Upvotes: 2
Views: 149