Skip to content

Commit

Permalink
Added Volume Control for BeatBoxer
Browse files Browse the repository at this point in the history
  • Loading branch information
iamKunal committed Nov 16, 2016
1 parent 4fa7061 commit 21747c2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
17 changes: 16 additions & 1 deletion src/beatboxer/BeatBoxer.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
<bottom>
<BorderPane id="player" fx:id="player" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<top>
<Slider fx:id="timeSlider" BorderPane.alignment="CENTER" />
<Slider fx:id="timeSlider" BorderPane.alignment="CENTER">
<padding>
<Insets left="5.0" right="5.0" />
</padding></Slider>
</top>
<left>
<Label fx:id="timer" minWidth="70.0" prefWidth="70.0" text="-- : --" BorderPane.alignment="CENTER" />
Expand Down Expand Up @@ -205,6 +208,18 @@
</children>
</VBox>
</right>
<left>
<VBox BorderPane.alignment="CENTER">
<children>
<Label fx:id="volumeLabel" maxWidth="45.0" minWidth="45.0" prefWidth="45.0" text="100" textAlignment="CENTER" />
<Slider fx:id="volumeSlider" maxHeight="-Infinity" orientation="VERTICAL" prefHeight="500.0" prefWidth="45.0">
<padding>
<Insets bottom="5.0" left="10.0" right="5.0" top="5.0" />
</padding>
</Slider>
</children>
</VBox>
</left>
</BorderPane>
</children>
</AnchorPane>
19 changes: 3 additions & 16 deletions src/beatboxer/BeatBoxer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class BeatBoxer extends Application {
public static StringProperty state;
public static BBSong defaultSong;
public static Timeline timer;
public static double volume;
@Override
public void start(Stage stage) throws Exception {
state= new SimpleStringProperty("Unknown");
Expand Down Expand Up @@ -101,28 +102,14 @@ public static void playNext(){
}
}

private static double getvolumevalue(String genre) {
double volume;
if (genre.contains("Dance") || genre.contains("Party") || genre.contains("Rock") || genre.contains("Metal") || genre.contains("Progressive House")) {
volume = 1.0;
} else if (genre.contains("Pop") || genre.contains("Electro") || genre.contains("Techno") || genre.contains("Edm") || genre.contains("Hip hop") || genre.contains("Hip-hop") || genre.contains("Trap")) {
volume = 0.05;
} else if (genre.contains("Romantic") || genre.contains("Soothing") || genre.contains("Soul") || genre.contains("Piano") || genre.contains("Tropical House") || genre.contains("Ambient")) {
volume = 0.02;
} else if (genre.contains("Bollywood")) {
volume = 0.1;
} else {
volume = 0.3;
}
return volume;
}


public static void play(BBSong track){
mediaPlayer.dispose();
mediaPlayer = toMediaPlayer(track.getLocation());
initMediaPlayer();
currentIndex = BBGenerator.find(nowPlaying, track);
mediaPlayer.setVolume(getvolumevalue(track.getGenre()));
mediaPlayer.setVolume(volume);
mediaPlayer.play();
}
public static void initMediaPlayer(){
Expand Down
55 changes: 52 additions & 3 deletions src/beatboxer/BeatBoxerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public class BeatBoxerController implements Initializable {
@FXML
private Button shuffleButton;
@FXML
private Slider volumeSlider;
@FXML
private Label volumeLabel;
@FXML
private ToggleButton autoplayButton;
public static ChangeListener currentTimePropertyListener;
public static ChangeListener totalDurationPropertyListener;
Expand Down Expand Up @@ -302,13 +306,18 @@ private void createPlaylist(){
public void playNext(){
int size = BeatBoxer.nowPlaying.size();
BeatBoxer.mediaPlayer.stop();
BeatBoxer.play(BeatBoxer.nowPlaying.get((BeatBoxer.currentIndex + 1)%size));
BBSong a;
a=BeatBoxer.nowPlaying.get((BeatBoxer.currentIndex + 1)%size);
setVolumeValue(a.getGenre());
BeatBoxer.play(a);
}
@FXML
public void playPrevious(){
int size = BeatBoxer.nowPlaying.size();
BeatBoxer.mediaPlayer.stop();
BeatBoxer.play(BeatBoxer.nowPlaying.get((BeatBoxer.currentIndex + size - 1)%size));
BBSong a = BeatBoxer.nowPlaying.get((BeatBoxer.currentIndex + size - 1)%size);
setVolumeValue(a.getGenre());
BeatBoxer.play(a);
}
@FXML
private void shuffle(){
Expand Down Expand Up @@ -353,6 +362,7 @@ public void playAlbum(BBItem album){
favouriteButton.setDisable(false);
nowPlayingListView.setItems(BeatBoxer.nowPlaying);
listViewTabPane.getSelectionModel().select(0);
setVolumeValue(BeatBoxer.nowPlaying.get(0).getGenre());
BeatBoxer.play(BeatBoxer.nowPlaying.get(0));
BeatBoxer.mediaPlayer.stop();
}
Expand All @@ -374,6 +384,7 @@ public void playArtist(BBItem artist){
favouriteButton.setDisable(false);
nowPlayingListView.setItems(BeatBoxer.nowPlaying);
listViewTabPane.getSelectionModel().select(0);
setVolumeValue(BeatBoxer.nowPlaying.get(0).getGenre());
BeatBoxer.play(BeatBoxer.nowPlaying.get(0));
BeatBoxer.mediaPlayer.stop();
}
Expand Down Expand Up @@ -402,6 +413,7 @@ private int getListViewIndex(ListView<BBSong> l, BBSong song){
}
private void refresh(){
try{
volumeSlider.setValue(100);
nowPlayingListView.setDisable(true);
nowPlayingListView.setItems(null);
trackDetails.setText("\n\nNo track Playing.");
Expand Down Expand Up @@ -474,11 +486,27 @@ else if (playlist.getId()==-1){
favouriteButton.setDisable(true);
}
else{
setVolumeValue(BeatBoxer.nowPlaying.get(0).getGenre());
BeatBoxer.play(BeatBoxer.nowPlaying.get(0));
}
BeatBoxer.mediaPlayer.stop();
}

private double setVolumeValue(String genre) {
double volume=100;
if (genre.contains("Dance") || genre.contains("Party") || genre.contains("Rock") || genre.contains("Metal") || genre.contains("Progressive House")) {
volume = 100;
} else if (genre.contains("Pop") || genre.contains("Electro") || genre.contains("Techno") || genre.contains("Edm") || genre.contains("Hip hop") || genre.contains("Hip-hop") || genre.contains("Trap")) {
volume = 70;
} else if (genre.contains("Romantic") || genre.contains("Soothing") || genre.contains("Soul") || genre.contains("Piano") || genre.contains("Tropical House") || genre.contains("Ambient")) {
volume = 40;
} else if (genre.contains("Bollywood")) {
volume = 80;
} else {
volume = 90;
}
volumeSlider.setValue(volume);
return BeatBoxer.volume;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
newPlayListMenu.setAccelerator(new KeyCodeCombination(KeyCode.N));
Expand Down Expand Up @@ -513,6 +541,24 @@ public void changed(ObservableValue<? extends Object> observable, Object oldValu
BeatBoxer.mediaPlayer.stop();
});
}
volumeSlider.valueProperty().addListener(new ChangeListener<Number>(){
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
volumeLabel.setText(Integer.toString(newValue.intValue()));
if(newValue.intValue()>=1){
BeatBoxer.volume = 1-(0.5*Math.log10(100-newValue.intValue()));
if(Double.isInfinite(BeatBoxer.volume)){
BeatBoxer.volume = 1.00;
}
BeatBoxer.mediaPlayer.setVolume(BeatBoxer.volume);
}
else{
BeatBoxer.volume=0;
BeatBoxer.mediaPlayer.setVolume(0);
}
}

});
/*-------------Slider and Time Listeners----------------------------*/

currentTimePropertyListener = new ChangeListener<Duration>() {
Expand Down Expand Up @@ -624,6 +670,7 @@ public void handle(MouseEvent click) {
else{
BBSong a = nowPlayingListView.getSelectionModel().getSelectedItem();
nowPlayingListView.getSelectionModel().select(-1);
setVolumeValue(a.getGenre());
BeatBoxer.play(a);
}
}
Expand All @@ -642,6 +689,7 @@ public void handle(MouseEvent click) {
allsongsListView.getSelectionModel().select(-1);
System.out.println(a.getId());
playPlaylist(new BBItem(0, "All Songs"));
setVolumeValue(a.getGenre());
BeatBoxer.play(a);
}
}
Expand Down Expand Up @@ -710,6 +758,7 @@ public void handle(MouseEvent click) {
songListView.getSelectionModel().select(-1);
System.out.println(a.getId());
playPlaylist(new BBItem(0, "All Songs"));
setVolumeValue(a.getGenre());
BeatBoxer.play(a);
}
}
Expand Down

0 comments on commit 21747c2

Please sign in to comment.