首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android向用户发送语音通知

android向用户发送语音通知
EN

Stack Overflow用户
提问于 2012-12-04 21:01:03
回答 4查看 4K关注 0票数 2

我已经写了一个后台服务来查找用户的位置。现在我想在用户的位置改变时发送语音通知给用户。我已经写了确定位置变化和发送简单的推送通知的代码,现在有人可以帮助我使它语音通知或如何编写语音通知代码,或是否有任何第三方库,我们发送语音通知给用户。感谢你的帮助。谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-05 14:41:25

是的,最终我以一种非常简单的方式得到了一个完整的解决方案。

代码语言:javascript
复制
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();
    }
}
票数 3
EN

Stack Overflow用户

发布于 2012-12-04 21:18:47

下面是一个示例link,您可能需要将其部分粘贴到此处。

代码语言:javascript
复制
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();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2012-12-04 21:08:23

使用android文本转语音接口see reference here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13703551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档