Home > Net >  How to use Android Audio Manager using QAndroidJniObject to setSpeakerphoneOn(true)?
How to use Android Audio Manager using QAndroidJniObject to setSpeakerphoneOn(true)?

Time:03-08

Im writing qt sip phone application and I need to set speakerphoneOn with QAndroidJniObject but I dont know how to do it properly.

This is what Im doing at the moment but the application crashes.

package Loudspeaker;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;

public class Speaker extends Activity
{
    private AudioManager audioManager;

    public Speaker()
    {
        this.audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

    public String speaker_on()
    {
        audioManager.setSpeakerphoneOn(true);
        return "DONE FROM JAVA";
    }

    public String speaker_off()
    {
        audioManager.setSpeakerphoneOn(false);
        return "DONE FROM JAVA";
    }
}

QAndroidJniObject someJavaObject = QAndroidJniObject("Loudspeaker/Speaker");
        QString as = someJavaObject.callObjectMethod<jstring>("speaker_on").toString();
        logfile1 << as.toStdString();

I am new at java and found out it crashes because of "extends Activity" but It doesn't compile without that.

CodePudding user response:

I found out the solution:

package MyJava;

import org.qtproject.qt5.android.QtNative;
import android.content.Context;
import android.media.AudioManager;

public class Speaker
{
    public Speaker()
    {
    }

    private static AudioManager am()
    {
        Context c = QtNative.activity().getApplicationContext();
        return (AudioManager)c.getSystemService(c.AUDIO_SERVICE);
    }

    public static void speaker_on()
    {
        am().setSpeakerphoneOn(true);
    }

    public static void speaker_off()
    {
        am().setSpeakerphoneOn(false);
    }
}

and

QAndroidJniObject::callStaticMethod<void>("MyJava/Speaker", "speaker_on");
  • Related