java - JavaFX 2.1 and threads? -or- Ending a JavaFX application gracefully? -


i wanted simple way play mp3 file java application. after research found newest versions of java 7 se packaged javafx, thought try. question not playing mp3 files, getting javafx work in well-behaved way.

so, in first experiment javafx, used code suggested in post stackoverflow (see here) , created, essentially, following test program:

import javafx.application.application; import javafx.stage.stage; import javafx.scene.media.media; import javafx.scene.media.mediaplayer;  public class progtest extends application {      /*---------------------------------------------------------*\         function:  main()     \*---------------------------------------------------------*/     public static void main(string args[])     {          launch(args);      } /* end:  main() */      /*---------------------------------------------------------*\         function:  start()     \*---------------------------------------------------------*/     @override     public void start(stage stage)     {          media medmsg              =              new media(getclass().getresource("msg.mp3").toexternalform());         mediaplayer medplmsg = new mediaplayer(medmsg);         medplmsg.play();          system.out.println("here.\n");      } /* end:  start() */  } 

(this more sophisticated original test program: version came after suggestions jewelsea made in response earlier question posted getting program running @ (see here).)

to compile code used:

javac -cp "c:\program files\oracle\javafx 2.1 runtime\lib\jfxrt.jar";..\bin -d ..\bin ..\src\progtest.java 

and, run code went ..\bin directory , used:

java -cp .;"c:\program files\oracle\javafx 2.1 runtime\lib\jfxrt.jar" progtest 

this program runs , plays clip plays. however, program doesn't return command prompt unless ctrl-c.

that behavior seemed sort of thread issue. have seen sort of behavior many times working java threads (which, sadly, have real problems allowing graceful exits programs).

so, thought, perhaps if put code in own thread , end program's main thread when thread ends, things okay. (i see wasn't wholly cogent thought, anyway, thought try.) so, wrote following second version of code:

here main thread:

import javafx.application.application; import javafx.stage.stage; import msg.*;  public class progtest2 extends application {      /*---------------------------------------------------------*\         function:  main()     \*---------------------------------------------------------*/     public static void main(string args[])     {          launch(args);      } /* end:  main() */      /*---------------------------------------------------------*\         function:  start()     \*---------------------------------------------------------*/     @override     public void start(stage stage)     {          msg msgtime = new msg();         msgtime.passclass(getclass());         msgtime.start();          try         {             msgtime.join();         }         catch (interruptedexception e)         {         }          system.out.println("here.\n");      } /* end:  start() */  } 

and here code msg thread should play mp3 file:

package msg;  import keyio.*; import javafx.application.application; import javafx.scene.media.media; import javafx.scene.media.mediaplayer;  public class msg extends thread {      private class classrt = null;      /*--------------------------------------------------------*\         function:  passclass()     \*--------------------------------------------------------*/     public void passclass(class rt)     {          classrt = rt;      } /* end:  passclass() */      /*--------------------------------------------------------*\         function:  run()     \*--------------------------------------------------------*/     public void run()     {          media medmsg             = new media(classrt.getresource("msg.mp3").toexternalform());         mediaplayer medplmsg = new mediaplayer(medmsg);         medplmsg.play();          system.out.println("leaving msg thread.\n");      } /* end:  run() */  } 

(i built , ran these files same command (mutatis mutandis re filenames , classnames) above.)

the program runs , plays mp3 file. before hear file, see "leaving msg thread." , "here.", can see come msg thread , main thread respectively. but, again, program doesn't end. have hit ctrl-c return command prompt.

i put system.exit() call right @ end of main thread there see behavior be. behavior clip not heard. seemed system.exit() call ended process before clip played.

so, next tried putting following lines in between system.out.println("here.\n"); , system.exit() call made:

system.out.print("press enter end..."); keyio.readline(); 

(keyio class made doing keyboard input output simply. details not relevant here.)

i figured block main thread going forward until clip played.

but, no. instead, clip didn't play. when hit enter, program exited without playing clip.

okay, left keyboard-input blocking code in, took out system.exit() call. audio clip played, program didn't end again.

urgh!

any ideas how gracefully work in way sort of obvious 1 want work? want make call play clip, , want able end program.

thank in advance!

in first progtest class, not create window, implicitexit action on closing last window not invoked. means program keep running until call application's exit method, don't do.

to program exit after mp3 has finished playing, call mediaplayer.setonendofmedia(runnable) , in runnable callback invoke platform.exit() method.

medplmsg.setonendofmedia(new runnable() {   @override public void run() {     platform.exit();   } }); 

all other threading stuff in question not required (and doesn't solve issue anyway...). wouldn't advise using custom threading unless need (which don't).

javafx 2.2 adds following api:

platform.setimplicitexit(boolean implicitexit) 

sets implicitexit attribute specified value. if attribute true, javafx runtime implicitly shutdown when last window closed; javafx launcher call application.stop() method , terminate javafx application thread. if attribute false, application continue run after last window closed, until application calls exit(). default value true.

this should give clue problems having.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -