java - How to partially stream an mp3 file -


i'm trying partially stream mp3 file, bytes before requested "byte-mark" stil being downloaded:

  • let's assume mp3 file 7000000 bytes in filesize.
  • i "jump" 6000000 bytes , begin streaming there end.
  • but notice every byte 1-5999999 being downloaded before mp3 file being played 6000000 byte mark.
  • i using jlayer (java zoom - http://www.javazoom.net/javalayer/javalayer.html) play mp3 file.

    import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstream; import java.net.malformedurlexception; import java.net.url; import java.net.urlconnection; import javazoom.jl.decoder.javalayerexception; import javazoom.jl.player.advanced.advancedplayer;  try {     url url = new url("http://somerandomsite.com/audiotestfile.mp3");     urlconnection connection = url.openconnection();     connection.connect();      int filesize = connection.getcontentlength();     inputstream inputstream = url.openstream();      system.out.println("filesize in bytes: " + filesize); // lets assume filesize of mp3 file 7000000 bytes      long skippedbytes = inputstream.skip(6000000); // skip 6000000 bytes stream file partially      system.out.println("skipped bytes: " + skippedbytes); // skipped bytes equal 6000000 bytes, previous bytes still being downloaded.      advancedplayer ap = new advancedplayer(inputstream);     ap.play(); } catch (filenotfoundexception | javalayerexception e) {     system.out.println(e.getmessage()); } 

how stream partially?

i think want have server send byte 6000000 onwards, code above downloading entire stream , ignoring or 'skipping' first 6000000 bytes, have observed.

this because inputstream.skip reads in , ignores bytes tell skip. documentation inputstream skip:

the skip method of class creates byte array , repeatedly reads until n bytes have been read or end of stream has been reached.

what think want request server starts streaming 6000001'th byte in mp3 file. 1 way use concept called 'byte serving':

  • your server indicates can accept request portion of file using accepts-ranges response header
  • the client sends request server particular portion of file using range request header
  • the server responds requested portion of file

there example of message flow , forth there server in answer - in case mp4 file approach same: https://stackoverflow.com/a/8507991/334402


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -