Results 1 to 1 of 1
  1. #1
    nicocannon is offline Member
    Join Date
    Dec 2014
    Posts
    30
    Rep Power
    0

    Question Allow seeking through streaming mp3 file

    I am making a web app that allows you to stream music content. I am able to stream the song and seek forward but as soon I seek before the current position it just stops playing. Weirdly testing this in the eclipse EE internal browser it allows seeking forward and backwards and the same with microsoft edge. This is only happening with chrome!

    Here is my servlet code:
    Java Code:
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String songName = request.getParameter("SongName");
    		songName = songName.replace("'", "");
    		FileInputStream in = null;
    		ServletOutputStream out = null;
    		//int position = (int) request.getAttribute("Pos");
    		
    		try {
    			File mp3 = new File(getServletContext().getRealPath(songName + ".mp3"));
    			in = new FileInputStream(mp3);
    			out = response.getOutputStream();
    			
    			//set Headers
    			response.setContentType("audio/mpeg");
    			response.setContentLength((int) mp3.length());
    			response.setHeader("Accept-Ranges", "bytes");
    			
    			byte[] data = new byte[(int) mp3.length()];
    			
    			in.read(data);
    			
    			out.write(data);
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			if (in != null) {
    				in.close();
    			}
    			
    			if (out != null) {
    				out.close();
    			}
    		}
    	}
    This is the html file calling this servlet:
    Java Code:
    <!DOCTYPE html>
    <html>
    <head>
    
    	<title>test</title>
    	
    	<script>
    		var position = document.getElementById('myAudio').currentTime;
    	</script>
    	</head>
    	
    	<body>
    		<audio controls id="myAudio">
    			<source src="GetSong?SongName=Core" type="audio/mpeg">
    		</audio>
    		<p></p>
    	</body>
    </html>

    I have also noticed this error when testing the code:

    ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error

    Also:

    ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
    Last edited by nicocannon; 08-30-2015 at 09:42 AM.

Similar Threads

  1. Seeking talented Java Developer
    By eXoSea in forum Jobs Discussion
    Replies: 0
    Last Post: 10-10-2013, 05:21 AM
  2. Seeking Lucene Consultant
    By evedk in forum Jobs Offered
    Replies: 0
    Last Post: 07-28-2012, 07:21 PM
  3. File Streaming in Java
    By Haroogan in forum Advanced Java
    Replies: 0
    Last Post: 01-18-2011, 08:57 PM
  4. seeking advice on using paintImmediately()
    By gib65 in forum AWT / Swing
    Replies: 1
    Last Post: 06-28-2010, 05:33 PM
  5. streaming mp3
    By arnab321 in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-11-2008, 03:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •