Mp3 Player funktioniert nicht

Diskutiere Mp3 Player funktioniert nicht im Allgemeine Java-Themen Bereich.
A

advanced_java

Habe einen Kommandozeilen Mp3 Player gebastelt:
Java:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.mpatric.mp3agic.ID3v1;
import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.Mp3File;

import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;

public class PlayerForMp3 {
	private BufferedReader bri = new BufferedReader(new InputStreamReader(System.in));
	private File[] mp3s = null;
	private int index = 0;
	private AdvancedPlayer ap = null;
	private boolean isPlaying = false;

	public PlayerForMp3() throws IOException {
		System.out.println("Bitte das Hauptverzeichnis (Root) eingeben:");
		String fn = bri.readLine();
		if (fn == null || fn.isBlank()) {
			return;
		}
		mp3s = getMp3s(new File(fn));
		play(mp3s[0]);
		// Press s for stop, b for begin, n (number) for next, p for previous or l for list:
		while (true) {
			String l = bri.readLine();
			if (l == null || l.isBlank()) {
				break;
			}
			if (isPlaying) {
				ap.stop();
			}
			switch (l.charAt(0)) {
			case 's':
				break;
			case 'b':
				play(mp3s[index]);
				break;
			case 'n':
				index = index + getI(l).orElse(1);
				play(mp3s[index]);
				break;
			case 'p':
				play(mp3s[--index]);
				break;
			case 'l':
				for (int i = 0; i < mp3s.length; i++) {
					if (i == index) {
						System.out.print(">> ");
					}
					System.out.println((i + 1) + ": " + mp3s[i].getName());
				}
				break;
			default:
				break;
			}
		}
	}

	private Optional<Integer> getI(String l) {
		Pattern p = Pattern.compile("-?\\d+");
		Matcher m = p.matcher(l);
		if (m.find()) {
			try {
				return Optional.of(Integer.parseInt(m.group()));
			} catch (NumberFormatException ignore) {
			}
		}
		return Optional.empty();
	}

	private void play(final File file) {
		isPlaying = true;
		new Thread(new Runnable() {
			public void run() {
				try (FileInputStream fis = new FileInputStream(file)) {
					ap = new AdvancedPlayer(fis);
					ap.setPlayBackListener(new PlaybackListener() {
						@Override
						public void playbackFinished(PlaybackEvent evt) {
							System.out.println("Stop playing: " + file.getName());
							isPlaying = false;
						}
					});
					System.out.println("Start playing: " + file.getName());

					Mp3File mp3file = new Mp3File(file);
					System.out.println("Length of this mp3 is: " + mp3file.getLengthInSeconds() + " seconds");
					System.out.println("Bitrate: " + mp3file.getBitrate() + " kbps " + (mp3file.isVbr() ? "(VBR)" : "(CBR)"));
					System.out.println("Sample rate: " + mp3file.getSampleRate() + " Hz");
					System.out.println("Has ID3v1 tag?: " + (mp3file.hasId3v1Tag() ? "YES" : "NO"));
					System.out.println("Has ID3v2 tag?: " + (mp3file.hasId3v2Tag() ? "YES" : "NO"));
					System.out.println("Has custom tag?: " + (mp3file.hasCustomTag() ? "YES" : "NO"));
					if (mp3file.hasId3v1Tag()) {
						ID3v1 id3v1Tag = mp3file.getId3v1Tag();
						System.out.println("Track: " + id3v1Tag.getTrack());
						System.out.println("Artist: " + id3v1Tag.getArtist());
						System.out.println("Title: " + id3v1Tag.getTitle());
						System.out.println("Album: " + id3v1Tag.getAlbum());
						System.out.println("Year: " + id3v1Tag.getYear());
						System.out.println("Genre: " + id3v1Tag.getGenre() + " (" + id3v1Tag.getGenreDescription() + ")");
						System.out.println("Comment: " + id3v1Tag.getComment());
					}
					if (mp3file.hasId3v2Tag()) {
						ID3v2 id3v2Tag = mp3file.getId3v2Tag();
						System.out.println("Track: " + id3v2Tag.getTrack());
						System.out.println("Artist: " + id3v2Tag.getArtist());
						System.out.println("Title: " + id3v2Tag.getTitle());
						System.out.println("Album: " + id3v2Tag.getAlbum());
						System.out.println("Year: " + id3v2Tag.getYear());
						System.out.println("Genre: " + id3v2Tag.getGenre() + " (" + id3v2Tag.getGenreDescription() + ")");
						System.out.println("Comment: " + id3v2Tag.getComment());
						System.out.println("Lyrics: " + id3v2Tag.getLyrics());
						System.out.println("Composer: " + id3v2Tag.getComposer());
						System.out.println("Publisher: " + id3v2Tag.getPublisher());
						System.out.println("Original artist: " + id3v2Tag.getOriginalArtist());
						System.out.println("Album artist: " + id3v2Tag.getAlbumArtist());
						System.out.println("Copyright: " + id3v2Tag.getCopyright());
						System.out.println("URL: " + id3v2Tag.getUrl());
						System.out.println("Encoder: " + id3v2Tag.getEncoder());
						byte[] albumImageData = id3v2Tag.getAlbumImage();
						if (albumImageData != null) {
							System.out.println("Have album image data, length: " + albumImageData.length + " bytes");
							System.out.println("Album image mime type: " + id3v2Tag.getAlbumImageMimeType());
						}
					}

					ap.play();
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			}
		}).start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			System.out.println(e.getMessage());
		}
		System.out.println("Press s for stop, b for begin, n (number) for next, p for previous or l for list:");
	}

	private File[] getMp3s(File mf) throws IOException {
		final ArrayList<File> list = new ArrayList<>();
		Files.walkFileTree(mf.toPath(), new SimpleFileVisitor<Path>() {

			public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
				return FileVisitResult.CONTINUE;
			}

			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				File f = file.toFile();
				if (f.isFile() && f.getName().toLowerCase().endsWith(".mp3")) {
					list.add(f);
				}
				return FileVisitResult.CONTINUE;
			}

			public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
				return FileVisitResult.CONTINUE;
			}
		});
		return list.toArray(new File[0]);
	}

	public static void main(String[] args) {
		try {
			new PlayerForMp3();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}
Folgende maven dependencies:
Code:
<!-- https://mvnrepository.com/artifact/javazoom/jlayer -->
<dependency>
	<groupId>javazoom</groupId>
	<artifactId>jlayer</artifactId>
	<version>1.0.1</version>
</dependency>

<dependency>
	<groupId>com.mpatric</groupId>
	<artifactId>mp3agic</artifactId>
	<version>0.9.1</version>
</dependency>
Das Problem ist aber, dass er nicht automatisch die nächste Mp3 abspielt, sobald ein Sound fertig ist, und außerdem zeigt er den aktuellen Abspielfortschritt nicht an. Wie kann ich das verbessern?
 
Thema: 

Mp3 Player funktioniert nicht

Passende Stellenanzeigen aus deiner Region:
Anzeige

Neue Themen

Anzeige

Anzeige
Oben