java - How to properly set up a media player javafx -
i have created media player method javafx called upon startup (below media player). problem pauses whenever interact scroll pane dragging or zooming player pauses , not start again. why case , how may fix this(included full application if try it).
method(live code)
private static void musicplayer() { if(musiclist.peek() == null) { return; } mediaplayer mediaplayer = new mediaplayer(new media(new file(musiclist.poll()).touri().tostring())); mediaplayer.setonready(() -> { mediaplayer.play(); mediaplayer.setonendofmedia(() -> { mediaplayer.dispose(); musicplayer(); }); }); }
minimal
package minimalist; import java.io.file; import java.io.ioexception; import java.nio.file.files; import java.nio.file.paths; import java.util.linkedlist; import java.util.list; import java.util.queue; import java.util.stream.collectors; import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.scrollpane; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.scene.layout.stackpane; import javafx.scene.media.media; import javafx.scene.media.mediaplayer; import javafx.stage.stage; public class minimal extends application{ private static queue<string> musiclist = new linkedlist<string>(); public static void main(string[] args) throws ioexception { list<string> result = files.find(paths.get(".\\music"), 100, (p, a) -> p.tostring().tolowercase().endswith(".mp3")) .map(path -> path.tostring()) .collect(collectors.tolist()); for(int = 0; < result.size(); a++) { musiclist.add(result.get(a)); } musicplayer(); launch(args); } @override public void start(stage primarystage) throws exception { scrollpane scrollpane = new scrollpane(); image image = new image("https://upload.wikimedia.org/wikipedia/commons/7/70/kusatma_zonaro.jpg"); imageview imageview = new imageview(image); scrollpane.setcontent(imageview); scrollpane.setpannable(true); scene scene = new scene(new stackpane(scrollpane)); primarystage.setscene(scene); primarystage.setfullscreen(true); primarystage.show(); } private static void musicplayer() { if(musiclist.peek() == null) { return; } mediaplayer mediaplayer = new mediaplayer(new media(new file(musiclist.poll()).touri().tostring())); mediaplayer.setonready(() -> { mediaplayer.play(); mediaplayer.setonendofmedia(() -> { mediaplayer.dispose(); musicplayer(); }); }); } }
this seems behave in of fragile manner.
i not recommend launching media player main
method. documentation bit lacking on threading policy mediaplayer
, since launching regular javafx application seem sensible follow usual rules , call methods on javafx application thread. on system, unable music playing way had set up.
i had sporadic issues getting start playing after changing that; guessed maybe leaving system resources tied after exiting, modified code ensure player disposed when application ended. after both changes, behaved expected. @ no point though did have issues associated user input; guess these caused launching media player main thread instead of fx application thread, , system-dependent.
here's code worked fine me. cleaned of redundancy in code (iterating through path
s convert them string
s convert file
s can convert string representation of url seems circuitous; create unnecessary list in process, etc) , renamed things abides proper naming conventations):
import java.io.ioexception; import java.nio.file.files; import java.nio.file.path; import java.nio.file.paths; import java.util.linkedlist; import java.util.queue; import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.scrollpane; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.scene.layout.stackpane; import javafx.scene.media.media; import javafx.scene.media.mediaplayer; import javafx.stage.stage; public class minimal extends application{ private queue<path> musiclist = new linkedlist<>(); private mediaplayer mediaplayer ; public static void main(string[] args) { launch(args); } @override public void start(stage primarystage) throws exception { // should have album. edit path if musical taste poor. files.find(paths.get(system.getproperty("user.home"),"music/itunes/itunes media/music/thievery corporation/saudade/"), 100, (p, a) -> p.tostring().tolowercase().endswith(".m4a")) .foreach(musiclist::add); playmusic(); scrollpane scrollpane = new scrollpane(); image image = new image("https://upload.wikimedia.org/wikipedia/commons/7/70/kusatma_zonaro.jpg"); imageview imageview = new imageview(image); scrollpane.setcontent(imageview); scrollpane.setpannable(true); scene scene = new scene(new stackpane(scrollpane)); primarystage.setscene(scene); primarystage.setfullscreen(true); primarystage.show(); } @override public void stop() { if (mediaplayer != null) { mediaplayer.stop(); mediaplayer.dispose(); } } private void playmusic() { if(musiclist.peek() == null) { return; } mediaplayer = new mediaplayer(new media(musiclist.poll().touri().tostring())); mediaplayer.setonready(() -> { mediaplayer.play(); mediaplayer.setonendofmedia(() -> { mediaplayer.dispose(); playmusic(); }); }); } }
Comments
Post a Comment