Beispiel #1
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();
      }
    }
  }
Beispiel #2
0
 public void pauseAudio() {
   ap.pause();
 }