just-ai / aimybox-android-assistant

Embeddable custom voice assistant for Android applications
https://aimybox.com
Apache License 2.0
226 stars 52 forks source link

How to extend Aimybox Assistant to different Activities? #61

Open Tedcas opened 3 years ago

Tedcas commented 3 years ago

Hi,

I'm working in a new project where I need a custom voice assistant in my android app. I tried this example Project and everything works perfectly, however, if I go from MainActivity to another activity and I try to load the assistant on this new activity, tts, stt and wake word doesn't work, I press the floating button and the assitant does its animation, but thats all, my question is, How can I implement something like the example project but extending the assistant in all the activities?

Thank you very much in advance.

bgubanov commented 3 years ago

Hello! Are you initializing the Aimybox object from the Application instance, like in the example? Can you send code snippet with the second activity? I added second activity to example project, copied code of first activity and everything started correctly on each of activities.

Billthebest1 commented 3 years ago

can anyone out there make this do everything the voice activated assisstant by speaktoit could if so I'm willing to pay

On Fri, Feb 12, 2021, 10:17 AM bgubanov notifications@github.com wrote:

Hello! Are you initializing the Aimybox object from the Application instance, like in the example? Can you send code snippet with the second activity? I added second activity to example project, copied code of first activity and everything started correctly on each of activities.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/just-ai/aimybox-android-assistant/issues/61#issuecomment-778291044, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2SMTDBN72LHH6OVIPI5YDS6VICRANCNFSM4XPOZN5Q .

Tedcas commented 3 years ago

Hello! Are you initializing the Aimybox object from the Application instance, like in the example? Can you send code snippet with the second activity? I added second activity to example project, copied code of first activity and everything started correctly on each of activities.

Hi, first of all many thanks for your reply ¡, I'm sorry I couldn't reply you earlier, answering your question, yes I'm initializing the Aimybox object from Application instance like the example, here is my code for that I've modified it a little bit to adjust to my project:

class AimyboxApplication : Application(), AimyboxProvider {

override val aimybox by lazy {
    createAimybox(this)
}

fun createAimybox(context: Context): Aimybox {
    val assetsKaldi = KaldiAssets.fromApkAssets(context, "vosk-model-small-es-0.3")

    val unitId = UUID.randomUUID().toString()
    val voiceTrigger = KaldiVoiceTrigger(assetsKaldi, listOf(context.getString(R.string.wakeWord1), context.getString(R.string.wakeWord2), context.getString(R.string.wakeWord3)))

    val textToSpeech =   GooglePlatformTextToSpeech(context, Locale.getDefault())

    val speechToText = GooglePlatformSpeechToText(context, Locale.getDefault())

    val RasaBot = BotEngine(
            MainScenario().model,
            activators = arrayOf(
                    RasaIntentActivator.Factory(RasaApi("https://949f1f18e64a.ngrok.io"))
            )
    )

    val dialogApi = JAICFDialogApi(unitId, RasaBot)

    return Aimybox(Config.create(speechToText, textToSpeech, dialogApi) {
        this.voiceTrigger = voiceTrigger
    })
}

And this is the class I use to create the Assistant in every class of my project:

class VoiceAssistant constructor(context: Context, container: Int) : Serializable, ActivitiesFather() {

var context = context as AppCompatActivity
var containerID = container
    get() = field

fun callAssistant(): AimyboxAssistantFragment? {
    if (AskForPermission(context, Manifest.permission.RECORD_AUDIO, MIC_PERMISSION))
        return drawAssitant()
    else
        return null
}

fun drawAssitant(): AimyboxAssistantFragment {
    //Set the context of the Scenario to let it control the app UI
    MainScenario.Mycontext = context

    context.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

    val aimyboxAssistantFragment = AimyboxAssistantFragment()

    context.supportFragmentManager.beginTransaction().apply {
        this.replace(containerID, aimyboxAssistantFragment)
        commit()
        runOnUiThread {
            val bottomUp = AnimationUtils.loadAnimation(context, R.anim.bottom_up)
            bottomUp.duration = 500
            val aimyboxButton: View = aimyboxAssistantFragment.requireView().findViewById(R.id.fragment_aimybox_assistant_button)
            aimyboxButton.startAnimation(bottomUp)
            aimyboxButton.setVisibility(View.VISIBLE)
        }
    }

    return aimyboxAssistantFragment
}

fun onBackPressedAssistant(): Boolean? {
    val assistantFragment = (context.supportFragmentManager.findFragmentById(containerID)
            as? AimyboxAssistantFragment)
    if(assistantFragment != null)
     return assistantFragment.onBackPressed()
    else
        return false;
}

So in Activity 1 I call the following methods on onCreate():

       voiceAssistant = new VoiceAssistant(this, R.id.assistant_container);
       voiceAssistant.callAssistant();

And then in the next activity I do the same (I simplified this step, calling those two statements in the father of all the Activities of my project)

I'm not sure what I'm doing wrong, I must admit I'm not used to Kotlin (nowadays I'm learning the language) and I could make a misstake there.

Again many thanks for your help in advance.

Tedcas commented 3 years ago

Hi, it's been a while since my last posr, after doing some tests I've realized that the problem only happens when I use KaldiVoiceTrigger with the TTS, STT and DialogApi together, if I disable the KaldiVoiceTrigger system, all works perfectly. I've try updating all the dependencies to the last version, but nothing change.

cdhiraj40 commented 2 years ago

does KaldiVoiceTrigger work perfectly for you folks? @bgubanov @Tedcas For me, it just triggers every time I say anything! Let me know thanks.