Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- =========================
- package com.example.zeevm.mypersonalassistance;
- import android.content.Context;
- import android.content.Intent;
- import android.speech.RecognizerIntent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //identify our system service
- final int AUDIO_RESULT=100;
- TextView txtRes;
- ImageView btnMic;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer()
- {
- this.context=this;
- txtRes=(TextView)findViewById(R.id.txtMic);
- btnMic=(ImageView)findViewById(R.id.btnMic);
- btnMic.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- sendAudio();
- }
- });
- }
- private void sendAudio()
- {
- //we are going to existing activity from the android os itself.
- Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Im ready my master..");
- startActivityForResult(intent,AUDIO_RESULT);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode==RESULT_OK && requestCode==AUDIO_RESULT)
- {
- String strRes="";
- Bundle bundle = data.getExtras();
- final ArrayList<String> matches=bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
- for (int counter=0;counter<matches.size();counter+=1)
- {
- strRes+=matches.get(counter)+"\n";
- }
- txtRes.setText(strRes);
- }
- }
- }
- activity_main.xml
- ========================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#fff"
- android:orientation="vertical">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:src="@drawable/mic"
- android:id="@+id/btnMic"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="\nready to get your command my master....."
- android:id="@+id/txtMic"/>
- </LinearLayout>
- manifest (need to add premission)
- =======================================
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.RECORD_AUDIO"/>
- <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement