问题
I'm using Youtube Api V3 for playing a video in my android App. However, I'm using Text to speech along with the video, so I would like to mute the video so that the other audio is audible.
I've searched the documentation and internet, but found solution only for javascript. Any help would be appreciated. Thanks.
回答1:
I Still haven't found the exact solution to mute the Youtube Video using the Api.
Instead, Here's the work-around solution I've reached, in my case. Hope it might be helpful for someone else.
I've set the TTS Stream to STREAM_ALARM.(refer this question)
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
textToSpeech.speak("Hello World", TextToSpeech.QUEUE_FLUSH, params);
Now I Mute the SREAM_MUSIC Volume and I got the result I wanted. Anyone got better Ideas are more than welcome.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
am.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
回答2:
The YouTube Data API v3 has nothing to do with playback, as the name implies it is used only for accessing YouTube's data.
If you want to mute a video you have to do that on a YouTubePlayer of some sort. This is the official YouTube player from Google. I am not sure you can mute the volume with this though, probably not.
Android-YouTube-Player is an alternative player, it is easier to use and you can change the volume programmatically.
YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(new YouTubePlayerInitListener() {
@Override
public void onInitSuccess(final YouTubePlayer initializedYouTubePlayer) {
initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady() {
String videoId = "6JYIGclVQdw";
initializedYouTubePlayer.loadVideo(videoId, 0);
initializedYouTubePlayer.setVolume(0);
}
});
}
}, true);
来源:https://stackoverflow.com/questions/49789660/how-to-mute-a-youtube-video-by-using-youtube-api-v3