Simple Sound Player
Goal
The goal of this assignment is to implement a simple sound player.
The application has been partially implemented, and you need to complete the missing bits.
When you open the project, you will see the class structure depicted below.
Three of the classes are fully implemented – you do not need to modify them. One class –
Player – has not been finished, and it is your job to finish implementation of this class.
Here is a quick overview over the existing classes:
Class SoundPlayerGUI
This class presents the GUI (Graphical User Interface) of the application. That is: It draws the
main windows, the menu bar, the buttons, and all the other things you see on the screen.
However, it does not actually do anything much. If the user clicks a button, that call is just
passed on the the Player class.
Class Track
This class represents a playable audio track. Investigate it: it has methods such as play, stop,
etc. You need to call these methods to make tracks play.
Class PlayList
This represents a list of tracks that can be played. A playlist is built by specifying a folder on
disk with some audio files in it (by default the folder called audio inside the project folder). The
list will automatically load all audio files that it finds in that folder (wav, au and aiff formats
only. No MP3 – sorry.)
Class Player
This class implements the logic of the player. This is where you have to do your work.
To complete this assignment you have to do the following:
Base tasks
• Implement the method stubs (methods with missing method bodies) in the Player
class.
• The effect should be this: When a user selects a track in the list, the track information
(track name and playing duration) should be displayed in the interface.
• When a user clicks Play, the track should start playing. Stop should stop the track.
• When a user clicks Play without having selected a track, nothing should happen. (The
same goes for Stop.)
• When Play is clicked while a track is still playing, that playing track should stop and the
selected track should play from the start.
• When another track is selected in the track list while a track is playing, the playing
should stop.
• getTrackName and getTrackInfo should return an empty string (a string of length zero)
if no track has been selected.
• You must complete all comments where they are missing (class and method comments).
• To achieve this, you have to make use of the ‘playList’ variable in the Player class to
retrieve tracks, and of the Track methods to start and stop playing the tracks. For example,
the line
◦ currentTrack = playList.getTrack(2);
◦ would retrieve track number 2 from the play list and store it in a variable named
currentTrack. Test your code well. Your program should never display a runtime
error.
Challenge tasks
Implement three more methods:
• one method named getNumberOfTracksPlayed that returns the number of tracks that
have been played (i.e. that have been started) since the player was started. The return
value should be an int.
• one method named getTotalPlayedTrackLength that returns the total playing time of all
tracks that have been played (or, to be more precise: started to play. We don’t care
whether they were played completely). The result is an int.
• one method named averageTrackLength that returns the average length of the tracks
played. The result is of type double.
When you have implemented these methods, open the SoundPlayerGUI class, find the method
named showAbout, and uncomment four lines of code as indicated in the comment
labeled “CHALLENGE TASK” in the source. The sound player’s About box (Help/About...) will
then call your methods to display this information.

We are not here to solve your assignements by reading this big requirement. Paste your code here and ask for specific help.

public class Player
{
    private PlayList playList;
    private boolean isPlaying = false;
    private Track track;

    /**
     * Constructor ...
     */
    public Player()
    {
        playList = new PlayList("audio");
    }

    /**
     * Return the track collection currently loaded in this player.
     */
    public PlayList getPlayList()
    {
        return playList;
    }

    /**
     * 
     */
    public void play()
    {
        if (!isPlaying){
        this.isPlaying = true;
        track.play();
        }
    }

    /**
     * 
     */
    public void stop()
    {
        if ( isPlaying){
        this.isPlaying = false;
        track.stop();
        }
    }

    /**
     * 
     */
    public void setTrack(int trackNumber)
    {
        this.track = trackNumber;
    }

    /**
     * 
     */
    public String getTrackName()
    {
        return this.trackName;
    }

    /**
     * Return information about the currently selected track. The information
     * contains the track name and playing time, in the format
     *    track-name (playing time)
     * Return an empty string if no track is selected.
     */
    public String getTrackInfo()
    {
        return this.trackInfo;
    }
}

when I compile it says I have incompatible types, can someone help?

I'm no expert in Java.
But can you really assign an int to an instance of Track?
In the method setTrack(int trackNumber) you're assigning the argument to this.track which is an instance of Track.

And also, in the method getTrackName() you're returning this.trackName that doesn't seem to be declared anywhere.

I may be real off base here. But is that possible even for Java?

when I compile it says I have incompatible types

Please post the full text of the compiler's error messages. They have useful info in them

Edited 4 Years Ago by NormR1

Ok I am doing this wrong. I tried to compile the void play() method and I got a error message saying java.lang.NullPointerException: Null.. What do I do?

Sub-Heading Here

this is the error message. java.lang.NullPointerException
    at Player.play(Player.java:37)

Look at line 37 in Player. There is a variable on that line that has a null value. Find the variable and then backtrack to see why that variable does not have a valid non-null value.

can you explain? I don't understand what you are saying. Line 37 is the stop method

at Player.play(Player.java:37)

The error message says the exception occured at line 37 in the play() method.
Have you changed the code and moved the lines around?

I see line 37 is track.play(); and what do I do with this? Do I just delete it? because I am getting the same error message for track.stop(); in the stop method ...

Where does the variable: track get a value? It must be null for those statements to cause the NPE

it gets it's value from the track track field which is an object of the Track which has a method of play.

I only have 1 constructor for the class Player which has no parameters, is that why it has a null value?

The default value is null. If it is not assigned another value it will stay null.

Edited 4 Years Ago by NormR1

public Track(File soundFile)

public Player()
{
    playList = new PlayList("audio");
    track = new Track();
}

The syntax of an operation to create a new object is new ClassName(parameter-list)

How do I put (File soundFile) as the parameters in new Track()?

Read the API doc for the Track class to see what parameters it takes in its constructor.

The reason why you get errors you don't understand, is because you didn't bother to try and understand what was going on, you just copy-pasted the codebase out of this thread without knowing anything about what files the OP had on his system.

you didn't even bother to rename the zip-file in which the OP of the last thread zipped his project, I think you didn't even bother to take his last version, in which some extra issues were solved.

if you really want to write an audio/wav/mp3/... player in java, my suggestion is: WRITE one in java. don't just show some code you don't know nothing about and can't explain anything about, so how are we going to have some help from your side? learn java. it's not like you need to know anything but core java to create an mp3 player in java, but you do have to understand something. if it's not core java, then at least the code you are working in, and as far as I've seen, you don't know neither of them. that's a bad place to start.

Yeah you are totally right. I am taking an introductory to Software Development course and I haven't understood it as well as I wanted too.. Are you willing to help out? You are right. Instead of giving me the answers, I need pointers so I can figure things out for myself... Where do I start?

by learning the basics. how far are you in your course, and what have you handled so far?
it's no use trying to code a media player if you don't understand/know the components you'll need.
it's like giving a toddler wood, hammer and nails, and expecting him to be able to build a desk. first learn what the nail is and does (your objects), check what your hammer (your calling class/method) is or the problems you can have with it and how to deal with it (exception handling).
these are just a few topics you'll need to understand before going over to larger projects.

    I know how to write fields, contructors, methods(get and set), local variables,if/else statements, abstractions and modularization. Learning how to use a class name as a type for a variable right now. We are using a textbook called Objects First with Java using BlueJ written by Michael Kolling.. The thing is I  think I understand the material but when I have an assignment to do something I can't understand what it's asking me to do... 

right now I am getting a high 75% in the course but I feel like I am not totally understand what I am doing... I know the more I practice the better I'll understand... I just can't understand what the question is asking me and how to go about it...

we are more then half way finished the course and I don't have a good unerstanding of our material. I can pass the course but how do I move to the next course without understanding what I am being taught right now. i will definately fail.. I don't care about the grades. I just want to be confident with what I am being taught.

I feel bad for trying to get answers from strangers and taking the easy way out... I don't want that. I want to learn. Do you have any suggestions?

tell your teacher you still have questions and ask for more explanation.

Learning how to use a class name as a type for a variable right now.

that sentence allone tells me you 're not really familiar with what you're doing. you're not just using the classname, you are either instantiating a class or calling static members from class.

This question has already been answered. Start a new discussion instead.