Skip to content

Commit

Permalink
Merge pull request #57 from talklittle/set-volume
Browse files Browse the repository at this point in the history
Add setVolume API
  • Loading branch information
PierfrancescoSoffritti authored Nov 16, 2017
2 parents 690ed5e + fd24f8e commit f665981
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions YouTubePlayer/src/main/res/raw/youtube_player.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,9 @@
player.unMute();
}

function setVolume(volumePercent) {
player.setVolume(volumePercent);
}

</script>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down

0 comments on commit f665981

Please sign in to comment.