java - print binary file on browser -


i developing project in jsp/servlet. have audio tag in html

<audio src=" player.jsp "/> 

player.jsp needed play custom starting duration 01:24
want player.jsp read file , print html audio can read/play it.

i wrote code. not working.

response.setheader("content-type","audio/mpeg"); string path1 = "c:\\1.mp3"; file filefilename = new file(path1);  inputstream in = new fileinputstream(path1); long length = filefilename.length();  byte[] bytes = new byte[(int) length];  int offset = 0; int numread = 0;  while (offset < bytes.length && (numread = in.read(bytes, offset, bytes.length - offset)) >= 0) {     offset += numread; }     string file = new string(bytes);     out.println(file); 

please me:

  1. make code work?
  2. how find duration of mp3 file?
  3. how start custom duration?

the easiest way make audio play custom start time use javascript api:

<audio id="player" src="player.jsp" type="audio/mpeg" data-start-time="45">     audio not supported </audio> <script type="text/javascript">     var player = document.getelementbyid('player');     player.currenttime = player.getattribute('data-start-time');     player.play(); </script> 

the script run after player loaded , set track current time 45 seconds. note moved start time custom attribute, set dynamically jstl: data-start-time="${starttimesec}.

the full clip duration can obtained javascript. in terms of previous example it's player.duration property.

finally, if want use servlet dynamic audio loading, should return output stream of bytes, not string now.

outputstream out = response.getoutputstream(); out.write(bytes, 0, numread); out.flush(); out.close(); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -