I made simple class that capture voice input from mic and I build it for android .
when the app run for first time, the capturing working fine after passing record permission and I can see data voice data output, however, once I close the app and open it again, the Capturing state set to OK but I see no data at all .
If I disabled the mic permssion manually and restarted the app, it works fine once again, close it and open it , problem happen again .
Whats wrong ?!
Here is also the CPP file
// Fill out your copyright notice in the Description page of Project Settings.
#include "SoundCapture.h"
#include "OnlineSubsystemUtils.h"
// Sets default values
ASoundCapture::ASoundCapture()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
void ASoundCapture::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
isCapture = false;
}
void ASoundCapture::FinishDestroy()
{
isCapture = false;
if (VoiceCapture.IsValid())
{
VoiceCapture->Stop();
VoiceCapture->Shutdown();
}
Super::FinishDestroy();
}
void ASoundCapture::StartCaptureVoice()
{
VoiceCapture = /*FVoiceModule::CreateVoiceCapture("", 512, 32);*/ FVoiceModule::Get().CreateVoiceCapture();
VoiceCapture->Start();
isCapture = true;
}
// Called when the game starts or when spawned
void ASoundCapture::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Warning, TEXT("Custom player controller initialized"));
isCapture = false;
}
// Called every frame
void ASoundCapture::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (isCapture)
{
VoiceCaptureBytesAvailable = 0;
CaptureState = VoiceCapture->GetCaptureState(VoiceCaptureBytesAvailable);
UE_LOG(LogTemp, Warning, TEXT("Bytes available: %d\nCapture state: %s"), VoiceCaptureBytesAvailable, EVoiceCaptureState::ToString(CaptureState));
GEngine->AddOnScreenDebugMessage(-1, 0.1f, FColor::Yellow, EVoiceCaptureState::ToString(CaptureState));
VoiceCaptureBuffer.Reset();
if (CaptureState == EVoiceCaptureState::Ok && VoiceCaptureBytesAvailable > 0)
{
VoiceCaptureTotalSquared = 0;
VoiceCaptureBuffer.SetNumUninitialized(VoiceCaptureBytesAvailable);
VoiceCapture->GetVoiceData(VoiceCaptureBuffer.GetData(), VoiceCaptureBytesAvailable, VoiceCaptureReadBytes);
for (uint32 i = 0; i < (VoiceCaptureReadBytes / 2); i++)
{
VoiceCaptureSample = (VoiceCaptureBuffer[i * 2 + 1] << 8) | VoiceCaptureBuffer[i * 2];
VoiceCaptureTotalSquared += ((float)VoiceCaptureSample * (float)VoiceCaptureSample);
}
VoiceCaptureMeanSquare = (2 * (VoiceCaptureTotalSquared / VoiceCaptureBuffer.Num()));
VoiceCaptureRms = FMath::Sqrt(VoiceCaptureMeanSquare);
VoiceCaptureFinalVolume = ((VoiceCaptureRms / 32768.0) * 200.f);
VeryCleanString = FString::SanitizeFloat(VoiceCaptureFinalVolume);
//VoiceCaptureVolume = VoiceCaptureFinalVolume;
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, VeryCleanString);
}
}
}
I’ve checked with monitor.bat , log at first time has no errors, but when I start the app once again i get this strange error E/AudioRecord(24604): start() status -38
Android 6 used .
Here is the full LOG FullLog