1

I have the following code. No sound is being played I have no idea what I am doing wrong. I have a file called "Test" in the directory specified. It is of .mp3 format.

@Override
public void start(Stage stage) throws Exception
{
    Media sound = new Media("file:///C:/Users/name/Music/HQ/Test.mp3");
    MediaPlayer mediaPlayer = new MediaPlayer(sound);
    mediaPlayer.setAutoPlay(true);

    VBox root = new VBox();
    root.getChildren().addAll();

    Scene scene = new Scene(root, 500, 500);
    stage.setScene(scene);
    stage.show();

}

public static void main(String[] args)
{
    launch(args);
}

3 Answers3

1

I had the same problem. I wanted to play 4 min mp3 track on menu screen in my game. I've converted from mp3 to wav. Still nothing. Then I've tried shorter sound (1 sec gong sound)... and it worked! However the sound was like cut in the middle. Well, that made me suspicious, so I've decided to run this inside a Thread and it worked! Then I've switched back to primary sound and now it was playing just fine.

For now I don't understand two things:

  • why running this inside Thread helps
  • why .wav works and .mp3 doesn't

It's not much for now but maybe it will help someone :) I will update my answer as soon as I find out!

Kamil Latosinski
  • 756
  • 5
  • 28
0

You have to call mediaPlayer.play(); somewhere. setAutoPlay() only sets the autoPlay-property.

Calculator
  • 2,769
  • 1
  • 13
  • 18
0

1)If it is inside your project the .mp3 file in [resources/music/test.mp3]:

Media media = null;
try {
  media = new Media(getClass().getResource("/music/Test.mp3").toURI().toString());
} catch (URISyntaxException e) {
  e.printStackTrace();
} 

2)If it is outside the project for example on file:///C:/Users/name/Music/HQ/Test.mp3

 Media media = null;
 try {
   media = new Media("file:C:/Users/name/Music/HQ/Test.mp3");
 } catch (URISyntaxException e) {
   e.printStackTrace();
 } 

Have a look on this question also : Getting a mp3 file to play using javafx

And here how it downloads the Image it will help you.

Community
  • 1
  • 1
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • I tried what you are saying, in both cases, the program runs with no sound. I have checked "media" and it has the correct file selected as well as the correct file path. I have checked my sound, nothing is being played. – ColonCapsDee Dec 22 '16 at 23:20
  • If it helps, when i use media.getDuration(), i get "UNKNOWN" as an output. – ColonCapsDee Dec 22 '16 at 23:26
  • @ColonCapsDee The `Test.mp3` is playing well with Windows Media Player for example ? VLC will play it for sure even it is corrupted... – GOXR3PLUS Dec 22 '16 at 23:38
  • I dont have windows media player, i used VLC. I will download and check if it works on that. – ColonCapsDee Dec 22 '16 at 23:41
  • Well I cant use Windows Media Player, but it works on numerous sound players (itunes, VLC, QuickTime Player etc – ColonCapsDee Dec 22 '16 at 23:45
  • Ok, so I tested the same code out on my laptop (different PC). The sounds work, so is there something wrong with my Eclipse or PC? – ColonCapsDee Dec 23 '16 at 00:13
  • @ColonCapsDee Same Java Version installed? – GOXR3PLUS Dec 23 '16 at 00:14