jPlayer播放后台返回的语音流

工作中需要在界面播放语音,网上找了许多js插件感觉都不是太好用,直到看到这个jPlayer,很强大也方便上手,内置了许多皮肤,就决定用它了。本文将实现从java后台返回流到前端,jPlayer播放。


java后台代码:

@RequestMapping("getVoice")
	public void voice(HttpServletRequest request , HttpServletResponse response) throws IOException {
		
		String filePath = "XXX.wav";//文件路径
		response.setContentType("audio/x-wav|wav");
		ServletOutputStream out;  
		File file = new File(filePath);
		try {
			if(file.exists()) {
 			  FileInputStream inputStream = new FileInputStream(file);    
	            //通过response获取ServletOutputStream对象(out)  
	            out = response.getOutputStream();  
	  
	            int b = 0;  
	            byte[] buffer = new byte[512];  
	            while (b != -1){  
	                b = inputStream.read(buffer);  
	                //4.写到输出流(out)中  
	                out.write(buffer,0,b);  
	            }  
	            inputStream.close();  
	            out.close();  
	            out.flush(); 
			}else {
				log.info("文件不存在");
			}
             
        } catch (IOException e) {  
            e.printStackTrace();  
        }
		
	}


 
  html代码: 
  
















js代码:

$(document).ready(function(){
	$("#jquery_jplayer_1").jPlayer({
		ready: function (event) {
			$(this).jPlayer("setMedia", {
				title: "Bubble",
				wav: "http://localhost:8081/getVoice"
				
			});
		},
		swfPath: "../../plugins/jPlayer/jplayer",//swf文件存放路径
		supplied: "wav",
		wmode: "window",
		useStateClassSkin: true,
		autoBlur: false,
		smoothPlayBar: true,
		keyEnabled: true,
		remainingDuration: true,
		toggleDuration: true
	});
});

此处是播放wav,当然也支持mp3,各种视频


你可能感兴趣的:(java)