// main method for testing
  // used before integrating with gui
  public static void main(String[] args) {
    Playlist p1 = new Playlist("timer_test");
    String song = "test";

    p1.addSong("1");
    p1.addSong("2");
    p1.addSong("3");
  }
Example #2
0
 public void update(Playlist p) {
   Playlist oldP = this.p;
   this.p = p;
   // Any items on the old list that aren't on the new, stop finding
   // sources for them
   for (String streamId : oldP.getStreamIds()) {
     if (!p.getStreamIds().contains(streamId)) control.stopFindingSources(streamId, this);
   }
   updateLock.lock();
   try {
     regenEventList();
   } finally {
     updateLock.unlock();
   }
   if (canEdit || activated) activate();
 }
    public void run() {
      list.load(musicHome.getMusicDirectory().getAbsolutePath() + "/" + list.getName());
      // add all songs in Playlist to queue, refresh, and play
      for (int i = 0; i < list.getSize(); i++) {
        list.setPosition(i);
        songQueue.add(new File(list.getCurrentSong()));
      }
      musicHome.refreshQueue();
      musicPlayer.setSong(songQueue.remove(0));
      musicHome.refreshQueue();
      if (!musicPlayer.isPlaying()) {
        musicPlayer.play(-1);
      }

      t.cancel();
    }
Example #4
0
 public static PlaylistTableModel create(RobonoboFrame frame, Playlist p, boolean canEdit) {
   List<Track> trax = new ArrayList<Track>();
   for (String sid : p.getStreamIds()) {
     trax.add(frame.ctrl.getTrack(sid));
   }
   EventList<Track> el = GlazedLists.eventList(trax);
   return new PlaylistTableModel(frame, p, canEdit, el, null);
 }
Example #5
0
 /** Must only be called from inside updateLock */
 protected void regenEventList() {
   eventList.clear();
   trackIndices.clear();
   int i = 0;
   for (String sid : p.getStreamIds()) {
     eventList.add(control.getTrack(sid));
     trackIndices.put(sid, i++);
   }
 }
 /**
  * Gets a list of a user's playlists on Last.fm. Note that this method only fetches metadata
  * regarding the user's playlists. If you want to retrieve the list of tracks in a playlist use
  * {@link Playlist#fetch(String, String) Playlist.fetch()}.
  *
  * @param user The last.fm username to fetch the playlists of.
  * @param apiKey A Last.fm API key.
  * @return a list of Playlists
  */
 public static Collection<Playlist> getPlaylists(String user, String apiKey) {
   Result result = Caller.getInstance().call("user.getPlaylists", apiKey, "user", user);
   if (!result.isSuccessful()) return Collections.emptyList();
   Collection<Playlist> playlists = new ArrayList<Playlist>();
   for (DomElement element : result.getContentElement().getChildren("playlist")) {
     playlists.add(Playlist.playlistFromElement(element));
   }
   return playlists;
 }
Example #7
0
 public void activate() {
   if (!activated) {
     activated = true;
     // Make sure we've got fresh info for all our tracks
     for (String sid : p.getStreamIds()) {
       Track t = control.getTrack(sid);
       if (t instanceof CloudTrack) control.findSources(sid, this);
       trackUpdated(sid, t);
     }
   }
 }
Example #8
0
 /**
  * If any of these streams are already in this playlist, they will be removed before being added
  * in their new position
  */
 public void addStreams(List<String> streamIds, int position) {
   if (!canEdit) throw new SeekInnerCalmException();
   updateLock.lock();
   try {
     // Rather than buggering about inside our eventlist, we re-order the playlist and then just
     // re-add the whole
     // list again
     // First, scan through our playlist and remove any that are in this
     // list (they're being moved)
     for (Iterator<String> iter = p.getStreamIds().iterator(); iter.hasNext(); ) {
       String pStreamId = iter.next();
       if (streamIds.contains(pStreamId)) iter.remove();
     }
     if (position > p.getStreamIds().size()) position = p.getStreamIds().size();
     // Put the new streams in the right spot
     p.getStreamIds().addAll(position, streamIds);
     // Now regenerate our tracks
     regenEventList();
   } finally {
     updateLock.unlock();
   }
   runPlaylistUpdate();
 }
Example #9
0
 @Override
 public void deleteTracks(List<String> streamIds) {
   if (!canDelete) throw new SeekInnerCalmException();
   updateLock.lock();
   try {
     for (String sid : streamIds) {
       p.getStreamIds().remove(sid);
       control.stopFindingSources(sid, this);
     }
     regenEventList();
   } finally {
     updateLock.unlock();
   }
   runPlaylistUpdate();
 }
Example #10
0
 protected PlaylistTableModel(
     RobonoboFrame frame, Playlist p, boolean canEdit, EventList<Track> el, SortedList<Track> sl) {
   super(frame, el, sl, null);
   this.p = p;
   this.canEdit = canEdit;
   this.canDelete = canEdit;
   int i = 0;
   for (String sid : p.getStreamIds()) {
     trackIndices.put(sid, i++);
   }
   if (canEdit) {
     control
         .getExecutor()
         .execute(
             new CatchingRunnable() {
               public void doRun() throws Exception {
                 activate();
               }
             });
   }
 }
Example #11
0
 public void nuke() {
   control.removeTrackListener(this);
   for (String streamId : p.getStreamIds()) {
     control.stopFindingSources(streamId, this);
   }
 }
 // schedule list to be played at time
 ScheduledPlay(Date time, Playlist list, MusicHome mh) {
   t = new Timer();
   isPlaylist = true;
   t.schedule(new scheduledPlaylist(list, mh), time);
   System.out.println("Playlist \"" + list.getName() + "\" scheduled for: " + time);
 }
Example #13
0
  /**
   * Prompt the user for input and interpret the command recieved.
   *
   * @throws MidiUnavailableException, InvalidMidiDataException
   */
  public static void executeUserCommand()
      throws MidiUnavailableException, InvalidMidiDataException {

    while (true) {
      System.out.println();
      System.out.print("Enter User Command. (Type 'quit' to quit) ");
      System.out.println();
      InputStreamReader isr = new java.io.InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);

      try {
        String userInput = br.readLine();
        // the userInput is evaluated to different functions encased in
        // the following if blocks.
        // some functions take in user input and evaluates what the user
        // wants by separating it into different sections
        if (userInput.equals("quit")) {
          Execution.quit(player);
          Execution.writePlaylistsToFile(library);
          break;

        } else if (userInput.startsWith("create playlist") && userInput.endsWith(")")) {
          library = Execution.createPlaylist(userInput, library);
          Execution.writePlaylistsToFile(library);

        } else if (userInput.startsWith("play playlist")) {
          String[] userInputSplited = userInput.split(" ");
          String playlist = userInputSplited[2];
          if (userInputSplited.length == 4 & userInput.matches(".*\\d.*")) {
            // When input length is 4, then play specified song that
            // user inputs
            int songIndex;
            try {
              songIndex = Integer.parseInt(userInputSplited[3]) - 1;
            } catch (NumberFormatException nfe) {
              System.out.println(
                  "Please enter a valid index that is in this playlist. Please try again.");
              continue;
            }
            try {
              player = Execution.playPlaylistSong(library, player, playlist, songIndex);
            } catch (IndexOutOfBoundsException iobe) {
              System.out.println(
                  "Please enter a valid index that is in this playlist. Please try again.");
              continue;
            } catch (NullPointerException e) {
              System.out.println("Playlist does not exist. Please try again.");
              continue;
            }

          } else if (userInputSplited.length == 3 | (!userInput.matches(".*\\d.*"))) {
            // if command does not contain integer, play entire
            // playlist
            Playlist pl;
            pl = library.findPlaylist(playlist);
            // checks if playlist exists
            if (pl == null) {
              System.out.println("Playlist does not exist. Please try again.");
              continue;
            }
            for (int i = 0; i < pl.numberOfSongs(); i++) {
              // plays the playlist songs one by one
              try {
                player = Execution.playPlaylistSong(library, player, pl, i);
              } catch (IOException e) {
                System.out.println("Midi file not available!");
              }
              player.checkIfRunning();
            }
          }

        } else if (userInput.startsWith("delete playlist")) {
          library = Execution.deletePlaylist(library, userInput);

        } else if (userInput.startsWith("find songs by")) {
          library = Execution.findSongByArtist(library, userInput);
        } else if (userInput.startsWith("play song")) {

          String[] userInputSplited = userInput.split(" ");
          int songIndex;
          try {
            songIndex = Integer.parseInt(userInputSplited[2]) - 1;
          } catch (NumberFormatException nfe) {
            // the user input can only be an integer
            System.out.println("Please enter a song index!");
            continue;
          }
          try {
            player = Execution.playSong(library, player, songIndex);
          } catch (IndexOutOfBoundsException iobe) {
            System.out.println("Song does not exist, please try again.");
            continue;
          }
        } else if (userInput.startsWith("delete song")) {
          String[] userInputSplited = userInput.split(" ");
          int songIndex;
          System.out.println(
              "Are you sure you want to permanently remove this song?(yes to confirm, type anything to return to menu)");
          String confirm = br.readLine().trim();
          // additional layer of confirmation, in case of accidental
          // deletion
          if (confirm.equals("yes")) {
            try {
              songIndex = Integer.parseInt(userInputSplited[2]) - 1;
            } catch (NumberFormatException nfe) {
              System.out.println("Please enter a song index!");
              continue;
            }
            try {
              library.delSong(songIndex);
              library = Execution.readPlaylistXml(library);
              library.displayAllLibrarySongs();
              library.displayAllLibraryPlaylists();
            } catch (IndexOutOfBoundsException iobe) {
              System.out.println("Song does not exist, please try again.");
              continue;
            }
          }
        } else if (userInput.startsWith("play random")) {
          // User may enter any of the three commands that begin with
          // play random
          // each is encased in an if block
          List<Integer> indexes;
          if (userInput.equals("play random for all")) {
            int numberOfSongs = library.numberOfSongs();

            indexes = new ArrayList<Integer>();
            for (int i = 0; i < numberOfSongs; i++) {
              indexes.add(i);
            }
            player = Execution.playRandom(library, player, indexes);

          } else if (userInput.startsWith("play random") & userInput.endsWith(")")) {
            // When user wants to play specified songs randomly
            String[] userInputSplited = userInput.split(" ");
            userInputSplited =
                userInputSplited[2].substring(1, userInputSplited[2].length() - 1).split(",");
            ArrayList<String> tempList = new ArrayList<String>(Arrays.asList(userInputSplited));

            indexes = new ArrayList<Integer>(tempList.size());
            for (String i : tempList) {
              if (Integer.valueOf(i) > library.numberOfSongs()) {
                System.out.println(
                    "the song of index "
                        + Integer.valueOf(i)
                        + " does not exist in your music libaray.");
              } else {
                indexes.add(Integer.valueOf(i) - 1);
              }
            }
            player = Execution.playRandom(library, player, indexes);

          } else if (userInput.startsWith("play random playlist")) {
            String[] userInputSplited = userInput.split(" ");
            // The 4th element in list is the playlist that user
            // wants to play randomly
            String playlist = userInputSplited[3];
            Playlist pl = library.findPlaylist(playlist);
            try {
              indexes = new ArrayList<Integer>(pl.numberOfSongs());
            } catch (NullPointerException npe) {
              System.out.println("Playlist " + playlist + " does not exist. Please try again.");
              continue;
            }

            for (int n = 0; n < pl.numberOfSongs(); n++) {
              indexes.add(n);
            }
            player = Execution.playRandom(library, player, pl, indexes);
          }

        } else if (userInput.startsWith("download")) {
          String title = userInput.split("download ")[1];
          library.downloadSong(title);

        } else if (userInput.startsWith("display jcloud songs")) {
          library.displayJcloudSongs();

        } else if (userInput.startsWith("get downloaded songs")) {
          library.downloadPreviousSongs();
          library.displayAllLibrarySongs();
        } else if (userInput.equals("display songs")) {
          library.displayAllLibrarySongs();
        } else if (userInput.equals("display library")) {
          library.displayAllLibrarySongs();
          library.displayAllLibraryPlaylists();
        } else if (userInput.equals("display playlists")) {
          library.displayAllLibraryPlaylists();
          // Shows nothing when no playlist have been created
        } else if (userInput.equals("help")) {
          displayUserManual();
        } else if (userInput.contains("pause")) {
          try {
            player.pause();
          } catch (NullPointerException e) {
            continue;
          }
        } else if (userInput.contains("resume")) {
          try {
            player.resume();
          } catch (NullPointerException e) {
            continue;
          }
        } else {
          System.out.println(
              "Invalid command. Please pay attention to input format and try again!");
        }
      } catch (IOException e) {

        e.printStackTrace();
      }
    }
  }