how to send live audio (not file!) byte array data to browser

a continuous data is added to the audioService. I see it sent like this but no sound is playing. f12 dev tools.

@GetMapping(value = "/audio/live/{id}") public StreamingResponseBody liveAudio(HttpServletRequest request, @PathVariable("id") String id) { return new StreamingResponseBody() { @Override public void writeTo(OutputStream out) throws IOException { try { try { byte[] audioBytes = audioService.getAudio(id); out.write(MyAudioUtil.convertWav(audioBytes).toByteArray()); out.flush(); } catch (InterruptedException | CloseNowException | ClientAbortException exc) { //ignore these exceptions return; } out.close(); } catch (Exception e) { LOGGER.error("blabblablab", e); return; } } //} }; } 
2

1 Answer

Add Jlayer to your dependency. on gradle

 implementation group: 'javazoom', name: 'jlayer', version: '1.0.1' 

on maven

<dependency> <groupId>javazoom</groupId> <artifactId>jlayer</artifactId> <version>1.0.1</version> </dependency> 

After adding jlayer, read your input and play with jlayer

BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(audioBytes.toByteArray())); Player player = new Player(inputStream); //player from jplayer player.play(); 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like