how to use javax.sound.sampled.LineListener?

warherolion :

I have a program that will ask the user which songs they want to play out of a list of available songs and after the user selects one once the song finishes it asks the user which song they want to play again. I have been told to use line listener for this but I can't seem to figure out how to even after using the oracle docs

my code

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] pathnames;
File MusicFileChosen;
String musicDir;
boolean songComplete = false;

pathnames = ProgramMap.musicDir.list();

// Print the names of files and directories
for (int ListNum = 0; ListNum < pathnames.length; ListNum++) {
    System.out.println(ListNum + 1 + ". " + pathnames[ListNum]);
}


for (int playlistLength = 0; playlistLength < pathnames.length; playlistLength++){
    if (!songComplete) {
        System.out.println("Which Song would you like to play?");
        int musicChoice = input.nextInt();
        musicDir = ProgramMap.userDir + "\\src\\Music\\" + pathnames[musicChoice - 1];
        MusicFileChosen = new File(musicDir);
        PlaySound(MusicFileChosen, pathnames[musicChoice - 1]);

    }
}
}
public static void PlaySound(File sound, String FileName){
try{
    // Inits the Audio System
    Clip clip = AudioSystem.getClip();

    AudioInputStream AudioInput = AudioSystem.getAudioInputStream(sound);

    //Finds and accesses the clip
    clip.open(AudioInput);

    //Starts the clip
    clip.start();

    System.out.println("Now Playing " + FileName);

    clip.drain();


}catch (Exception e){
    System.out.println("Error playing music");
}

} }

Yan :

Basically one thing which you need to change is to replace this:

for (int playlistLength = 0; playlistLength < pathnames.length; playlistLength++){

to something like:

while (true) {
    System.out.println("Which Song would you like to play?");
    int musicChoice = input.nextInt();
    musicDir = ProgramMap.userDir + "\\src\\Music\\" + pathnames[musicChoice - 1];
    MusicFileChosen = new File(musicDir);
    PlaySound(MusicFileChosen, pathnames[musicChoice - 1]);
}

You can add some logic to break the loop.

Also, I would recommend changing a little bit PlaySound method:

    public static void PlaySound(File sound, String FileName) {
        try (final AudioInputStream in = getAudioInputStream(sound)) {

            final AudioFormat outFormat = getOutFormat(in.getFormat());
            Info info = new Info(SourceDataLine.class, outFormat);

            try (final SourceDataLine line =
                         (SourceDataLine) AudioSystem.getLine(info)) {

                if (line != null) {
                    line.open(outFormat);
                    line.start();
                    System.out.println("Now Playing " + FileName);
                    stream(getAudioInputStream(outFormat, in), line);
                    line.drain();
                    line.stop();
                }
            }

        } catch (UnsupportedAudioFileException
                | LineUnavailableException
                | IOException e) {
            System.err.println("Error playing music\n" + e.getMessage());
        }
    }

    private static AudioFormat getOutFormat(AudioFormat inFormat) {
        final int ch = inFormat.getChannels();
        final float rate = inFormat.getSampleRate();
        return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
    }

    private static void stream(AudioInputStream in, SourceDataLine line)
            throws IOException {
        final byte[] buffer = new byte[4096];
        for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
            line.write(buffer, 0, n);
        }
    }

It needs to play MP3 because you can face such a problem: Unknown frame size.

To add MP3 reading support to Java Sound, add the mp3plugin.jar of the JMF to the run-time classpath of the application. https://www.oracle.com/technetwork/java/javase/download-137625.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403951&siteId=1