如何使用javafx轻松地将mp3 / mp4文件元数据插入到TableView中? (Tableview已使用Scenebuilder构建)

时间:2017-01-25 14:05:32

标签: javafx

我正在尝试制作媒体播放器并需要将mp3文件元数据插入到TableView中。

我需要帮助从文件夹中检索数据,然后将所述数据插入TableView。要检索的数据是标题,专辑和艺术家。

如果可能,我需要一个简单的选项。重要提示:TableView已经在Scenebuilder中创建,因此它可以帮助代码连接到FXML部分。

主要功能

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {


//launching from main page
@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("main window.fxml"));
    Scene scene = new Scene(root);
    primaryStage.setTitle("WaterPlayer");
    primaryStage.setScene(scene);
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}
}

控制器

 package sample;

import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.geometry.Pos;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.VBox;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;




    public class Controller implements Initializable {

    //button actions
    @FXML
    public void AddButtonClicked(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("add window.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();
    }

    @FXML
    public void MusicButtonClicked(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("music window.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();
    }

    @FXML
    public void MovieButtonClicked(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("movie window.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();
    }

    @FXML
    public void PlaylistButtonClicked(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("playlist window.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();

    }


    @FXML
    public void BackButtonClicked(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("main window.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();
    }

    @FXML
    public void RandomButtonClicked(ActionEvent event) throws IOException {
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("");
        window.setMinWidth(100);
        Label label = new Label();
        label.setText("RANDOM:");
        Button musicButton = new Button("Music");
        Button movieButton = new Button("Movie");
        movieButton.setOnAction(e -> window.close());
        musicButton.setOnAction(e -> window.close());
        VBox layout = new VBox(10);
        layout.getChildren().addAll(label, musicButton, movieButton);
        layout.setAlignment(Pos.CENTER);
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();
    }

    @FXML
    public void SearchBar(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("search window.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();
    }

    @FXML
    public void CreateButtonClicked(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("name playlist.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();
    }

    @FXML
    public void Playlistname(ActionEvent event) throws IOException {
        Parent Main_parent = FXMLLoader.load(getClass().getResource("playlist window.fxml"));
        Scene Main_scene = new Scene(Main_parent);
        Stage Main_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Main_stage.setScene(Main_scene);
        Main_stage.show();
    }



    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}

FXML文件     

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <bottom>
      <Button fx:id="Back" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#BackButtonClicked" prefHeight="25.0" prefWidth="60.0" text="Back" BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets bottom="12.5" left="555.0" />
         </BorderPane.margin>
      </Button>
   </bottom>
   <top>
      <HBox prefHeight="50.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <ChoiceBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="150.0">
               <HBox.margin>
                  <Insets left="10.0" top="12.5" />
               </HBox.margin>
            </ChoiceBox>
            <TextField fx:id="SearchBar" onAction="#SearchBar" prefHeight="25.0" prefWidth="185.0" promptText="Search...">
               <HBox.margin>
                  <Insets left="295.0" top="12.5" />
               </HBox.margin>
            </TextField>
         </children>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
      </HBox>
   </top>
    <center>
      <ScrollPane prefHeight="196.0" prefWidth="197.0" BorderPane.alignment="CENTER">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="400.0" prefWidth="630.0">
           <children>
              <TableView fx:id="Musiclist" prefHeight="386.0" prefWidth="615.0">
                <columns>

                  <TableColumn fx:id="Title" prefWidth="219.0" text="Title">

                  </TableColumn>

                  <TableColumn fx:id="Album" prefWidth="200.0" text="Album">

                  </TableColumn>

                    <TableColumn fx:id="Author" prefWidth="219.0" text="Author">

                    </TableColumn>
                </columns>
              </TableView>

           </children>
        </AnchorPane>
    </content>
     <BorderPane.margin>
        <Insets bottom="12.5" left="10.0" right="10.0" />
     </BorderPane.margin>
  </ScrollPane>

和歌曲课程

    package sample;
public class Song {
    private String Title;


    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getAlbum() {

        return Album;
    }

    public void setAlbum(String album) {
        Album = album;
    }

    private String Album;

    public String getAuthor() {
        return Author;
    }

    public void setAuthor(String author) {
        Author = author;
    }

    private String Author;

}

1 个答案:

答案 0 :(得分:0)

如何从mp3中检索ID3元数据:使用java库提取mp3agicjaudiotagger等元数据。例如,jaudiotagger使用以下语法:

AudioFile f = AudioFileIO.read(testFile);
Tag tag = f.getTag();
AudioHeader = f.getAudioHeader();

tag.getFirst(FieldKey.ARTIST);
tag.getFirst(FieldKey.ALBUM);
tag.getFirst(FieldKey.TITLE);
tag.getFirst(FieldKey.COMMENT);
tag.getFirst(FieldKey.YEAR);

如何将数据插入到tableview中:

ObservableList<Song> songs = FXCollections.observableArrayList();
//populate your observable list here
Musiclist.setItems(songs);

调整此snipet并将其添加到fxml中,以便tableview知道它必须显示的Song类中的哪些数据。

<TableColumn fx:id="Title" prefWidth="219.0" text="Title">
    <cellValueFactory>
        <PropertyValueFactory property="Title" />
    </cellValueFactory>
</TableColumn>