我有问题,我使用Text To Speech并使用MediaPlayer播放一些声音。据我所知,如果我想控制MediaPlayer的音量,我必须设置:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
但我注意到我的TextToSpeech是由铃声音量控制的。因此,当我设置STREAM_MUSIC
时,我可以设置sfx音量,但是当我没有设置STREAM_MUSIC
时,无法设置TTS音量,反之亦然。问题是 - 如何同时控制两个卷或如何将我的tts流设置为音乐。最后请给我一些其他的想法。
edit1:
所以这是我在我的Singleton
中初始化的我的SoundManager类public class SoundManager {
private static boolean allowed = true;
private MediaPlayer mediaPlayer;
public SoundManager(Context context) {
mediaPlayer = new MediaPlayer();
}
public boolean isAllowed() {
return allowed;
}
public static void setAllowed(boolean allowed) {
SoundManager.allowed = allowed;
}
public void Play(Context context, String text) {
if(allowed) {
int identifier = context.getResources().getIdentifier(text, "raw", context.getPackageName());
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
}
mediaPlayer = MediaPlayer.create(context, identifier);
mediaPlayer.start();
}
}
public void destroy(){
if (mediaPlayer != null) {
mediaPlayer.release();
}
}
这是我的演讲班:
public class Speaker implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private static boolean ready = false;
private static boolean allowed = true;
public Speaker(Context context){
tts = new TextToSpeech(context, this);
}
public boolean isAllowed(){
return allowed;
}
public static boolean isReady() {
return ready;
}
public static void setAllowed(boolean allowed){
Speaker.allowed = allowed;
}
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
ready = true;
int result = 0;
result = tts.setLanguage(Locale.US); // Language set
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
ready = false;
}
}else{
ready = false;
}
}
public void speak(String text){
// Speak only if the TTS is ready
// and the user has allowed speech
if(ready && allowed) {
HashMap<String, String> hash = new HashMap<String,String>();
hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
tts.speak(text, TextToSpeech.QUEUE_ADD, hash);
}
}
public void pause(int duration){
tts.playSilence(duration, TextToSpeech.QUEUE_ADD, null);
}
// Free up resources
public void destroy(){
tts.shutdown();
}
在我的活动中onCreate
我穿上了
setVolumeControlStream(AudioManager.STREAM_MUSIC);
然后我可以管理音量控制而不是TTS。