setStreamMute(STREAM_RING)将文本静音到语音android

时间:2013-06-30 17:10:57

标签: java android text-to-speech

我想说一些文字。由于某些奇怪的原因,STREAM_RING还将文本转换为静音。我已经搜索了互联网,但我想之前没有人有这个问题,所以我找不到答案。这是我的代码:

int volume=0;

    if(state == TelephonyManager.CALL_STATE_RINGING)
    {
        Toast.makeText(getApplicationContext(), "Inside", Toast.LENGTH_SHORT).show();

        if(incomingNumber != null)
        {
            volume= audioManager.getStreamVolume(AudioManager.STREAM_RING);


            audioManager.setStreamMute(AudioManager.STREAM_RING, true);
            audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, AudioManager.FLAG_ALLOW_RINGER_MODES);
            tts.speak(incomingNumber, TextToSpeech.QUEUE_FLUSH, null);

            Toast.makeText(getApplicationContext(), "speak number", Toast.LENGTH_SHORT).show();
        }


        if(state == TelephonyManager.CALL_STATE_IDLE)
        {
            audioManager.setStreamVolume(AudioManager.STREAM_RING, volume, AudioManager.FLAG_ALLOW_RINGER_MODES);
            audioManager.setStreamMute(AudioManager.STREAM_RING, false);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

int mRingerMode;

Toast.makeText(getApplicationContext(), "INCOMING CALL", Toast.LENGTH_SHORT).show();

if(state == TelephonyManager.CALL_STATE_RINGING)
{
    Toast.makeText(getApplicationContext(), "Inside", Toast.LENGTH_SHORT).show();

    if(incomingNumber != null)
    {
        mRingerMode = audioManager.getRingerMode();
                        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                HashMap<String, String> myHashRender = new HashMap<String, String>();
                myHashRender.put(TextToSpeech.Engine.KEY_PARAM_STREAM, 
                        String.valueOf(AudioManager.STREAM_VOICE_CALL));
                tts.speak(incomingNumber, TextToSpeech.QUEUE_FLUSH, myHashRender);
            }
        }).start();

        Toast.makeText(getApplicationContext(), "speak number", Toast.LENGTH_SHORT).show();
    }


    if(state == TelephonyManager.CALL_STATE_IDLE)
    {
        audioManager.setRingerMode(mRingerMode);
    }
}

}