Question

I need to create a JavaFx media player "code" with a "10 song" playlist and a...

I need to create a JavaFx media player "code" with a "10 song" playlist and a cover to each song. ("I am using Net beans and Eclipse to write my codes) The codes will need pause/stop/play buttons. All the bells and whistles to make a outstanding final project.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. Create an object of AudioInputStream by using AudioSystem.getAudioInputStream(File file). AudioInputStream converts an audio file into stream.
  2. Get a clip reference object from AudioSystem.
  3. Stream an audio input stream from which audio data will be read into the clip by using open() method of Clip interface.
  4. Set the required properties to the clip like frame position, loop, microsecond position.
  5. Start the clip

code:

// Java program to play an Audio

// file using Clip Object

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

  

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.Clip;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

  

public class SimpleAudioPlayer

{

  

    // to store current position

    Long currentFrame;

    Clip clip;

      

    // current status of clip

    String status;

      

    AudioInputStream audioInputStream;

    static String filePath;

  

    // constructor to initialize streams and clip

    public SimpleAudioPlayer()

        throws UnsupportedAudioFileException,

        IOException, LineUnavailableException

    {

        // create AudioInputStream object

        audioInputStream =

                AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());

          

        // create clip reference

        clip = AudioSystem.getClip();

          

        // open audioInputStream to the clip

        clip.open(audioInputStream);

          

        clip.loop(Clip.LOOP_CONTINUOUSLY);

    }

  

    public static void main(String[] args)

    {

        try

        {

            filePath = "Your path for the file";

            SimpleAudioPlayer audioPlayer =

                            new SimpleAudioPlayer();

              

            audioPlayer.play();

            Scanner sc = new Scanner(System.in);

              

            while (true)

            {

                System.out.println("1. pause");

                System.out.println("2. resume");

                System.out.println("3. restart");

                System.out.println("4. stop");

                System.out.println("5. Jump to specific time");

                int c = sc.nextInt();

                audioPlayer.gotoChoice(c);

                if (c == 4)

                break;

            }

            sc.close();

        }

          

        catch (Exception ex)

        {

            System.out.println("Error with playing sound.");

            ex.printStackTrace();

          

          }

    }

      

    // Work as the user enters his choice

      

    private void gotoChoice(int c)

            throws IOException, LineUnavailableException, UnsupportedAudioFileException

    {

        switch (c)

        {

            case 1:

                pause();

                break;

            case 2:

                resumeAudio();

                break;

            case 3:

                restart();

                break;

            case 4:

                stop();

                break;

            case 5:

                System.out.println("Enter time (" + 0 +

                ", " + clip.getMicrosecondLength() + ")");

                Scanner sc = new Scanner(System.in);

                long c1 = sc.nextLong();

                jump(c1);

                break;

      

        }

      

    }

      

    // Method to play the audio

    public void play()

    {

        //start the clip

        clip.start();

          

        status = "play";

    }

      

    // Method to pause the audio

    public void pause()

    {

        if (status.equals("paused"))

        {

            System.out.println("audio is already paused");

            return;

        }

        this.currentFrame =

        this.clip.getMicrosecondPosition();

        clip.stop();

        status = "paused";

    }

      

    // Method to resume the audio

    public void resumeAudio() throws UnsupportedAudioFileException,

                                IOException, LineUnavailableException

    {

        if (status.equals("play"))

        {

            System.out.println("Audio is already "+

            "being played");

            return;

        }

        clip.close();

        resetAudioStream();

        clip.setMicrosecondPosition(currentFrame);

        this.play();

    }

      

    // Method to restart the audio

    public void restart() throws IOException, LineUnavailableException,

                                            UnsupportedAudioFileException

    {

        clip.stop();

        clip.close();

        resetAudioStream();

        currentFrame = 0L;

        clip.setMicrosecondPosition(0);

        this.play();

    }

      

    // Method to stop the audio

    public void stop() throws UnsupportedAudioFileException,

    IOException, LineUnavailableException

    {

        currentFrame = 0L;

        clip.stop();

        clip.close();

    }

      

    // Method to jump over a specific part

    public void jump(long c) throws UnsupportedAudioFileException, IOException,

                                                        LineUnavailableException

    {

        if (c > 0 && c < clip.getMicrosecondLength())

        {

            clip.stop();

            clip.close();

            resetAudioStream();

            currentFrame = c;

            clip.setMicrosecondPosition(c);

            this.play();

        }

    }

      

    // Method to reset audio stream

    public void resetAudioStream() throws UnsupportedAudioFileException, IOException,

                                            LineUnavailableException

    {

        audioInputStream = AudioSystem.getAudioInputStream(

        new File(filePath).getAbsoluteFile());

        clip.open(audioInputStream);

        clip.loop(Clip.LOOP_CONTINUOUSLY);

    }

  

}

In above program we have used AudioInputStream which is a class in Java to read audio file as a stream. Like every stream of java if it is to be used again it has to be reset.

  • To pause the playback we have to stop the player and store the current frame in an object. So that while resuming we can use it. When resuming we just have to play again the player from the last position we left.
    clip.getMicrosecondPosition() method returns the current position of audio and clip.setMicrosecondPosition(long position) sets the current position of audio.
  • To stop the playback, you must have to close the clip otherwise it will remain open. I have also used clip.loop(Clip.LOOP_CONTINOUSLY) for testing. Because wav files are generally small so I have played mine in a loop.
Add a comment
Know the answer?
Add Answer to:
I need to create a JavaFx media player "code" with a "10 song" playlist and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need JAVA code for the following question. I am using Eclipse IDE. Ratings will be given...

    Need JAVA code for the following question. I am using Eclipse IDE. Ratings will be given to the correct answer. Send the code and screenshots of the program running. 2.) Name Formatter Create a JavaFX application that lets the user enter the following pieces of data: - The user's first name - The user's middle name - The user's last name - The user's preferred title (Mr., Mrs., Ms., Dr., and so on.) Assume the user has entered the following...

  • I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

    I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...

  • I need the create a Python code that makes a two-player Rock-Paper-Scissors game. (Hint: Ask for...

    I need the create a Python code that makes a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game) Please help me see why my "continue" is asking the players to start a new game. Here is my code; user1= input("Whats your name? ") user2= input("And your name? ") a = input("%s, do yo want to choose...

  • I need help wring a python code for my IOT class. This code will be used...

    I need help wring a python code for my IOT class. This code will be used on Tinkercad I am to create a Blinking LED and Ultrasonic Senor For this project you will be creating a distance sensor circuit with visual feedback. You will use a Ultrasonic sensor to measure the proximity. An LED will be used to indicate the proximity on an object by blinking fast to close objects, and slow for far away objects. You can use your...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good...

    I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good to me but it will not run. Please help... due in 24 hr. Problem As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. Currently, there is a test...

  • I need java code for this . The assignment requires using objects Exercise 2 of 2:...

    I need java code for this . The assignment requires using objects Exercise 2 of 2: Create a program that lets the player play a simplified game of Yahtzee. The game starts with the user throwing five 6-sided dice. The user can choose which dice to keep and which dice to re-roll. The user can re-roll a maximum of 2 times. After two times, the user has 5 dice with a certain value. The player then has to choose where...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • I need to create a bit of code for javascript which I can have random numbers...

    I need to create a bit of code for javascript which I can have random numbers generate with a certain amount of digits set by the user. and ask the user how many times they want this to happen. For example The user says they want a number with 4 digits (any number between 0 - 9999) and the user can input if they want to add, subtract, multiply, divide, or make it random 2 random numbers are generated. First...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT