Can't stop song because it has to end before other lines run












0















I just want it to be able to stop halfway through playing the song but not sure how I need to do this? Does it need to be threaded?



public class playMusic {
public static void main(String args){
try{
FileInputStream fileInputStream = new FileInputStream("*filePath*");
Player player = new Player(fileInputStream);
player.play();
player.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(JavaLayerException e){
e.printStackTrace();
}
}
}









share|improve this question

























  • are you using this javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/… ?

    – Jonathan Johx
    Nov 24 '18 at 0:08











  • yes I'm using that one

    – Owen Cook
    Nov 24 '18 at 0:19











  • Based on its methods, I think you won't be able to do it. at least you know what you want to stop on some time exact. it'd be while (!player.isComplete()) { if (player.getPosition() == 15000) player.close(); break;}

    – Jonathan Johx
    Nov 24 '18 at 0:30











  • But you could use other library that is more complete like docs.oracle.com/javafx/2/api/javafx/scene/media/… if you take a look , you will find methods like getCurrentTime() and getDurantion() so you can divide /2 and find the halfway

    – Jonathan Johx
    Nov 24 '18 at 0:33











  • okay thank you. But when I've got the song running, no other lines run so how do I keep other lines running at the same time? Sorry if it's a really obvious question but only have fairly basic knowledge

    – Owen Cook
    Nov 24 '18 at 0:52
















0















I just want it to be able to stop halfway through playing the song but not sure how I need to do this? Does it need to be threaded?



public class playMusic {
public static void main(String args){
try{
FileInputStream fileInputStream = new FileInputStream("*filePath*");
Player player = new Player(fileInputStream);
player.play();
player.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(JavaLayerException e){
e.printStackTrace();
}
}
}









share|improve this question

























  • are you using this javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/… ?

    – Jonathan Johx
    Nov 24 '18 at 0:08











  • yes I'm using that one

    – Owen Cook
    Nov 24 '18 at 0:19











  • Based on its methods, I think you won't be able to do it. at least you know what you want to stop on some time exact. it'd be while (!player.isComplete()) { if (player.getPosition() == 15000) player.close(); break;}

    – Jonathan Johx
    Nov 24 '18 at 0:30











  • But you could use other library that is more complete like docs.oracle.com/javafx/2/api/javafx/scene/media/… if you take a look , you will find methods like getCurrentTime() and getDurantion() so you can divide /2 and find the halfway

    – Jonathan Johx
    Nov 24 '18 at 0:33











  • okay thank you. But when I've got the song running, no other lines run so how do I keep other lines running at the same time? Sorry if it's a really obvious question but only have fairly basic knowledge

    – Owen Cook
    Nov 24 '18 at 0:52














0












0








0








I just want it to be able to stop halfway through playing the song but not sure how I need to do this? Does it need to be threaded?



public class playMusic {
public static void main(String args){
try{
FileInputStream fileInputStream = new FileInputStream("*filePath*");
Player player = new Player(fileInputStream);
player.play();
player.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(JavaLayerException e){
e.printStackTrace();
}
}
}









share|improve this question
















I just want it to be able to stop halfway through playing the song but not sure how I need to do this? Does it need to be threaded?



public class playMusic {
public static void main(String args){
try{
FileInputStream fileInputStream = new FileInputStream("*filePath*");
Player player = new Player(fileInputStream);
player.play();
player.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(JavaLayerException e){
e.printStackTrace();
}
}
}






java jlayer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 0:18









Daniel Loudon

678116




678116










asked Nov 23 '18 at 23:25









Owen CookOwen Cook

238




238













  • are you using this javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/… ?

    – Jonathan Johx
    Nov 24 '18 at 0:08











  • yes I'm using that one

    – Owen Cook
    Nov 24 '18 at 0:19











  • Based on its methods, I think you won't be able to do it. at least you know what you want to stop on some time exact. it'd be while (!player.isComplete()) { if (player.getPosition() == 15000) player.close(); break;}

    – Jonathan Johx
    Nov 24 '18 at 0:30











  • But you could use other library that is more complete like docs.oracle.com/javafx/2/api/javafx/scene/media/… if you take a look , you will find methods like getCurrentTime() and getDurantion() so you can divide /2 and find the halfway

    – Jonathan Johx
    Nov 24 '18 at 0:33











  • okay thank you. But when I've got the song running, no other lines run so how do I keep other lines running at the same time? Sorry if it's a really obvious question but only have fairly basic knowledge

    – Owen Cook
    Nov 24 '18 at 0:52



















  • are you using this javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/… ?

    – Jonathan Johx
    Nov 24 '18 at 0:08











  • yes I'm using that one

    – Owen Cook
    Nov 24 '18 at 0:19











  • Based on its methods, I think you won't be able to do it. at least you know what you want to stop on some time exact. it'd be while (!player.isComplete()) { if (player.getPosition() == 15000) player.close(); break;}

    – Jonathan Johx
    Nov 24 '18 at 0:30











  • But you could use other library that is more complete like docs.oracle.com/javafx/2/api/javafx/scene/media/… if you take a look , you will find methods like getCurrentTime() and getDurantion() so you can divide /2 and find the halfway

    – Jonathan Johx
    Nov 24 '18 at 0:33











  • okay thank you. But when I've got the song running, no other lines run so how do I keep other lines running at the same time? Sorry if it's a really obvious question but only have fairly basic knowledge

    – Owen Cook
    Nov 24 '18 at 0:52

















are you using this javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/… ?

– Jonathan Johx
Nov 24 '18 at 0:08





are you using this javazoom.net/javalayer/docs/docs1.0/javazoom/jl/player/… ?

– Jonathan Johx
Nov 24 '18 at 0:08













yes I'm using that one

– Owen Cook
Nov 24 '18 at 0:19





yes I'm using that one

– Owen Cook
Nov 24 '18 at 0:19













Based on its methods, I think you won't be able to do it. at least you know what you want to stop on some time exact. it'd be while (!player.isComplete()) { if (player.getPosition() == 15000) player.close(); break;}

– Jonathan Johx
Nov 24 '18 at 0:30





Based on its methods, I think you won't be able to do it. at least you know what you want to stop on some time exact. it'd be while (!player.isComplete()) { if (player.getPosition() == 15000) player.close(); break;}

– Jonathan Johx
Nov 24 '18 at 0:30













But you could use other library that is more complete like docs.oracle.com/javafx/2/api/javafx/scene/media/… if you take a look , you will find methods like getCurrentTime() and getDurantion() so you can divide /2 and find the halfway

– Jonathan Johx
Nov 24 '18 at 0:33





But you could use other library that is more complete like docs.oracle.com/javafx/2/api/javafx/scene/media/… if you take a look , you will find methods like getCurrentTime() and getDurantion() so you can divide /2 and find the halfway

– Jonathan Johx
Nov 24 '18 at 0:33













okay thank you. But when I've got the song running, no other lines run so how do I keep other lines running at the same time? Sorry if it's a really obvious question but only have fairly basic knowledge

– Owen Cook
Nov 24 '18 at 0:52





okay thank you. But when I've got the song running, no other lines run so how do I keep other lines running at the same time? Sorry if it's a really obvious question but only have fairly basic knowledge

– Owen Cook
Nov 24 '18 at 0:52












1 Answer
1






active

oldest

votes


















0














If you want the program to run two separate threads concurrently have a look at Concurrency/Threads.
Example:



class PlayerThread extends Thread {
private final Player player;

PlayerThread(Player player) {
this.player = player;
}

public void run() {
try {
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}

class Main {
public static void main(String args) throws InterruptedException {
String filename = "*filename*";
try (FileInputStream fileInputStream = new FileInputStream(filename)) {
// load the file
Player player = new Player(fileInputStream);
// start playing music in separate thread
new PlayerThread(player).start();

// wait until you want to stop the music,
// or do something else, maybe wait for user's decision
Thread.sleep(10_000); // 10 seconds
// close the player
player.close();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
}


this way one thread runs plays the music while you can interact with the other and give commands. Bear in mind, concurrency is a large and complex topic, if you never used it you may run into some troubles. Study the tutorial:



https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html






share|improve this answer
























  • Thank you very much :)

    – Owen Cook
    Nov 24 '18 at 23:39











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%2f53453842%2fcant-stop-song-because-it-has-to-end-before-other-lines-run%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you want the program to run two separate threads concurrently have a look at Concurrency/Threads.
Example:



class PlayerThread extends Thread {
private final Player player;

PlayerThread(Player player) {
this.player = player;
}

public void run() {
try {
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}

class Main {
public static void main(String args) throws InterruptedException {
String filename = "*filename*";
try (FileInputStream fileInputStream = new FileInputStream(filename)) {
// load the file
Player player = new Player(fileInputStream);
// start playing music in separate thread
new PlayerThread(player).start();

// wait until you want to stop the music,
// or do something else, maybe wait for user's decision
Thread.sleep(10_000); // 10 seconds
// close the player
player.close();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
}


this way one thread runs plays the music while you can interact with the other and give commands. Bear in mind, concurrency is a large and complex topic, if you never used it you may run into some troubles. Study the tutorial:



https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html






share|improve this answer
























  • Thank you very much :)

    – Owen Cook
    Nov 24 '18 at 23:39
















0














If you want the program to run two separate threads concurrently have a look at Concurrency/Threads.
Example:



class PlayerThread extends Thread {
private final Player player;

PlayerThread(Player player) {
this.player = player;
}

public void run() {
try {
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}

class Main {
public static void main(String args) throws InterruptedException {
String filename = "*filename*";
try (FileInputStream fileInputStream = new FileInputStream(filename)) {
// load the file
Player player = new Player(fileInputStream);
// start playing music in separate thread
new PlayerThread(player).start();

// wait until you want to stop the music,
// or do something else, maybe wait for user's decision
Thread.sleep(10_000); // 10 seconds
// close the player
player.close();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
}


this way one thread runs plays the music while you can interact with the other and give commands. Bear in mind, concurrency is a large and complex topic, if you never used it you may run into some troubles. Study the tutorial:



https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html






share|improve this answer
























  • Thank you very much :)

    – Owen Cook
    Nov 24 '18 at 23:39














0












0








0







If you want the program to run two separate threads concurrently have a look at Concurrency/Threads.
Example:



class PlayerThread extends Thread {
private final Player player;

PlayerThread(Player player) {
this.player = player;
}

public void run() {
try {
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}

class Main {
public static void main(String args) throws InterruptedException {
String filename = "*filename*";
try (FileInputStream fileInputStream = new FileInputStream(filename)) {
// load the file
Player player = new Player(fileInputStream);
// start playing music in separate thread
new PlayerThread(player).start();

// wait until you want to stop the music,
// or do something else, maybe wait for user's decision
Thread.sleep(10_000); // 10 seconds
// close the player
player.close();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
}


this way one thread runs plays the music while you can interact with the other and give commands. Bear in mind, concurrency is a large and complex topic, if you never used it you may run into some troubles. Study the tutorial:



https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html






share|improve this answer













If you want the program to run two separate threads concurrently have a look at Concurrency/Threads.
Example:



class PlayerThread extends Thread {
private final Player player;

PlayerThread(Player player) {
this.player = player;
}

public void run() {
try {
player.play();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}

class Main {
public static void main(String args) throws InterruptedException {
String filename = "*filename*";
try (FileInputStream fileInputStream = new FileInputStream(filename)) {
// load the file
Player player = new Player(fileInputStream);
// start playing music in separate thread
new PlayerThread(player).start();

// wait until you want to stop the music,
// or do something else, maybe wait for user's decision
Thread.sleep(10_000); // 10 seconds
// close the player
player.close();
} catch (JavaLayerException | IOException e) {
e.printStackTrace();
}
}


this way one thread runs plays the music while you can interact with the other and give commands. Bear in mind, concurrency is a large and complex topic, if you never used it you may run into some troubles. Study the tutorial:



https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 2:27









Milo BemMilo Bem

812418




812418













  • Thank you very much :)

    – Owen Cook
    Nov 24 '18 at 23:39



















  • Thank you very much :)

    – Owen Cook
    Nov 24 '18 at 23:39

















Thank you very much :)

– Owen Cook
Nov 24 '18 at 23:39





Thank you very much :)

– Owen Cook
Nov 24 '18 at 23:39




















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%2f53453842%2fcant-stop-song-because-it-has-to-end-before-other-lines-run%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

Costa Masnaga

Fotorealismo

Create new schema in PostgreSQL using DBeaver