Thursday 30 June 2022

How to capture TV remote microphone on Android TV OS?

I'm trying to use Watson Speech to Text API in my Android app on TV. I tried it on the TV emulator and, after enabling the mic on the virtual remote, the app work as it supposed to. But when I try the app on real hardware, its not recording my speech at all.

So what I did is add a piece of code found in the validated answer for "How to check if android microphone is available for use?". Then I added code in the onKeyDown() function of my class extendind GLSurfaceView to check if the center key of the keypad is pressed. In which case the app check if the device got mic then display the appropriate message depending on the availability of th microphone.

code-listing 1: check for mic

public class OpenGLView extends GLSurfaceView
{

    //constructors and other member functions here

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch(keyCode)
        {
            case KeyEvent.KEYCODE_DPAD_CENTER:
                if(getMicrophoneAvailable(ctx))
                {
                    Toast.makeText(Display.getInstance().getContext(), "Microphone available!", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(Display.getInstance().getContext(), "Microphone not available!", Toast.LENGTH_SHORT).show();
                }
        }
        
        return super.onKeyDown(keyCode, event);
    }
}

I tried the modified app on emulator with and without the mic enabled. the toast saying "Microphone available!" is the only one showing. Same thing when I try on my Android TV device. Either the code I got from "How to check if android microphone is available for use?" is not working as it was supposed to or microphone availability and activation is different on Android TV. I am hoping for the later. That's why I am here.

I'm wondering how to enable microphone programmatically. And I think it can be done because one can enable Voice Assistant menu at the top-left most menu on the TV by pushing the center DPAD button.

Top menu with Voice Assistant selected

The Android TV device I'm using has no mic on it but the mic is on the remote as seen on the picture below (mic hole on top left corner):

Android TV Remote

Also note that I'm loading the microphone for recording this way.

code-listing 3: loading and starting the mic

MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile("file.3gp");

mediaRecorder.prepare();
mediaRecorder.start();


from How to capture TV remote microphone on Android TV OS?

No comments:

Post a Comment