URL thing = getClass().getResource(Song);
Media audioFile = new Media( thing.toString() );
MediaPlayer m = new MediaPlayer(audioFile);
m.setAutoPlay(true);
如果我运行此代码,我的MP3可以播放,但几秒后停止! 我不想,音频文件会停止。
这是代码
package javafxapplication1;
import javafx.scene.control.Label;
import javafx.scene.text.TextAlignment;
import javafx.scene.control.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.StageStyle;
import java.net.URL;
import javafx.application.Application;
import javafx.event.EventType;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author rodey_000
*/
public class JavaFXApplication1 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root);
stage.setTitle("Label Sample");
stage.setWidth(400);
stage.setHeight(180);
HBox hbox = new HBox();
Button button1 = new Button("Bild");
button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
button1.setTranslateX(-430);
button1.setTranslateY(30);
String T ="TroubleSeeker.mp3";
Label label1 = new Label("Bild");
label1.setFont(Font.font("Cambria", 30));
label1.setTranslateY(0);
Label label5 = new Label("User");
label5.setFont(Font.font("Cambria", 30));
label5.setTranslateY(100);
label5.setTranslateX(-300);
final TextField name = new TextField();
name.setPromptText("Enter BildName.png.");
name.setPrefColumnCount(10);
name.setText("testbild.png");
name.getText();
name.setTranslateX(-40);
final TextField name2 = new TextField();
name2.setPromptText("Enter Songname.mp3.");
name2.setPrefColumnCount(10);
name2.setText("TroubleSeeker.mp3");
name2.getText();
name2.setTranslateX(-270);
name2.setTranslateY(55);
final TextField name3 = new TextField();
name3.setPromptText("Enter User");
name3.setPrefColumnCount(10);
name3.setText("Michael");
name3.getText();
name3.setTranslateX(-440);
name3.setTranslateY(100);
button1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
Stage secondaryStage = new Stage(StageStyle.UTILITY);
Group root2 = new Group();
Scene scene2 = new Scene(root2);
String TextBild = name.getText();
String TextAufBild = name3.getText();
String Song = name2.getText();
int lenght = name3.getLength();
play(Song);
Image image = new Image(getClass().getResourceAsStream(TextBild));
double high = image.getHeight();
double widht = image.getWidth();
secondaryStage.setTitle("Label Sample");
secondaryStage.setWidth(600);
secondaryStage.setHeight(300);
Label label3 = new Label();
label3.setGraphic(new ImageView(image));
Label label4= new Label(TextAufBild);
label4.setFont(new Font("", 50));
label4.setTextAlignment(TextAlignment.JUSTIFY);
label4.setTranslateY(50);
if(lenght == 1){
label4.setTranslateX(-340);
}
else if(lenght == 2){
label4.setTranslateX(-350);
}
else if(lenght == 3){
label4.setTranslateX(-360);
}
else if(lenght == 4){
label4.setTranslateX(-370);
}
else if(lenght == 5){
label4.setTranslateX(-380);
}
else if(lenght == 6){
label4.setTranslateX(-390);
}
else if(lenght == 7){
label4.setTranslateX(-400);
}
else if(lenght == 8){
label4.setTranslateX(-410);
}
else if(lenght == 9){
label4.setTranslateX(-420);
}
else if(lenght == 10){
label4.setTranslateX(-430);
}
else if(lenght == 11){
label4.setTranslateX(-440);
}
else if(lenght == 12){
label4.setTranslateX(-450);
}
else if(lenght == 13){
label4.setTranslateX(-460);
}
else if(lenght == 14){
label4.setTranslateX(-470);
}
else if(lenght == 15){
label4.setTranslateX(-480);
}
HBox hbox2 = new HBox();
hbox2.setSpacing(10);
hbox2.getChildren().add((label3));
hbox2.getChildren().add((label4));
((Group)scene2.getRoot()).getChildren().add(hbox2);
secondaryStage.setTitle("Secondary Stage");
secondaryStage.setScene(scene2);
secondaryStage.show();
}
});
Label label2 = new Label("Song");
label2.setFont(Font.font("Cambria", 30));
label2.setTranslateY(50);
label2.setTranslateX(-58);
label2.setTranslateZ(1);
hbox.setSpacing(10);
hbox.getChildren().add(label1);
hbox.getChildren().add(label2);
hbox.getChildren().add(name);
hbox.getChildren().add((label5));
hbox.getChildren().add(name2);
hbox.getChildren().add(name3);
hbox.getChildren().add(button1);
((Group)scene.getRoot()).getChildren().add(hbox);
stage.setScene(scene);
stage.show();
}
public void play(String Song){
URL thing = getClass().getResource(Song);
Media audioFile = new Media( thing.toString() );
MediaPlayer m = new MediaPlayer(audioFile);
m.play();
}
}
答案 0 :(得分:1)
如果您想要播放整个声音文件,则必须使用MediaView。
这是一个基本的例子,文件&#34; test.mp3&#34;与该类位于同一目录中:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.net.URL;
public class MusicPlayer extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 540, 210);
primaryStage.setTitle("Music Player");
primaryStage.setScene(scene);
primaryStage.show();
final URL resource = getClass().getResource("test.mp3");
Media media = new Media(resource.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView(mediaPlayer);
((Group)scene.getRoot()).getChildren().add(mediaView);
}
}