diff --git a/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/WebViewYouTubePlayer.java b/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/WebViewYouTubePlayer.java index 22ceb398..94094979 100644 --- a/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/WebViewYouTubePlayer.java +++ b/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/WebViewYouTubePlayer.java @@ -120,6 +120,20 @@ public void run() { }); } + @Override + public void setVolume(final int volumePercent) { + if (volumePercent < 0 || volumePercent > 100) { + throw new IllegalArgumentException("Volume must be between 0 and 100"); + } + + mainThreadHandler.post(new Runnable() { + @Override + public void run() { + loadUrl("javascript:setVolume(" + volumePercent + ")"); + } + }); + } + @Override public void seekTo(final int time) { mainThreadHandler.post(new Runnable() { diff --git a/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/YouTubePlayer.java b/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/YouTubePlayer.java index 09a9348b..de562b42 100644 --- a/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/YouTubePlayer.java +++ b/YouTubePlayer/src/main/java/com/pierfrancescosoffritti/youtubeplayer/player/YouTubePlayer.java @@ -18,6 +18,11 @@ public interface YouTubePlayer { void pause(); void mute(); void unMute(); + + /** + * @param volumePercent Integer between 0 and 100 + */ + void setVolume(final int volumePercent); void seekTo(final int time); @PlayerConstants.PlayerState.State int getCurrentState(); diff --git a/YouTubePlayer/src/main/res/raw/youtube_player.html b/YouTubePlayer/src/main/res/raw/youtube_player.html index dd4f2779..4d8d5b62 100644 --- a/YouTubePlayer/src/main/res/raw/youtube_player.html +++ b/YouTubePlayer/src/main/res/raw/youtube_player.html @@ -150,5 +150,9 @@ player.unMute(); } + function setVolume(volumePercent) { + player.setVolume(volumePercent); + } + \ No newline at end of file diff --git a/sample/src/main/java/com/pierfrancescosoffritti/youtubeplayersample/MainActivity.java b/sample/src/main/java/com/pierfrancescosoffritti/youtubeplayersample/MainActivity.java index 05088c1a..cc1b19f9 100644 --- a/sample/src/main/java/com/pierfrancescosoffritti/youtubeplayersample/MainActivity.java +++ b/sample/src/main/java/com/pierfrancescosoffritti/youtubeplayersample/MainActivity.java @@ -53,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) { initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() { @Override public void onReady() { + initializedYouTubePlayer.setVolume(100); initializedYouTubePlayer.loadVideo(videoIds[0], 0); setVideoTitle(youTubePlayerView.getPlayerUIController(), videoIds[0]); }