Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:42:22+00:00 2026-05-30T13:42:22+00:00

I’ve created a Control Bar/Menu Bar/Media Player which will open up an initial MP3

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:42:23+00:00Added an answer on May 30, 2026 at 1:42 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.