I need a really easy way to play an mp3 file from a Java application. After some research, I found that the latest versions of Java 7 SE are packaged in JavaFX, so I decided to give it a try. This question is NOT about playing mp3 files, but about how to get JavaFX to work well.
So, in my first experiment with JavaFX, I used some code suggested in post to stackoverflow ( see here ) and created essentially the 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
{
public static void main(String args[])
{
launch(args);
}
@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");
}
}
(This is a little more complicated than my original test program: this version appeared after the offers from jewelsea made in response to an earlier question that I posted about how to run the program in general ( see here ).)
, :
javac -cp "c:\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar";..\bin -d ..\bin ..\src\progtest.java
, , .. \bin :
java -cp .;"c:\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar" progtest
. , , Ctrl-C.
- . , Java (, , , ).
, , , , , , . ( , , , .) , :
:
import javafx.application.Application;
import javafx.stage.Stage;
import Msg.*;
public class progtest2 extends Application
{
public static void main(String args[])
{
launch(args);
}
@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");
}
}
Msg, mp3 :
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;
public void passClass(Class rt)
{
classRt = rt;
}
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");
}
}
( (mutatis mutandis re ), .)
mp3 . , " Msg thread". "", , , Msg . , , . Ctrl-C, .
System.exit() , , . , . , System.exit() , .
, System.out.println( ".\n" ); System.exit(), :
System.out.print("Press ENTER to end...");
KeyIO.ReadLine();
(KeyIO - , . .)
, , .
. . ENTER, .
, - , System.exit(). , .
Urgh!
, , , ? , , , .
!