Reputation: 31
It's my second question about JavaFX. I am sorry cause I am not an experienced Java programmer and quite a novice with JavaFX. I don't understand previous answer thoroughly and will read it again and again later. But now I have the next question. I rewrite my program as follows:
import java.io.FileNotFoundException;
import java.io.IOException;
import static java.lang.Thread.sleep;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage; import javafx.util.Duration;
public class JavaFXMediaPlayer05 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws FileNotFoundException,IOException, InterruptedException {
Media media = new Media("file:///D:/1016_00.mp3");
//Set and play the first fragment of mp3-file
MediaPlayer mediaPlayer1 = new MediaPlayer(media);
mediaPlayer1.setStartTime(Duration.millis(1219.0));
mediaPlayer1.setStopTime(Duration.millis(2728.0));
mediaPlayer1.play();
System.out.println("1st fragment played!");
TimeUnit.SECONDS.sleep(3);
//Set and play the second fragment
MediaPlayer mediaPlayer2 = new MediaPlayer(media);
mediaPlayer2.setStartTime(Duration.millis(3947.0));
mediaPlayer2.setStopTime(Duration.millis(6629.0));
mediaPlayer2.play();
System.out.println("2nd fragment played!");
TimeUnit.SECONDS.sleep(3);
//Set and play the third fragment
MediaPlayer mediaPlayer3 = new MediaPlayer(media);
mediaPlayer3.setStartTime(Duration.millis(7453.0));
mediaPlayer3.setStopTime(Duration.millis(10704.0));
mediaPlayer3.play();
System.out.println("3rd fragment played!");
//Just sleep
System.out.println("pre-sleep");
TimeUnit.SECONDS.sleep(3);
System.out.println("after-sleep");
}
}
And I debug it in NetBeans step-by-step. I get a strange result. I hear the sound not at mediaplayer.play() but only at the end of method start(). All 3 fragments play at once. How do I manage them to play sequentially? Do I have to fire method start() many times - one time for every fragment? Or is there another way?
Upvotes: 1
Views: 268
Reputation: 34498
MediaPlayer
starts playing media asynchronously. So all your mediaPlayerN.play()
happen simultaneously.
You can use Timeline
to start playing at specific time, see below.
Also as you have only 1 media for all players you can use same MediaPlayer
for all samples. Just note you should use seek()
method instead of setStartTime()
in this case.
final MediaPlayer mediaPlayer1 = new MediaPlayer(media);
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, "plays at start", new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
mediaPlayer1.setStartTime(Duration.millis(1219.0));
mediaPlayer1.setStopTime(Duration.millis(2728.0));
mediaPlayer1.play();
}
}),
new KeyFrame(Duration.seconds(5), "plays after 5 seconds", new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
mediaPlayer1.seek(Duration.millis(3947.0));
mediaPlayer1.setStopTime(Duration.millis(6629.0));
mediaPlayer1.play();
}
}),
new KeyFrame(Duration.seconds(10), "plays after 10 seconds", new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
mediaPlayer1.seek(Duration.millis(7453.0));
mediaPlayer1.setStopTime(Duration.millis(10704.0));
mediaPlayer1.play();
}
})
);
timeline.play();
Upvotes: 2