1

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Monster Brain
  • 1,950
  • 18
  • 28
  • 1
    I've looked at the Youtube API for Android and haven't seen any volume controls. What I've seen on the Android documentation tho is the native method [setCurrentVolume](https://developer.android.com/reference/android/media/VolumeProvider.html) under the [VolumeProvider](https://developer.android.com/reference/android/media/VolumeProvider.html) object. – ReyAnthonyRenacia Apr 12 '18 at 09:17
  • Thanks for the reply. But does that also reduce the volume of the TextToSpeech voice also ? – Monster Brain Apr 12 '18 at 09:21
  • not familiar, sorry – ReyAnthonyRenacia Apr 12 '18 at 09:49

2 Answers2

2

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);
}
Monster Brain
  • 1,950
  • 18
  • 28
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);
Pierfrancesco Soffritti
  • 1,678
  • 1
  • 18
  • 22
  • Thanks for your kind reply. I've already tried your youtube-player (omg. You're the author !!) and some other based on webview, but couldn't get it to work (maybe I was using the latest android version emulator). I realized that the webview based youtube implementations has got volume control. Finally resolved to using the above workaround. – Monster Brain Apr 14 '18 at 17:05
  • @MonsterBrain WebView based implementations usually have problems running on emulators. I think it has something to do with graphics drivers. – Pierfrancesco Soffritti Apr 14 '18 at 17:16