android向用户发送语音通知
我已经编写了一个后台服务来查找用户的位置。现在,当用户的位置发生变化时,我想向用户发送语音通知。 我已经编写了用于确定位置更改和发送简单推送通知的代码,现在是否有人可以帮助我进行语音通知,或者如何编写语音通知代码,或者是否有任何第三方库,我们可以通过该库向用户发送语音通知。 谢谢你的帮助。android向用户发送语音通知,android,notifications,Android,Notifications,我已经编写了一个后台服务来查找用户的位置。现在,当用户的位置发生变化时,我想向用户发送语音通知。 我已经编写了用于确定位置更改和发送简单推送通知的代码,现在是否有人可以帮助我进行语音通知,或者如何编写语音通知代码,或者是否有任何第三方库,我们可以通过该库向用户发送语音通知。 谢谢你的帮助。 感谢使用android文本语音转换api,这里是一个例子 ,您可能需要的零件粘贴在此处 private TextToSpeech myTTS; Intent checkTTSIntent = new Int
感谢使用android文本语音转换api,这里是一个例子 ,您可能需要的零件粘贴在此处
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();
}
}
在通知方法(其中调用了
notificationManager.notify(0,通知);
)中,添加以下内容:
try {
Uri ring_uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), ring_uri);
r.play();
} catch (Exception e) {
// Error playing sound
}
每当你推送通知时,它都会发出通知声。是的,最后我以非常简单的方式得到了它,并找到了一个完整的解决方案
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();
}
}
嘿,我可以自定义这个声音吗?我现在只想让它说特定的字符串。请尽快回复如果你有你想播放的每个声音的声音文件,你可以使用这个,但我认为这不是你的问题的情况。您应该使用TextToSpeech类