1) MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/mic"
tools:ignore="VectorDrawableCompat" />
</android.support.design.widget.CoordinatorLayout>
2) MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextToSpeech textToSpeech;
private SpeechRecognizer speechRecognizer;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initTxtToSpeech();
initSpeechRecognizer();
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
speechRecognizer.startListening(intent);*/
speack("My name is sexy and i am your Your artificial intelligence");
/* Date date = new Date();
String time = DateUtils.formatDateTime(MainActivity.this, date.getTime(),
DateUtils.FORMAT_SHOW_TIME);
speack("the time now is " + time);
speack("thanks for giving me this opportunity to help you");*/
}
});
}
private void initSpeechRecognizer() {
if(SpeechRecognizer.isRecognitionAvailable(this)){
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
List<String> result = results.getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
getingResult(result.get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
}
}
private void getingResult(String cmd){
cmd = cmd.toLowerCase();
if(cmd.indexOf("what") != -1){
if(cmd.indexOf("your name") != -1){
speack("my name is sexy");
}
if(cmd.indexOf("time") != -1){
Date date = new Date();
String time = DateUtils.formatDateTime(this, date.getTime(),
DateUtils.FORMAT_SHOW_TIME);
speack("the time now is " + time);
}
} else if(cmd.indexOf("open") != -1){
if(cmd.indexOf("browser") != -1){
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
startActivity(i);
}
}
}
private void initTxtToSpeech() {
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(textToSpeech.getEngines().size() == 0){
Toast.makeText(MainActivity.this, "Your Device Is Not upport", Toast.LENGTH_SHORT).show();
finish();
} else {
textToSpeech.setLanguage(Locale.US);
speack("Hi This Is Sexy Your artificial intelligence");
}
}
});
}
private void speack(String msg){
if(Build.VERSION.SDK_INT >=21){
textToSpeech.speak(msg, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
textToSpeech.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
textToSpeech.shutdown();
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/mic"
tools:ignore="VectorDrawableCompat" />
</android.support.design.widget.CoordinatorLayout>
2) MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextToSpeech textToSpeech;
private SpeechRecognizer speechRecognizer;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initTxtToSpeech();
initSpeechRecognizer();
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
speechRecognizer.startListening(intent);*/
speack("My name is sexy and i am your Your artificial intelligence");
/* Date date = new Date();
String time = DateUtils.formatDateTime(MainActivity.this, date.getTime(),
DateUtils.FORMAT_SHOW_TIME);
speack("the time now is " + time);
speack("thanks for giving me this opportunity to help you");*/
}
});
}
private void initSpeechRecognizer() {
if(SpeechRecognizer.isRecognitionAvailable(this)){
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
List<String> result = results.getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
getingResult(result.get(0));
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
}
}
private void getingResult(String cmd){
cmd = cmd.toLowerCase();
if(cmd.indexOf("what") != -1){
if(cmd.indexOf("your name") != -1){
speack("my name is sexy");
}
if(cmd.indexOf("time") != -1){
Date date = new Date();
String time = DateUtils.formatDateTime(this, date.getTime(),
DateUtils.FORMAT_SHOW_TIME);
speack("the time now is " + time);
}
} else if(cmd.indexOf("open") != -1){
if(cmd.indexOf("browser") != -1){
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
startActivity(i);
}
}
}
private void initTxtToSpeech() {
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(textToSpeech.getEngines().size() == 0){
Toast.makeText(MainActivity.this, "Your Device Is Not upport", Toast.LENGTH_SHORT).show();
finish();
} else {
textToSpeech.setLanguage(Locale.US);
speack("Hi This Is Sexy Your artificial intelligence");
}
}
});
}
private void speack(String msg){
if(Build.VERSION.SDK_INT >=21){
textToSpeech.speak(msg, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
textToSpeech.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
textToSpeech.shutdown();
}
}
1 comment:
Vidmate help
Post a Comment