1

I've been using MediaPlayer on win10 and it works perfectly fine, but on my MacBook Air (10.13.4 (17E202)) is no sound output at all. I've already found similar problems and observed that its caused by the GC.

I tried everything avoiding this, based on answers like this one

I tried making the media player final, static, private, public, working with getter and setter, outsourcing, as a class var, as a local var but nothing is working.

I'm running on Java 9.0.4.

If I add player.setCycleCount(MediaPlayer.INDEFINITE); the sound plays, but its looping and if I set it to 2 its working well, but my next , back, play, pause buttons are getting very weird then.

Using AudioClip instead is no option for me, because I have to use the MediaPlayer .

public class Main extends Application {

  MediaPlayer player;

  @Override
  public void start(Stage primaryStage) throws Exception {
    BorderPane root = new BorderPane();
    File file = new File("/users/xxx/desktop/xxx/Song.mp3");
    Media media = new Media(file.toURI().toString());
    this.player = new MediaPlayer(media);
    player.play();
    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
  }
}

I would appreciate every help.

Holger
  • 285,553
  • 42
  • 434
  • 765
Darano
  • 11
  • 1
  • Your issue has nothing to do with these other question, even if they happen to be connected to the Mediaplayer. There is no indicator that your issue is related to the garbage collector at all. It’s also not surprising that changing the access modifier of the variable doesn’t change the behavior of the program. As you may have encountered a bug, you should be more specific about “buttons are getting very weird then”… – Holger May 28 '18 at 08:01
  • Thanks for your answer so far. Actually only the "next-Song" und "prev-Song" buttons are not working anymore. I can play a song, and if i press next-Song button nothing is happening and Im not able to use any other buttons anymore.I really think the setCycleCount-way is not the right way.. – Darano Jun 01 '18 at 14:10
  • This happens to me with audioclip also! (on java8) – profPlum Aug 24 '20 at 16:58

2 Answers2

2

Try adding player.setStartTime(new Duration(0)); before player.play()

davidchoo12
  • 1,261
  • 15
  • 26
0

I solved the problem. I had to put player.stop(); before the player.start(); call.

¯\_(ツ)_/¯ 
Darano
  • 11
  • 1