how to record the voice and get the fft data in both android and ios?

mc 5,041 Reputation points
2024-12-30T11:01:28.2366667+00:00

I want to get the fft data and draw it .

I am trying platform on android:

mediaRecorder.SetAudioSource(AudioSource.Mic);
mediaRecorder.SetOutputFormat(OutputFormat.ThreeGpp);
mediaRecorder.SetAudioEncoder(AudioEncoder.AmrNb);
var file = Java.IO.File.CreateTempFile("audio", ".3gp");
mediaRecorder.SetOutputFile(file);
file.DeleteOnExit();
mediaRecorder.Prepare();
visualizer = new Visualizer(Visualizer.GetCaptureSizeRange()[1]);
visualizer.SetCaptureSize(visualizer.CaptureSize);
visualizer.SetDataCaptureListener(new DataCaptureListener(), Visualizer.MaxCaptureRate / 2, false, true);
visualizer.SetEnabled(true);
mediaRecorder.Start();

but it is not work it is recording and the visualizer data of fft and waveform is all zero .

how to do it?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,777 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Mike Slomczynski 0 Reputation points
    2024-12-30T13:41:36.66+00:00

    If I understand correctly, the recording is working but the visualizer is not. If that is the case, then what might be the issue is that the Visualizer was not properly instantiated.

    The Visualizer class has one public constructor and it calls for an audioSession not a captureSizeRange. See here Visualizer**
    **
    MediaRecorder does not have a method to get the audioSession from what I can see. In fact it looks like only MediaPlayer or AudioTrack audioSessions can be passed to the Visualizer

    From the Visualizer reference docs

    • If the session is not 0, the audio from a particular MediaPlayer or AudioTrack using this audio session is visualized

    You might be able to get away with using a MediaRecorder if you pass a 0 to the Visualizer. No guarantees, but according to the Visualizer reference docs

    • If the session is 0, the audio output mix is visualized

    So I would try changing this line

    visualizer = new Visualizer(Visualizer.GetCaptureSizeRange()[1]);
    

    to

    visualizer = new Visualizer(0);
    

    This might allow you to see the audio output from the MediaRecorder.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.