我正在拼命尝试播放 zip 文件中的 mp3。 现在我正在制作一个临时文件并从 zip 中的音频文件复制数据
File songfile = File.createTempFile("audio", ".mp3");
Files.copy(zipFile.getInputStream(zipEntries.get(i)), songfile.toPath());
song = new Media(songfile.getPath());
songPlayer = new MediaPlayer(song);
songPlayer.play();
我似乎不知道下一步该做什么,使用此临时文件创建媒体的尝试失败了。
我尝试简单地使用临时文件创建一个媒体,但我想我无法弄清楚它的路径? 无论我做什么都会出错。
#1 从 ZileFile 获取音频文件
ZipFile zipFile = new ZipFile("your_zip_file.zip");
ZipEntry audioEntry = zipFile.getEntry("path_to_audio_file_inside_zip.mp3");
#2 从 ZipFile 中提取音频文件
File tempFile = File.createTempFile("audio", ".mp3");
try (InputStream inputStream = zipFile.getInputStream(audioEntry);
OutputStream outputStream = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
#3 您可能想将其保存在硬盘中。
#4 创建媒体
Media media = new Media(tempFile.toURI().toString());
#5 使用 MediaPlayer 播放