S0laris_Kn1ght
S0laris_Kn1ght

Reputation: 17

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.

Upvotes: 0

Views: 429

Answers (1)

c0nD
c0nD

Reputation: 13

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.

Upvotes: 1

Related Questions