我寫了一個後臺服務來查找用戶的位置。現在,我想在他的位置發生變化時向用戶發送語音通知。 我已經編寫了用於確定位置更改和發送簡單推送通知的代碼,現在任何人都可以幫助我進行語音通知或如何編寫語音通知代碼,或者是否有任何第三方庫通過它將語音通知發送到用戶。 幫助讚賞。 感謝android向用戶發送語音通知
1
A
回答
2
是的,終於我以非常簡單的方式得到它和一個完整的解決方案。
private TextToSpeech myTTS; // Define the TTS objecy
private int MY_DATA_CHECK_CODE = 0;
//write the following code in oncreate method or whereever you want to use this
{
myTTS = new TextToSpeech(this, this);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
speakWords("Pass the String here");
}
private void speakWords(String speech) {
//speak straight away
//myTTS.setLanguage(Locale.US);
System.out.println(speech + " TTSTTTS");
myTTS.speak(speech, TextToSpeech.LANG_COUNTRY_AVAILABLE, null);
}
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
} else if (status == TextToSpeech.ERROR) {
Toast.makeText(getApplicationContext(), "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
}
}
1
採用Android文本到語音API see reference here
2
下面是一個例子 link,其部分可能需要被粘貼在這裏。
private TextToSpeech myTTS;
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
speakWords(words); //call this function with the string you want as voice
//speak the user text
private void speakWords(String speech) {
//speak straight away
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
//act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
}
else {
//no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
//setup TTS and don't forget to implement OnInitListener interface to use this method
public void onInit(int initStatus) {
//check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.US);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
}
}
1
在您的通知方法(其中notificationManager.notify(0, notification);
叫),添加以下內容:
try {
Uri ring_uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), ring_uri);
r.play();
} catch (Exception e) {
// Error playing sound
}
這將使通知聲音,只要你推送通知。
相關問題
- 1. 向最終用戶發送Android通知
- 2. 向用戶發送「推送」通知
- 3. 向所有用戶發送android推送通知
- 4. Android,向多個用戶發送推送通知
- 5. 如何通過門戶向Android發送Azure推送通知?
- 6. MYSQL向多個用戶發送通知
- 7. 如何向用戶發送通知?
- 8. 發送用戶通知到Android
- 9. 使用Android的Azure通知中心 - 向用戶的子集發送通知
- 10. Twilio - 通過向用戶發送遠程通知邀請用戶
- 11. Android:在某個日期前7天向用戶發送通知。
- 12. Firebase通知 - 如何僅向特定用戶發送通知?
- 13. 發送通知給用戶
- 14. 如何使用GCM向特定用戶發送推送通知
- 15. 使用Malcom API向特定用戶發送推送通知
- 16. 使用FCM向不同用戶發送推送通知
- 17. 如何使用GCM向多個用戶發送推送通知
- 18. Azure應用服務 - 向特定用戶發送推送通知
- 19. 通過應用程序向FB用戶發送通知
- 20. 向JavaScript發送通知
- 21. 向卵石發送通知
- 22. 向特定用戶發送推送通知
- 23. 向查詢中的所有用戶發送推送通知
- 24. 如何向所選用戶iphone發送推送通知?
- 25. 如何向50位用戶發送推送通知
- 26. 如何向用戶發送推送通知
- 27. 向特定用戶發送推送通知 - Cordova
- 28. 如何向特定用戶發送解析推送通知
- 29. 向特定用戶發送遠程推送通知
- 30. 向iOS用戶發送多個推送通知
嘿,我可以自定義這個聲音的意思,現在它唯一的通知我希望它說特定的字符串。 請回復ASAP –
如果你有你想播放的每個聲音的語音文件,你可以使用這個,但我認爲你的問題並非如此。你應該使用TextToSpeech類 –
好的,仍然感謝您的幫助 –