馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
大家好,我是剛接觸app的新手
由於公司的需求所以必須要將手機上的wav檔案分析成文字顯示出來
目前我參照網路上的code 寫出來 是按一個按鈕透過自己講話後再分析成文字顯示在textview上
code如下
MainActivity.java
public class MainActivity extends AppCompatActivity implements RecognitionListener {
private TextView returnedText;
private ProgressBar progressBar;
private ToggleButton toggleButton;
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
returnedText = (TextView)findViewById(R.id.textView1);
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
progressBar.setVisibility(View.INVISIBLE);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
}else{
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
}
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
if (speech != null) {
speech.destroy();
Log.i("LOG_TAG", "destroy");
}
}
@Override
public void onBeginningOfSpeech() {
//使用者開始說話,可以在此時通知UI秀出相關icon,告知使用者已收到聲音輸入了
Log.i("LOG_TAG", "onBeginningOfSpeech");
progressBar.setIndeterminate(false);
progressBar.setMax(10);
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.i("LOG_TAG", "onBufferReceived: " + buffer);
}
@Override
public void onEndOfSpeech() {
Log.i("LOG_TAG", "onEndOfSpeech");
progressBar.setIndeterminate(true);
toggleButton.setChecked(false);
}
@Override
public void onError(int errorCode) {
String errorMessage = getErrorText(errorCode);
Log.d("LOG_TAG", "FAILED " + errorMessage);
returnedText.setText(errorMessage);
toggleButton.setChecked(false);
}
@Override
public void onEvent(int arg0, Bundle arg1) {
Log.i("LOG_TAG", "onEvent");
}
@Override
public void onPartialResults(Bundle arg0) {
Log.i("LOG_TAG", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle arg0) {
Log.i("LOG_TAG", "onReadyForSpeech");
//已經準備好可以接受語音輸入,可以在此時通知UI秀出相關icon,告知使用者開始說話
}
@Override
public void onResults(Bundle results) {
Log.i("LOG_TAG", "onResults");
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result : matches){
text += result + "\n";
}
returnedText.setText(text);
}
@Override
public void onRmsChanged(float rmsdB) {
Log.i("LOG_TAG", "onRmsChanged: " + rmsdB);
progressBar.setProgress((int) rmsdB);
}
public static String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}
}
activity_main.xml的部分
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="nissan.myapplication.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="horizontal"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="48dp"
android:layout_height="50dp"
android:layout_weight="1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:textSize="35sp"/>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_weight="1"
android:text="ToggleButton" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml 權限設定加了
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
請問我要怎麼在按下按鈕的時候辨識手機裡面的wav檔案(檔案位置如下↓)
File sd = new File(Environment.getExternalStorageDirectory().getPath() + "/BLEAudioFiles/Audio2017-05-03 07:20:02.204.wav");
我另外有查過google的google-speech-v2的API(網址:https://github.com/gillesdemey/google-speech-v2)
但不太知道要怎麼做...查了好多相關文章但似乎都無法達到我的需求
|
|