-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
184 lines (148 loc) · 6.14 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.ritrageproductions.app.chatbot;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText username;
MediaRecorder recorder;
String name;
String saveUrl = "/sdcard/VoiceBot/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
final TextView displayText = findViewById(R.id.displayText);
final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@Override
public void onResults(Bundle bundle) {
Toast.makeText(MainActivity.this, "blahblah", Toast.LENGTH_SHORT).show();
//getting all the matches
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
//displaying the first match
if (matches != null)
displayText.setText(matches.get(0));
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
boolean exists = (new File(saveUrl)).exists();
if(!exists) {
new File(saveUrl).mkdirs();
}
findViewById(R.id.confirmUsername).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = findViewById(R.id.username);
name = username.getText().toString() + ".mp3";
}
});
// recorder = new MediaRecorder();
// recorder = new MediaRecorder();
// recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// recorder.setAudioEncodingBitRate(160 * 1024);
// recorder.setOutputFile(saveUrl + name);
findViewById(R.id.onRecord).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP: {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
mSpeechRecognizer.stopListening();
}
break;
case MotionEvent.ACTION_DOWN: {
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(160 * 1024);
recorder.setOutputFile(saveUrl + name);
try {
recorder.prepare();
//Toast.makeText(MainActivity.this, "nahi kata", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
//Toast.makeText(MainActivity.this, "kat gaya", Toast.LENGTH_SHORT).show();
}
recorder.start();
displayText.setText("");
displayText.setHint("Listening...");
}
break;
}
return false;
}
});
}
private void checkPermission() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(!(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivity(intent);
finish();
}
}
}
}