import javax.swing.*; import java.awt.*; import java.util.*; // The MediaPlayer class is used to "play" tracks. When instantiated, a // graphical user interface is displayed. The interface displays the active // play list, ie the tracks present in the requested playlists. The play method // plays (actually it just pauses/waits) the track for 1/20th of the track // length in seconds. The method play is passed a single PlayList. The playlist // is cleared before each new invocation of play. public class MediaPlayer extends JFrame { // Classes to store the playlist information private JList list; private DefaultListModel listModel; // Used to display the current track name thats been played private JTextField trackName; public MediaPlayer() { super("Silent Media Player"); // The list model will have the text strings that go into the playlist display listModel = new DefaultListModel(); //Create the list and put it in a scroll pane list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setFont(list.getFont().deriveFont(Font.ITALIC)); JScrollPane listScrollPane = new JScrollPane(list); // Set a border for the playlist listScrollPane.setBorder(BorderFactory.createTitledBorder("Active Playlist")); // Set up the text field for the current track trackName = new JTextField(30); trackName.setFont(trackName.getFont().deriveFont(Font.ITALIC)); trackName.setText("Not Playing anything yet"); //Create a panel that uses FlowLayout (the default). JPanel trackPane = new JPanel(); trackPane.add(new JLabel("Currently Playing...")); trackPane.add(trackName); Container contentPane = getContentPane(); contentPane.add(listScrollPane, BorderLayout.CENTER); contentPane.add(trackPane, BorderLayout.SOUTH); this.pack(); this.setVisible(true); } // The play method has a single PlayList of tracks that had been requested to // be played. Each of these tracks is displayed in the active play list. // Playing each track corresponds to pausing (waiting) for 1/20th of the // track length before proceeding. So the total time taken in this method // is 1/20 the total ttack length of all the tracks that were on the requested // playlists. public void play(PlayList requestedPlayList){ // Remove all the entries in the earlier requested playlist listModel.clear(); // Get the new tracks ArrayList newTracks = requestedPlayList.getTracks(); // Add each track for(int tr = 0; tr < newTracks.size(); tr++){ Track track = (Track) newTracks.get(tr); int seconds = track.getTrackLength(); int mins = seconds/60; int remSeconds = seconds%60; int displayTrackNum = tr + 1; // Form the string to be printed String trackDisplay = displayTrackNum + ". " + track.getArtistName() + " -- " + track.getTrackName() + " ... " + mins + ":"; // Take care of adding a "0" before seconds if less than 10. if (remSeconds < 10) { trackDisplay += "0" + remSeconds; } else { trackDisplay += remSeconds; } // Add this track details to the list listModel.addElement(trackDisplay); } // Add each track for(int tr = 0; tr < newTracks.size(); tr++){ Track track = (Track) newTracks.get(tr); // Set the text box to to be the track name String trackName = (String) track.getTrackName(); this.trackName.setText(trackName); // Select the track in the JList list.setSelectedIndex(tr); // Sleep for about a 1/20th of the track length in songs int seconds = track.getTrackLength(); try{ Thread.sleep(50 * seconds); } catch (Exception e){ System.out.println(e.toString()); } } this.trackName.setText("Not playing anything yet"); list.setSelectedIndex(-1); } }