Androidの音声認識とテキストからスピーチへのテキストは簡単になりました。
implementation ' net.gotev:speech:x.y.z ' xyzを置き換えます
ライブラリの使用を開始するには、アクティビティでライブラリを初期化する必要があります
public class YourActivity extends Activity {
Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . your_layout );
Speech . init ( this , getPackageName ());
}
@ Override
protected void onDestroy () {
// prevent memory leaks when activity is destroyed
Speech . getInstance (). shutdown ();
}
}このライブラリをexamples Directoryで使用する完全に機能するデモアプリを見つけることができます。プロジェクトをチェックアウトして、試してみてください。
アクティビティ内:
try {
// you must have android.permission.RECORD_AUDIO granted at this point
Speech . getInstance (). startListening ( new SpeechDelegate () {
@ Override
public void onStartOfSpeech () {
Log . i ( "speech" , "speech recognition is now active" );
}
@ Override
public void onSpeechRmsChanged ( float value ) {
Log . d ( "speech" , "rms is now: " + value );
}
@ Override
public void onSpeechPartialResults ( List < String > results ) {
StringBuilder str = new StringBuilder ();
for ( String res : results ) {
str . append ( res ). append ( " " );
}
Log . i ( "speech" , "partial result: " + str . toString (). trim ());
}
@ Override
public void onSpeechResult ( String result ) {
Log . i ( "speech" , "result: " + result );
}
});
} catch ( SpeechRecognitionNotAvailable exc ) {
Log . e ( "speech" , "Speech recognition is not available on this device!" );
// You can prompt the user if he wants to install Google App to have
// speech recognition, and then you can simply call:
//
// SpeechUtil.redirectUserToGoogleAppOnPlayStore(this);
//
// to redirect the user to the Google App page on Play Store
} catch ( GoogleVoiceTypingDisabledException exc ) {
Log . e ( "speech" , "Google voice typing must be enabled!" );
}あなたのアクティビティのonDestroyで、追加してください:
@ Override
protected void onDestroy () {
Speech . getInstance (). shutdown ();
}メモリリークを防ぐため。
これをレイアウトに追加します。
< LinearLayout
android : orientation = " vertical "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : id = " @+id/linearLayout " >
< net .gotev.speech.ui.SpeechProgressView
android : id = " @+id/progress "
android : layout_width = " 120dp "
android : layout_height = " 150dp " />
</ LinearLayout > SpeechProgressViewが常にLinearLayoutの内部にあるために適切に機能することが重要です。それに応じて、バーの高さ設定に幅と高さを調整できます(以下を参照)。
次に、音声認識を開始したら、 SpeechProgressViewにも合格します。
Speech . getInstance (). startListening ( speechProgressView , speechDelegate );必要に応じて5つのバーの色をすべて設定できます。これは単なる例です:
int [] colors = {
ContextCompat . getColor ( this , android . R . color . black ),
ContextCompat . getColor ( this , android . R . color . darker_gray ),
ContextCompat . getColor ( this , android . R . color . black ),
ContextCompat . getColor ( this , android . R . color . holo_orange_dark ),
ContextCompat . getColor ( this , android . R . color . holo_red_dark )
};
speechProgressView . setColors ( colors ); int [] heights = { 60 , 76 , 58 , 80 , 55 };
speechProgressView . setBarMaxHeightsInDp ( heights );アクティビティ内:
Speech . getInstance (). say ( "say something" );ステータスを受信するためのコールバックを提供することもできます。
Speech . getInstance (). say ( "say something" , new TextToSpeechCallback () {
@ Override
public void onStart () {
Log . i ( "speech" , "speech started" );
}
@ Override
public void onCompleted () {
Log . i ( "speech" , "speech completed" );
}
@ Override
public void onError () {
Log . i ( "speech" , "speech error" );
}
});スピーチインスタンスでセッターメソッドを使用して、さまざまなパラメーターを構成できます。これは、コードのどこでもこのように入手できます。
Speech . getInstance ()完全な参照については、Javadocsを参照してください。
デフォルトでは、ライブラリのロギングが無効になっています。 [デバッグログ]を呼び出して有効にすることができます。
Logger . setLogLevel ( LogLevel . DEBUG );コードのどこにでも必要です。デバッグからオフまでの詳細レベルを調整できます。
ライブラリロガーはデフォルトでandroid.util.Logを使用するため、 LogCatで出力を取得します。ログを別の出力にリダイレクトするか、別のロガーを使用する場合は、次のような独自のデリゲートの実装を提供できます。
Logger . setLoggerDelegate ( new Logger . LoggerDelegate () {
@ Override
public void error ( String tag , String message ) {
//your own implementation here
}
@ Override
public void error ( String tag , String message , Throwable exception ) {
//your own implementation here
}
@ Override
public void debug ( String tag , String message ) {
//your own implementation here
}
@ Override
public void info ( String tag , String message ) {
//your own implementation here
}
}); Speech.getInstance().getSpeechToTextLanguage() and Speech.getinstance().getTextToSpeechVoice()使用してください。完全な例については、デモアプリを確認してください。
Speech.getInstance().getSupportedSpeechToTextLanguages(listener)とSpeech.getInstance().getSupportedTextToSpeechVoices()を使用します。完全な例については、デモアプリを確認してください。
Speech.getInstance().setLocale(locale) and Speech.getInstance().setVoice(voice)を使用します。完全な例については、デモアプリを確認してください。
ロケールを設定すると、音声はその言語のデフォルト音声に自動的に変更されます。特定の音声を設定したい場合は、ロケールを変更するたびに再セットすることを忘れないでください。
音声認識ビューの元の実装をしてくれた@Zagumに感謝します。
Copyright (C) 2019 Aleksandar Gotev
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
コードレビュー、バグの修正、ライブラリ改善のアイデアについては、Kristiyan Petrovに感謝します。