Skip to content

Instantly share code, notes, and snippets.

@davisonio
Created January 12, 2018 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davisonio/4c4112a14c54708adf02689a9ba4954c to your computer and use it in GitHub Desktop.
Save davisonio/4c4112a14c54708adf02689a9ba4954c to your computer and use it in GitHub Desktop.
JavaFX Music Functions
*/* CODE AT TOP OF CLASS SO THAT MEDIA PLAYER IS ACCESSIBLE EVERYWHERE */*
public static MediaPlayer songPlayer = null;
*/* CODE TO START PLAYING */*
File songFile = new File("Music/amazingsong.mp3"); // Music folder
should be in project root, not in 'src'
if (songFile.isFile()) {
Media songMedia = new Media(songFile.toURI().toString());
songPlayer = new MediaPlayer(songMedia);
songPlayer.play();
} else {
System.out.println("File error.");
}
*/* CODE TO PAUSE */*
if (songPlayer != null) {
songPlayer.pause();
}
*/* CODE TO CHANGE VOLUME */*
if (songPlayer != null) {
songPlayer.setVolume(0.5); // Set volume to 50%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment