Telefonunuza kendi asistanınızı yapın.&Siri &Jarvis gibi.

0
274
views
GoogleSpeak
googlespeak

An article I’m trying to tell you can do your own mobile assistant.

After a long time, we will use Google Speech Recognition to describe your problem to your phone and write an application that will answer you using TTS.

Let’s start!

First of all, your device should have the settings described in the following pictures. Also, we can not enable offline mode because there is no Turkish support.

Now let’s get to the codes. You can also download the project from my Github address.

First, we add permissions to manifest.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Now we are editing in activity_main.xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/arkaplannew"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


        
         <ImageButton
             android:id="@+id/voice_button"
             android:layout_width="114dp"
             android:layout_height="wrap_content"
             android:background="@android:drawable/dialog_holo_dark_frame"
             android:src="@android:drawable/ic_btn_speak_now"
             android:layout_weight="0.22"
             android:layout_alignParentTop="true"
             android:layout_centerHorizontal="true" />

        <EditText
            android:id="@+id/speech_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textColor="#ff7aff10"
            android:textColorHint="#ffff23d1"
            android:inputType="textMultiLine"
            android:padding="20dp"
            android:layout_below="@+id/voice_button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>

        <EditText
            android:id="@+id/cevap_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"
            android:textColor="#ff7aff10"
            android:textColorHint="#ffff23d1"
            android:layout_below="@+id/speech_txt"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
</RelativeLayout>
package info.androidhive.speechtotext;
package com.app.serhat.aydnserhatsezenspeechrecognizer;

import java.util.ArrayList;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements
        TextToSpeech.OnInitListener {

    public ImageButton voice_button;
    public EditText multiline_txt;
    public EditText cevap;
    public Intent intent;
    public static final int request_code_voice = 1;
    private SpeechRecognizer speech = null;
    private TextToSpeech t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        voice_button = (ImageButton)findViewById(R.id.voice_button);
        multiline_txt = (EditText)findViewById(R.id.speech_txt);
        cevap = (EditText)findViewById(R.id.cevap_txt);

        multiline_txt.setEnabled(false);
        cevap.setEnabled(false);

        t1 = new TextToSpeech(this, this);

        voice_button.setOnClickListener(new OnClickListener() { 

            @Override
            public void onClick(View v) {

                intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

                try{
                    startActivityForResult(intent, request_code_voice);
                }catch(ActivityNotFoundException e)
                {
                    e.printStackTrace();
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage("Telefonunuz bu sistemi desteklemiyor!!!")
                            .setTitle("Asistan")
                            .setPositiveButton("TAMAM", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();
                }
            }
        });

    }
    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = t1.setLanguage(new Locale("tr", "TR"));

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            } else {
                konus();
            }
        } else {
        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case request_code_voice: {

                if (resultCode == RESULT_OK && data != null)
                {
                    ArrayList speech = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    multiline_txt.setText(speech.get(0));


                    if(speech.get(0).equals("nasılsın") ){
                        cevap.setText("İyiyim sen nasılsın");
                        konus();
                    }
                    if(speech.get(0).equals("Ben de iyiyim") ){
                        cevap.setText("Güzel bir gün o zaman senin için.");
                        konus();
                    }
                    if(speech.get(0).equals("adın ne") ){
                        cevap.setText("Adım Aydın Serhat");
                        konus();
                    }
                    if(speech.get(0).equals("şarkı listesini açar mısın") || speech.get(0).equals("şarkı listesini aç")){
                        cevap.setText("Tamam");
                        konus();
                        Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
                        startActivity(intent);
                    }if(speech.get(0).equals("Instagram aç") || speech.get(0).equals("İnstagram açar mısın") ){
                    cevap.setText("Tabi");
                    konus();
                    String apppackage = "com.instagram.android";
                    Context cx=this;
                    try {
                        Intent i = cx.getPackageManager().getLaunchIntentForPackage(apppackage);
                        cx.startActivity(i);
                    } catch (Exception  e) {
                        Toast.makeText(this, "Sorry, Instagram Apps Not Found", Toast.LENGTH_LONG).show();
                    }
                }


                }
                break;
            }
        }
    }
    private void konus() {
        String toSpeak = cevap.getText().toString();
        t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

    }


}

It’s that simple, actually.

Here you can put your assistant’s name by talking with using SharedPreferences, you can teach him various commands.

CEVAP VER

Please enter your comment!
Please enter your name here