如何在Android中获取/设置媒体音量(spotify)和TTS? [英] How do you get/set media volume (spotify) and TTS in Android?
问题描述
减少流量并同时增加TTS音量
Decrease the stream volume and Increase TTS volume at the same time
我有这个Android应用程序每次都与TTS对话并使用流式音乐(spotify)。因此,我需要降低音量并增加TTS
I had this Android application that talk with TTS every time and uses streaming music (spotify). So, I need to decrease the volume and increase the TTS
我使用此代码但这也会减少tts。
I use this code but this decrease the tts as well.
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,[int value],[if desired a flag]);
任何方式都可以减少像流音乐这样的Spotify。
Any way to decrease only spotify like stream music .
在Java / android。
in Java/android.
推荐答案
你想要做的是知道'audio ducking'
这里有一些示例代码:
/**
* Our {@link AudioManager.OnAudioFocusChangeListener}
*/
private static AudioManager.OnAudioFocusChangeListener audioFocus = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(final int focusChange) {
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager focusChange: " + focusChange);
}
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_GAIN");
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS");
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS_TRANSIENT");
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
}
break;
default:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager focusChange: AUDIOFOCUS default");
}
break;
}
}
};
/**
* Duck the audio
*
* @param ctx the application context
*/
public static void duckAudioMedia(final Context ctx) {
if (DEBUG) {
MyLog.i(CLS_NAME, "duckAudioMedia");
}
try {
final AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
switch (audioManager.requestAudioFocus(audioFocus, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)) {
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
if (DEBUG) {
MyLog.w(CLS_NAME, "AudioManager duckAudioMedia AUDIOFOCUS_REQUEST_FAILED");
}
break;
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager duckAudioMedia AUDIOFOCUS_REQUEST_GRANTED");
}
break;
default:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager duckAudioMedia AUDIOFOCUS default");
}
break;
}
} catch (final NullPointerException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "duckAudioMedia: NullPointerException");
e.printStackTrace();
}
} catch (final Exception e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "duckAudioMedia: Exception");
e.printStackTrace();
}
}
}
/**
* Notify the System that any previous condition requiring to duck or pause audio is now complete.
*
* @param ctx the application context
*/
public static void abandonAudioMedia(final Context ctx) {
if (DEBUG) {
MyLog.i(CLS_NAME, "abandonAudioMedia");
}
try {
final AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
switch (audioManager.abandonAudioFocus(audioFocus)) {
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
if (DEBUG) {
MyLog.w(CLS_NAME, "AudioManager abandonAudioMedia AUDIOFOCUS_REQUEST_FAILED");
}
break;
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager abandonAudioMedia AUDIOFOCUS_REQUEST_GRANTED");
}
break;
default:
if (DEBUG) {
MyLog.i(CLS_NAME, "AudioManager abandonAudioMedia AUDIOFOCUS default");
}
break;
}
} catch (final NullPointerException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "abandonAudioMedia: NullPointerException");
e.printStackTrace();
}
} catch (final Exception e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "abandonAudioMedia: Exception");
e.printStackTrace();
}
}
}
你必须删除我的自定义日志记录。
You'll have to delete my custom logging.
在TTS启动时调用 duckAudioMedia
并且 abandonAudioMedia
当话语完成时。
Call duckAudioMedia
as the TTS starts and abandonAudioMedia
when the utterance is complete.
这篇关于如何在Android中获取/设置媒体音量(spotify)和TTS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!