[android] 벨소리 울리는 중에 TTS 말하기

도움 받은 곳 : http://stackoverflow.com/questions/15654983/changing-ringtone-with-tts-in-android

 

public class MainActivity extends Activity
{
  private TextToSpeech _tts;
  private AudioManager _audioManager;
  private int _volume;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    _tts = new TextToSpeech(getApplicationContext(), ttsInitListener);
    _tts.setSpeechRate(1);
    
    _audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    
    TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    manager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
  }
  
  
  private void speakWithDelay(final String $msg)
  {
    Runnable runn = new Runnable()
    {
      @Override
      public void run()
      {
        _volume = _audioManager.getStreamVolume(AudioManager.STREAM_RING);
        _audioManager.setStreamMute(AudioManager.STREAM_RING, true);
        speak($msg);
      }
    };
    Handler handler = new Handler();
    handler.postDelayed(runn, 3000);
  }
  
  
  private void speak(String $msg)
  {
    Log.i("SystemTTS.java | speak", "|System TTS speak|" + $msg + "|");
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "messageID");
    
    try
    {
      _tts.speak($msg, TextToSpeech.QUEUE_FLUSH, map);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  
  private void restoreVolume()
  {
    _audioManager.setStreamMute(AudioManager.STREAM_RING, false);
    _audioManager.setStreamVolume(AudioManager.STREAM_RING, _volume, AudioManager.FLAG_ALLOW_RINGER_MODES);
  }
  
  private OnInitListener ttsInitListener = new OnInitListener()
  {
    @SuppressLint("NewApi")
    @Override
    public void onInit(int status)
    {
      if (status != TextToSpeech.SUCCESS)
        return;
      
      if (VERSION.SDK_INT == VERSION_CODES.ICE_CREAM_SANDWICH)
        _tts.setOnUtteranceCompletedListener(completedListener);
      else
        _tts.setOnUtteranceProgressListener(progressListener);
    }
  };
  
  @SuppressLint("NewApi")
  private UtteranceProgressListener progressListener = new UtteranceProgressListener()
  {
    @Override
    public void onStart(String utteranceId)
    {
    }
    
    
    @Override
    public void onError(String utteranceId)
    {
    }
    
    
    @Override
    public void onDone(String utteranceId)
    {
      restoreVolume();
      Log.i("SystemTTS.java | onDone", "|" + "old system TTS speak complete" + "|");
    }
  };
  
  @SuppressWarnings("deprecation")
  private OnUtteranceCompletedListener completedListener = new OnUtteranceCompletedListener()
  {
    @Override
    public void onUtteranceCompleted(String utteranceId)
    {
      restoreVolume();
      Log.i("SystemTTS.java | onUtteranceCompleted", "|" + "new system TTS speak complete" + "|");
    }
  };
  
  private PhoneStateListener phoneStateListener = new PhoneStateListener()
  {
    public void onCallStateChanged(int state, String incomingNumber)
    {
      Log.i("MainActivity.java | onCallStateChanged", "|state:" + state + "(ringing:" + TelephonyManager.CALL_STATE_RINGING + ", offhook:" + TelephonyManager.CALL_STATE_OFFHOOK + ", idle:"
          + TelephonyManager.CALL_STATE_IDLE + ")" + "|number:" + incomingNumber + "|");
      
      if (state == TelephonyManager.CALL_STATE_RINGING)
      {
        speakWithDelay("집 떠나와 열차타고 훈련소로 가는 날");
      }
      else if (state == TelephonyManager.CALL_STATE_IDLE)
      {
        restoreVolume();
      }
    };
  };
}
<uses-permission android:name="android.permission.READ_PHONE_STATE" />