การจดจำคำพูดของ 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 เพียงแค่ชำระเงินโครงการและลองดู
ภายในกิจกรรม:
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 );ทุกที่ที่คุณต้องการในรหัสของคุณ คุณสามารถปรับระดับรายละเอียดจากการดีบักเป็นปิด
Library Logger ใช้ 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() และ Speech.getinstance().getTextToSpeechVoice() ตรวจสอบแอพสาธิตสำหรับตัวอย่างที่สมบูรณ์
ใช้ Speech.getInstance().getSupportedSpeechToTextLanguages(listener) และ Speech.getInstance().getSupportedTextToSpeechVoices() ตรวจสอบแอพสาธิตสำหรับตัวอย่างที่สมบูรณ์
ใช้ Speech.getInstance().setLocale(locale) และ 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 สำหรับการตรวจสอบรหัสการแก้ไขข้อผิดพลาดและแนวคิดการปรับปรุงห้องสมุด