0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    That sounds quite achievable to me. Provided you never explicitly write it to file, then it will remain in memory by default. What have you tried so far? What did you get stuck on? You could also convert the blob to an input stream like so `InputStream is = resultSet.getBinaryStream("...");` and if you media player library supports an input stream then just feed it directly into the library. You could still write it to a temp file and use `file.deleteOnExit();` to delete it when the VM closes. – sorifiend Jul 19 '22 at 05:30
  • Hi , thanks for the reply , in the beginning I got the BLOB from the database using getBinaryStream(""); , but had no idea of how to have the byte data converted to mp3 and then play it, I am using javafx.media and mediaplayer library and I am not sure if it has a method that would accept InputStream data. And are you suggesting that I write it to disk and then load into memory by adding it to an array and then playing it from there , once it has loaded I use file.deleteOnExist() ? Once Again Thanks, – S0laris_Kn1ght Jul 19 '22 at 05:49
  • 1
    You need to explicitly state what api is used to play the mp3 and where you are stuck with feeding the blob data to it. What audio format is the blob? Is that already mp3 data – SpaceTrucker Jul 19 '22 at 06:14
  • @SpaceTrucker I am using jdbc driver with mysql workbench ce to store my data , the mp3 files are converted to byte stream and then pushed into the database as a MEDIUM BLOB column , the goal is to take that BLOB from the database convert it back to mp3 and then play it . I am currently using mediaplayer which is a part of javafx.media library . The BLOB is mp3 format data , which needs to be written to a file in memory location in order to play it from that memory location . The goal is to not to have to store that file(BLOB -> mp3 file) on user computer which saves space. – S0laris_Kn1ght Jul 19 '22 at 06:23
  • @SpaceTrucker I currently have no idea on how to write that byte stream data into a file in memory location and then being able to play it. – S0laris_Kn1ght Jul 19 '22 at 06:24
  • Does this answer your question? [Playing a mp3 file in a client program](https://stackoverflow.com/questions/15400231/playing-a-mp3-file-in-a-client-program) – Arun Sudhakaran Jul 19 '22 at 07:10

1 Answers1

1

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.

c0nD
  • 13
  • 1
  • 1
  • 4
  • Hi , many thanks for the reply , but I suppose you are opening a mp3 file that already exists in the file system and playing it, my goal is to get the mp3 file from the database which exists in the database as a BLOB , and then store it somewhere in memory(ofcourse converting it back to its original format because the resultset will retrieve a binaryStream object) and then being able to play it from that memory location. – S0laris_Kn1ght Jul 19 '22 at 06:31