Search code examples
javamp3media-playerjavafx

Java FX - Play new track via FileChooser Selection


I've created a Control Bar/Menu Bar/Media Player which will open up an initial MP3 but when I go to change from one track to the next, via the FileChooser object it keeps the same MP3 on the Control Bar.

If I do a mp.stop() and then set the new media for the mp and do mp.play() it will play the new track but it won't be controlled properly.

Below are my two current classes:

package gc.mediaplayer;

import java.io.File;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;

public class GCMediaPlayer extends Application {

    MediaPlayer tracks;
    MediaControl gcMediaPlayerControl;
    Label mp3Info;
    private ChangeListener<Duration> progressChangeListener;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("GC-MediaPlayer");

        Group root = new Group();
        Scene scene = new Scene(root, 1024, 768);

        File mp3File = new File("D:\\Selena.mp3");
        String initURL = null;
        try {
            initURL = mp3File.toURL().toExternalForm();
        } catch (MalformedURLException ex) {
            System.out.println("Malformed URL Exception");
        }
        System.out.println(initURL);
        Media initMedia = new Media(initURL);
        tracks = new MediaPlayer(initMedia);
        setMediaControl(tracks);
        System.out.println("Set first track");

        BorderPane border = new BorderPane();
        mp3Info = new Label("TEST");
        MenuBar menuBar = buildMenuBarWithMenus(primaryStage.widthProperty(), scene);

        border.setTop(menuBar);
        border.setCenter(mp3Info);
        border.setBottom(gcMediaPlayerControl);
        System.out.println("Printing Border");

        root.getChildren().add(border);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void setMediaControl(MediaPlayer mp3Player)
    {
        System.out.println("Setting Media Viewer");
        gcMediaPlayerControl = new MediaControl(mp3Player);
    }

     private MenuBar buildMenuBarWithMenus(final ReadOnlyDoubleProperty menuWidthProperty, final Scene rootScene)
    {
        final MenuBar menuBar= new MenuBar();

        final Menu menu1 = new Menu("File");

        MenuItem openFile = new MenuItem("Open File");
        openFile.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                FileChooser fc = new FileChooser();          
                fc.setInitialDirectory(new File("C:\\"));
                File result = fc.showOpenDialog(rootScene.getWindow());

                if(result != null) 
                {
                    try {
                        String mp3FilePath = result.toURL().toExternalForm();
                        mp3FilePath = mp3FilePath.replaceAll(" ","%20");
                        Media mediaFile = new Media(mp3FilePath);
                        System.out.println("Got New Track");
                        System.out.println("Track DIR: "+mp3FilePath);
                        setNewTrack(mediaFile);
                    } catch (MalformedURLException ex) {
                        System.out.println("Malformed URL Exception");
                    }
                 }
            }
    });
        menu1.getItems().add(openFile);
        menuBar.getMenus().add(menu1);

        final Menu menu2 = new Menu("Options");
        menuBar.getMenus().add(menu2);

        final Menu menu3 = new Menu("Help");
        menuBar.getMenus().add(menu3);

        menuBar.prefWidthProperty().bind(menuWidthProperty);

        return menuBar;
    }

     private void setNewTrack(Media mediaObjFile)
     {
        System.out.println("Stopping Old Media First");
        tracks.stop();
        System.out.println("Setting New Track");
        tracks = new MediaPlayer(mediaObjFile);
        setMediaControl(tracks);
     }
}

MediaControl class which I'm currently using:

http://docs.oracle.com/javafx/2.0/media/playercontrol.htm

Do you see anything which doesn't mesh? I'm probably just over looking something simple.

Thanks -Matt


Solution

  • You are using Java as C++. Java has no references, only pointers, so assigning gcMediaPlayerControl new value wouldn't change anything on scene.

    In more details: next line in your code adds media player to scene:

    border.setBottom(gcMediaPlayerControl);
    

    it adds one concrete object to scene.

    Your setMediaControl method changes the variable in the class. It doesn't change anything in scene.

    private void setMediaControl(MediaPlayer mp3Player)
    {
        System.out.println("Setting Media Viewer");
        gcMediaPlayerControl = new MediaControl(mp3Player);
    }
    

    Solution:

    private void setMediaControl(MediaPlayer mp3Player)
    {
        System.out.println("Setting Media Viewer");
        gcMediaPlayerControl = new MediaControl(mp3Player);
        border.setBottom(gcMediaPlayerControl);
    }