简体   繁体   中英

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. 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. 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.

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("..."); , but I have not tested this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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