JavaFX2 play wav in background










0















So I'm trying to play indefinitly a song on a background thread, but when the music ends it does not loop as it was supose to.



Tried the offered solution but yet no joy! Here is the code for the main class, hope this helps in the resolution of the issue.



Even tried to loop the thread, but no joy...



Not sure why it's ending after playing the full file once, but not sure how to solve it!



Here is the code I have. Any help is welcome



 public class Main extends Application {
Media sugar = new Media(this.getClass().getResource("sounds/t1coSugar.wav").toExternalForm());

/**
* @param args the command line arguments
*/
public static void main(String args)
Application.launch(Main.class, args);


@Override
public void start(Stage primaryStage)

primaryStage.setTitle("pacman");
primaryStage.setWidth(MazeData.calcGridX(MazeData.GRID_SIZE_X + 2)); //stage size x
primaryStage.setHeight(MazeData.calcGridY(MazeData.GRID_SIZE_Y + 5)); //stage size y

//splash screen

//end of splash screen

final Group root = new Group();
final Scene scene = new Scene(root);
root.getChildren().add(new Maze());
primaryStage.setScene(scene);
primaryStage.show();
int playbackgroundmusic = playbackgroundmusic();



private int playbackgroundmusic()

Runnable task = new Runnable()
@Override
public void run()
playSugar(); //method of the music

;
// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();
return 0;



public void playSugar()

MediaPlayer mediaplayer = new MediaPlayer(sugar);
mediaplayer.volumeProperty().setValue(0.4);
mediaplayer.setStartTime(Duration.seconds(0));
mediaplayer.setStopTime(Duration.seconds(67));
mediaplayer.setAutoPlay(true);
mediaplayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaplayer.play();











share|improve this question
























  • stackoverflow.com/questions/23498376

    – Robert Harvey
    Nov 15 '18 at 22:02











  • Possible duplicate of [A]How to make a MP3 repeat in javafx?

    – Sedrick
    Nov 15 '18 at 22:05











  • Not working, tried before posting :(

    – Antonio Cunha Santos
    Nov 15 '18 at 22:17















0















So I'm trying to play indefinitly a song on a background thread, but when the music ends it does not loop as it was supose to.



Tried the offered solution but yet no joy! Here is the code for the main class, hope this helps in the resolution of the issue.



Even tried to loop the thread, but no joy...



Not sure why it's ending after playing the full file once, but not sure how to solve it!



Here is the code I have. Any help is welcome



 public class Main extends Application {
Media sugar = new Media(this.getClass().getResource("sounds/t1coSugar.wav").toExternalForm());

/**
* @param args the command line arguments
*/
public static void main(String args)
Application.launch(Main.class, args);


@Override
public void start(Stage primaryStage)

primaryStage.setTitle("pacman");
primaryStage.setWidth(MazeData.calcGridX(MazeData.GRID_SIZE_X + 2)); //stage size x
primaryStage.setHeight(MazeData.calcGridY(MazeData.GRID_SIZE_Y + 5)); //stage size y

//splash screen

//end of splash screen

final Group root = new Group();
final Scene scene = new Scene(root);
root.getChildren().add(new Maze());
primaryStage.setScene(scene);
primaryStage.show();
int playbackgroundmusic = playbackgroundmusic();



private int playbackgroundmusic()

Runnable task = new Runnable()
@Override
public void run()
playSugar(); //method of the music

;
// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();
return 0;



public void playSugar()

MediaPlayer mediaplayer = new MediaPlayer(sugar);
mediaplayer.volumeProperty().setValue(0.4);
mediaplayer.setStartTime(Duration.seconds(0));
mediaplayer.setStopTime(Duration.seconds(67));
mediaplayer.setAutoPlay(true);
mediaplayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaplayer.play();











share|improve this question
























  • stackoverflow.com/questions/23498376

    – Robert Harvey
    Nov 15 '18 at 22:02











  • Possible duplicate of [A]How to make a MP3 repeat in javafx?

    – Sedrick
    Nov 15 '18 at 22:05











  • Not working, tried before posting :(

    – Antonio Cunha Santos
    Nov 15 '18 at 22:17













0












0








0








So I'm trying to play indefinitly a song on a background thread, but when the music ends it does not loop as it was supose to.



Tried the offered solution but yet no joy! Here is the code for the main class, hope this helps in the resolution of the issue.



Even tried to loop the thread, but no joy...



Not sure why it's ending after playing the full file once, but not sure how to solve it!



Here is the code I have. Any help is welcome



 public class Main extends Application {
Media sugar = new Media(this.getClass().getResource("sounds/t1coSugar.wav").toExternalForm());

/**
* @param args the command line arguments
*/
public static void main(String args)
Application.launch(Main.class, args);


@Override
public void start(Stage primaryStage)

primaryStage.setTitle("pacman");
primaryStage.setWidth(MazeData.calcGridX(MazeData.GRID_SIZE_X + 2)); //stage size x
primaryStage.setHeight(MazeData.calcGridY(MazeData.GRID_SIZE_Y + 5)); //stage size y

//splash screen

//end of splash screen

final Group root = new Group();
final Scene scene = new Scene(root);
root.getChildren().add(new Maze());
primaryStage.setScene(scene);
primaryStage.show();
int playbackgroundmusic = playbackgroundmusic();



private int playbackgroundmusic()

Runnable task = new Runnable()
@Override
public void run()
playSugar(); //method of the music

;
// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();
return 0;



public void playSugar()

MediaPlayer mediaplayer = new MediaPlayer(sugar);
mediaplayer.volumeProperty().setValue(0.4);
mediaplayer.setStartTime(Duration.seconds(0));
mediaplayer.setStopTime(Duration.seconds(67));
mediaplayer.setAutoPlay(true);
mediaplayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaplayer.play();











share|improve this question
















So I'm trying to play indefinitly a song on a background thread, but when the music ends it does not loop as it was supose to.



Tried the offered solution but yet no joy! Here is the code for the main class, hope this helps in the resolution of the issue.



Even tried to loop the thread, but no joy...



Not sure why it's ending after playing the full file once, but not sure how to solve it!



Here is the code I have. Any help is welcome



 public class Main extends Application {
Media sugar = new Media(this.getClass().getResource("sounds/t1coSugar.wav").toExternalForm());

/**
* @param args the command line arguments
*/
public static void main(String args)
Application.launch(Main.class, args);


@Override
public void start(Stage primaryStage)

primaryStage.setTitle("pacman");
primaryStage.setWidth(MazeData.calcGridX(MazeData.GRID_SIZE_X + 2)); //stage size x
primaryStage.setHeight(MazeData.calcGridY(MazeData.GRID_SIZE_Y + 5)); //stage size y

//splash screen

//end of splash screen

final Group root = new Group();
final Scene scene = new Scene(root);
root.getChildren().add(new Maze());
primaryStage.setScene(scene);
primaryStage.show();
int playbackgroundmusic = playbackgroundmusic();



private int playbackgroundmusic()

Runnable task = new Runnable()
@Override
public void run()
playSugar(); //method of the music

;
// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();
return 0;



public void playSugar()

MediaPlayer mediaplayer = new MediaPlayer(sugar);
mediaplayer.volumeProperty().setValue(0.4);
mediaplayer.setStartTime(Duration.seconds(0));
mediaplayer.setStopTime(Duration.seconds(67));
mediaplayer.setAutoPlay(true);
mediaplayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaplayer.play();








javafx javafx-2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 22:21







Antonio Cunha Santos

















asked Nov 15 '18 at 21:59









Antonio Cunha SantosAntonio Cunha Santos

4614




4614












  • stackoverflow.com/questions/23498376

    – Robert Harvey
    Nov 15 '18 at 22:02











  • Possible duplicate of [A]How to make a MP3 repeat in javafx?

    – Sedrick
    Nov 15 '18 at 22:05











  • Not working, tried before posting :(

    – Antonio Cunha Santos
    Nov 15 '18 at 22:17

















  • stackoverflow.com/questions/23498376

    – Robert Harvey
    Nov 15 '18 at 22:02











  • Possible duplicate of [A]How to make a MP3 repeat in javafx?

    – Sedrick
    Nov 15 '18 at 22:05











  • Not working, tried before posting :(

    – Antonio Cunha Santos
    Nov 15 '18 at 22:17
















stackoverflow.com/questions/23498376

– Robert Harvey
Nov 15 '18 at 22:02





stackoverflow.com/questions/23498376

– Robert Harvey
Nov 15 '18 at 22:02













Possible duplicate of [A]How to make a MP3 repeat in javafx?

– Sedrick
Nov 15 '18 at 22:05





Possible duplicate of [A]How to make a MP3 repeat in javafx?

– Sedrick
Nov 15 '18 at 22:05













Not working, tried before posting :(

– Antonio Cunha Santos
Nov 15 '18 at 22:17





Not working, tried before posting :(

– Antonio Cunha Santos
Nov 15 '18 at 22:17












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328490%2fjavafx2-play-wav-in-background%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328490%2fjavafx2-play-wav-in-background%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric