Android文本到语音(TTS)仅在foor循环中播放最后一个值

时间:2018-06-14 11:04:20

标签: android text-to-speech

我有一些代码可以从网站中提取一些JSON,我希望它循环遍历字符串值,然后将它们转换为语音。

问题是,所有字符串都正确打印,但文本到语音只播放最后一个值。如何在循环播放数组时播放每个单独的值。

以下是我对代码所做的工作。

    public class MainActivity extends AppCompatActivity  {

    TextToSpeech t1;
    final String BASE_URL = "x";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }


        });



        final JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, BASE_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {

                    JSONObject data = response.getJSONObject("x");
                    JSONArray children = data.getJSONArray("x");

                    for (int i = 0; i<children.length(); i++) {

                        JSONObject json_data = children.getJSONObject(i).getJSONObject("x");
                        Log.v("test", "Err:" + json_data.getString("title"));

                        String a = json_data.getString("title");

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {


                            t1.speak(a,TextToSpeech.QUEUE_FLUSH,null,null);
                        } else {
                            t1.speak(a, TextToSpeech.QUEUE_FLUSH, null);
                        }



                    }


                } catch (JSONException e) {

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.v("Test", "Err:" + error.getLocalizedMessage());
            }
        });

        Volley.newRequestQueue(this).add(jsonRequest);
    }


}

谢谢

3 个答案:

答案 0 :(得分:0)

QUEUE_FLUSH会导致任何先前的语音被新的语音中断。您可以将其更改为QUEUE_ADD,并且会说出所有内容。

这些解释为in the TTS documentation

答案 1 :(得分:0)

您可以简单地连接字符串并在for循环结束时播放文本,如下所示

String my_text = "";
for (int i = 0; i<children.length(); i++) 
{
    JSONObject json_data = children.getJSONObject(i).getJSONObject("x");
    Log.v("test", "Err:" + json_data.getString("title"));

    String a = json_data.getString("title");
    my_text ="    "+a;
 }

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
 {
     t1.speak(my_text ,TextToSpeech.QUEUE_FLUSH,null,null);
 } else {
     t1.speak(my_text , TextToSpeech.QUEUE_FLUSH, null);
 }

答案 2 :(得分:0)

我也面临同样的问题。希望我的回答能对某人有所帮助。

我在kotlin中使用了间隔很少的数据数组列表。您可以根据数据数组更改分隔符字符串。

private fun convertTextToSpeech() {
val speech = arrayList.joinToString(separator = "?")
val myHash = HashMap<String, String>()
val bundle = Bundle()
bundle.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"done")
myHash[TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID] = "done"

val splitSpeech = speech.split("\\?".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()

for (i in splitSpeech.indices) {
    Log.i("responseString",splitSpeech[i])
    if (i == 0) { // Use for the first splited text to flush on audio stream
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tts?.speak( splitSpeech[i].trim { it <= ' ' },TextToSpeech.QUEUE_FLUSH, bundle, null);
        } else {
            tts?.speak( splitSpeech[i].trim { it <= ' ' }, TextToSpeech.QUEUE_FLUSH, myHash);
        }

    } else { // add the new test on previous then play the TTS
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tts?.speak( splitSpeech[i].trim { it <= ' ' }, TextToSpeech.QUEUE_ADD, bundle, null);
        } else {
            tts?.speak( splitSpeech[i].trim { it <= ' ' }, TextToSpeech.QUEUE_ADD, myHash);
        }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tts?.playSilentUtterance(750, TextToSpeech.QUEUE_ADD, null)
    } else {
        tts?.playSilence(750, TextToSpeech.QUEUE_ADD, null)
    }
}

}