我有一个JavaFX应用程序,它大量使用不同的媒体文件来显示信息。问题是,所有这些文件都包含在.zip存档中。如何访问这些文件并进行播放?这是我当前的MediaPlayerManager类。
public class MediaPlayerManager {
private MediaPlayer mediaPlayer;
private Media media;
public void init(File file) {
media = new Media(file.toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(false);
mediaView = new MediaView(mediaPlayer);
}
public void play() {
mediaPlayer.play();
}
public void stop() {
mediaPlayer.stop();
}
public void pause() {
mediaPlayer.pause();
}
}
这是我的Controller类中应该能够在存档中找到文件并播放它的方法。
public void playbackAudioFile(String audioPath, String audioName) {
// audioPath is the path to archive which contains audioName
MediaPlayerManager audioClip = new MediaPlayerManager();
// Code missing for playing audio files inside .zip
System.out.println("Playing sound...");
audioClip.play();
System.out.println("Sound played!");
}
答案 0 :(得分:0)
借用Read directly a file within a Zip file - Java,您可以重构MediaPlayerManager
,以便它直接接受URL
的字符串形式:
public class MediaPlayerManager {
private MediaPlayer mediaPlayer;
private Media media;
public void init(String url) {
media = new Media(url);
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(false);
mediaView = new MediaView(mediaPlayer);
}
public void init(File file) {
init(file.toURI().toString());
}
// ...
}
现在做
public void playbackAudioFile(String audioPath, String audioName) {
// audioPath is the path to archive which contains audioName
MediaPlayerManager audioClip = new MediaPlayerManager();
String zipURL = new File(audioPath).toURI().toString();
String jarURL = "jar:" + zipURL + "!" +audioName ;
audioClip.init(jarURL);
System.out.println("Playing sound...");
audioClip.play();
System.out.println("Sound played!");
}