Home > front end >  How can I acces microphone input in .Net MAUI?
How can I acces microphone input in .Net MAUI?

Time:10-30

I am developing .net maui app that measures sound volume and sends it with bluetooth to pc that logs data and makes graph as a hobby project. My problem is accesing microphone input. There seems to be no multiplatform way to do this in maui. So i tried to do it using platform specific android api, but when I tried so, it seemed like class that I needed was not fully implemented in maui Android namespace.

The part of my code I have trouble with is this:

using Android.Media;
using Java.Util;

public static class MicIn
{
    static MediaRecorder mRecorder;


    // Other functions

    public static void startRecorder()
    {
        if (mRecorder == null)
        {
            mRecorder = new MediaRecorder();
            mRecorder.SetAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.SetOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.SetAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.SetOutputFile("/dev/null");
            try
            {
                mRecorder.Prepare();
            }
            catch (Java.IO.IOException ioe)
            {
                Debug.WriteLine("IO exception");

            }
            catch (Java.Lang.SecurityException e)
            {
                Debug.WriteLine("Security exception");
            }
            try
            {
                mRecorder.Start();
            }
            catch (Java.Lang.SecurityException e)
            {
                Debug.WriteLine("Security exception");
            }
        }
    }
}

Visual studio gives me errors on MediaRecorder.AudioSource.MIC, MediaRecorder.OutputFormat.THREE_GPP and MediaRecorder.AudioEncoder.AMR_NB It says that these classes do not have definition for MIC, THREE_GPP and AMR_NB constants, even if they are in official android api documentation.

Do you have any ideas what might be error or other ways of taking microphone input in .net maui? Thank you for help.

CodePudding user response:

according to the docs

AudioSource Mic

OutputFormat ThreeGpp

AudioEncoder AmrNb

  • Related