JAVA Netbeans - 如何打印当前正在播放的歌曲

时间:2014-12-11 19:32:49

标签: java

我在Java中使用这个Audioplayer。 我希望能够打印出有关当前正在播放的歌曲的信息,但我似乎无法让它发挥作用。 我该怎么做? 如果您需要更多信息,请告诉我。

package BLL;

import java.util.Scanner;

public class SoundControlMenu extends Menu {

AudioPlayer p;
final String fileA = "hi";

/**
 *
 */
public SoundControlMenu() {
    super("      Sound Controls",
            "|Play              |",
            "|Pause             |",
            "|Resume            |",
            "|Stop              |",
            "|Now playing       |");
      }

 /**
 * @param option
 * @void prints the SoundControlMenu
 */
@Override
protected void doAction(int option)  {
    switch (option) {
        case 0:
            if (p != null) {
                p.stop();
            }
            break;
        case 1:
            Scanner scanner = new Scanner(System.in);
            App.Menu.getInstance().showSongs();
            System.out.println("Enter song title:");
            String chooseSong = scanner.nextLine();
            chooseSong = chooseSong.replace(" ", "");
            if (p != null) {
                p.stop();
            }
            p = new AudioPlayer(chooseSong + ".mp3");

            if (p.isValid() == false) {
                System.out.println("The audio player is not working -- " + p.getErrorMessage());
            } else {
                p.startToPlay();
            }
            break;
        case 2:
            if (p != null) {
                p.pause();
            }
            break;
        case 3:
            if (p != null) {
                p.resume();
            }
            break;
        case 4:
            if (p != null) {
                p.stop();
            }
            break;
        case 5:
            if (p == null) {
                System.out.println("No audio player exist...");
            } else if (p.isPlaying()) {
                System.out.println(fileA);
            } else {
                System.out.println("NOT playing...");
            }
    }
}

}

1 个答案:

答案 0 :(得分:0)

按照@ moskito-x的建议,

我插入fileA = chooseSong;

[...]
chooseSong = chooseSong.replace(" ", "");
fileA = chooseSong; 
[...]

哪个有效。