import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; import javafx.geometry.Pos; public class AudioMediaDemo extends Application { private static final String MEDIA_URL = "http://profesores.elo.utfsm.cl/~agv/elo329/JavaProg/JavaFX/AudioMediaDemo/droneSound.mp3"; /* other options: private static final String MEDIA_URL = "file:/path/to/file/droneSound.mp3"; // or private static final String MEDIA_URL = AudioMediaDemo.class.getResource("droneSound.mp3").toExternalForm(); */ public void start(Stage primaryStage) { Media media = new Media(MEDIA_URL); MediaPlayer mediaPlayer = new MediaPlayer(media); mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE); Button playButton = new Button(">"); playButton.setOnAction(e -> { if (playButton.getText().equals(">")) { mediaPlayer.play(); playButton.setText("||"); } else { mediaPlayer.pause(); playButton.setText(">"); } }); Slider slVolume = new Slider(); slVolume.setPrefWidth(150); slVolume.setMaxWidth(Region.USE_PREF_SIZE); slVolume.setMinWidth(30); slVolume.setValue(50); mediaPlayer.volumeProperty().bind(slVolume.valueProperty().divide(100)); HBox hBox = new HBox(10); hBox.setAlignment(Pos.CENTER); hBox.getChildren().addAll(playButton, new Label("Volume"), slVolume); Scene scene = new Scene(hBox, 350, 100); primaryStage.setTitle("AudioMediaDemo"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }