Advertisement
zeev

my own siri

Nov 17th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. MainActivity.java
  2. =========================
  3. package com.example.zeevm.mypersonalassistance;
  4.  
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.speech.RecognizerIntent;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;
  13.  
  14. import java.util.ArrayList;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18.     //identify our system service
  19.     final int AUDIO_RESULT=100;
  20.     TextView txtRes;
  21.     ImageView btnMic;
  22.     Context context;
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         setPointer();
  28.     }
  29.  
  30.     private void setPointer()
  31.     {
  32.         this.context=this;
  33.         txtRes=(TextView)findViewById(R.id.txtMic);
  34.         btnMic=(ImageView)findViewById(R.id.btnMic);
  35.         btnMic.setOnClickListener(new View.OnClickListener() {
  36.             @Override
  37.             public void onClick(View view) {
  38.                 sendAudio();
  39.             }
  40.         });
  41.     }
  42.  
  43.     private void sendAudio()
  44.     {
  45.         //we are going to existing activity from the android os itself.
  46.         Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  47.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  48.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  49.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Im ready my master..");
  50.         startActivityForResult(intent,AUDIO_RESULT);
  51.     }
  52.  
  53.     @Override
  54.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  55.         if (resultCode==RESULT_OK && requestCode==AUDIO_RESULT)
  56.         {
  57.             String strRes="";
  58.             Bundle bundle = data.getExtras();
  59.             final ArrayList<String> matches=bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
  60.             for (int counter=0;counter<matches.size();counter+=1)
  61.             {
  62.                 strRes+=matches.get(counter)+"\n";
  63.             }
  64.             txtRes.setText(strRes);
  65.         }
  66.     }
  67. }
  68.  
  69.  
  70. activity_main.xml
  71. ========================
  72. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  73.     android:layout_width="match_parent"
  74.     android:layout_height="match_parent"
  75.     android:background="#fff"
  76.     android:orientation="vertical">
  77.  
  78.     <ImageView
  79.         android:layout_width="wrap_content"
  80.         android:layout_height="wrap_content"
  81.         android:layout_gravity="center"
  82.         android:src="@drawable/mic"
  83.         android:id="@+id/btnMic"/>
  84.     <TextView
  85.         android:layout_width="match_parent"
  86.         android:layout_height="match_parent"
  87.         android:text="\nready to get your command my master....."
  88.         android:id="@+id/txtMic"/>
  89.  
  90. </LinearLayout>
  91.  
  92.  
  93. manifest (need to add premission)
  94. =======================================
  95.  <uses-permission android:name="android.permission.INTERNET"/>
  96.     <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  97.     <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement