أصبح التعرف على الكلام 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" );
}
});يمكنك تكوين المعلمات المختلفة باستخدام أساليب Setter على مثيل الكلام ، والتي يمكنك الحصول عليها مثل هذا في أي مكان في الكود الخاص بك:
Speech . getInstance ()الرجوع إلى Javadocs للحصول على مرجع كامل.
بشكل افتراضي ، يتم تعطيل تسجيل المكتبة. يمكنك تمكين سجل التصحيح من خلال التذرع:
Logger . setLogLevel ( LogLevel . DEBUG );أينما تريد في الكود الخاص بك. يمكنك ضبط مستوى التفاصيل من التصحيح إلى OFF.
يستخدم 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().getTextToSpeechVoice() Speech.getInstance().getSpeechToTextLanguage() تحقق من التطبيق التجريبي للحصول على مثال كامل.
استخدم Speech.getInstance().getSupportedSpeechToTextLanguages(listener) Speech.getInstance().getSupportedTextToSpeechVoices() . تحقق من التطبيق التجريبي للحصول على مثال كامل.
Speech.getInstance().setVoice(voice) Speech.getInstance().setLocale(locale) تحقق من التطبيق التجريبي للحصول على مثال كامل.
عندما تقوم بتعيين اللغة ، يتم تغيير الصوت تلقائيًا إلى الصوت الافتراضي لتلك اللغة. إذا كنت ترغب في تعيين صوت معين ، تذكر إعادة تعيينه في كل مرة تقوم فيها بتغيير اللغة أيضًا.
بفضل 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 لمراجعة الكود وإصلاحات الأخطاء وأفكار تحسين المكتبات.