简体   繁体   English

如何将 mp3 文件存储在内存中,然后在 Java 中从那里播放

[英]How to store a mp3 file in memory and then play it from there in Java

I have a database that stores mp3 files in BLOB format.我有一个以 BLOB 格式存储 mp3 文件的数据库。 I would like to retrieve those BLOBs from the database and instead of writing them to the hard disk, I want to convert them, from BLOB format to an MP3 file in memory and then play it from that memory location.我想从数据库中检索这些 BLOB,而不是将它们写入硬盘,我想将它们从 BLOB 格式转换为内存中的 MP3 文件,然后从该内存位置播放它。 The whole idea of it is not to store it on local machine.它的整个想法不是将其存储在本地机器上。 Please guide me with any articles or tutorials in achieving so.请指导我实现这一目标的任何文章或教程。

From my understanding you're looking to take this mp3 file stored in BLOB and play it back as audio.据我了解,您希望将此 mp3 文件存储在 BLOB 中并将其作为音频播放。

As far as I know, something like this should work.据我所知,这样的事情应该有效。

// "Opens" up the mp3 file and stores it in memory like you said
File blobMp3File = new File("your mp3 audio filepath").getAbsoluteFile();
Clip clip = AudioSystem.getClip();
clip.open(blobMp3File);
clip.start(); // Plays the audio once
clip.close(); // Closes it whenever you're done

As mentioned in a comment, this might also work with InputStream is = resultSet.getBinaryStream("...");正如评论中提到的,这也可能适用于InputStream is = resultSet.getBinaryStream("..."); , but I have not tested this. ,但我没有对此进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM