android source

Sound, media 대신 ring mode로 설정

리오파파 2024. 10. 19. 21:46

TTS를 다음과 같이 정의

mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mFocusGain = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
        .build();
mTTS = null;
mTTS = new TextToSpeech(mContext, status -> {
    if (status == TextToSpeech.SUCCESS) {
        initializeTTS();
    }
});

 

initializeTTS()에서 STREAM_MUSIC 대신 STREAM_RING으로 변경

 

  private void initializeTTS() {

        // switch Media Volume to Ring Volume, so that Media volume is always near to zero
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build();
        mTTS.setAudioAttributes(audioAttributes);
//        AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
//        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

        mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {
                ttsID = utteranceId;
            }

            @Override
            // this method will always called from a background thread.
            public void onDone(String utteranceId) {
                new Timer().schedule(new TimerTask() {
                    public void run () {
                        if (mTTS.isSpeaking())
                            return;
                        NotificationBar.hideStop();
                        isTalking = false;
                        beepOnce(soundType.POST.ordinal());
                        mAudioManager.abandonAudioFocusRequest(mFocusGain);
                    }
                }, 100);
            }

            @Override
            public void onError(String utteranceId) {
                new Utils().logE("Sound", "TTS Error:" + utteranceId);
                logUpdate.addLog("TTS Error", utteranceId);
            }
        });

        int result = mTTS.setLanguage(Locale.getDefault());
        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            beepOnce(soundType.ERR.ordinal());
        } else {
            mTTS.setPitch(1.2f);
            mTTS.setSpeechRate(1.3f);
        }
    }

'android source ' 카테고리의 다른 글

TextView clicked animation  (0) 2024.12.03
textview touch color 변화  (0) 2024.10.24
sub class 에서 localbroadcaster로 main call 하기  (0) 2024.09.21
PhoneShaking Detect  (1) 2024.09.17
String Utility  (0) 2024.09.16