spring如何在响应流中发送音频

时间:2013-11-08 15:54:27

标签: java spring servlets spring-mvc audio-streaming

我有一个像

这样的嵌入式代码
<embed id="player" style="height:100%;width:100%"src="../PlayAudio2" controller="true" autoplay="true" autostart="True" type="audio/wav" />

我可以从servlet doGet

播放文件
File file = new File("Z:/53611.wav");
FileInputStream fis;
byte[] buffer=null;
try {
  fis = new FileInputStream(file);
  buffer= new byte[fis.available()];
  fis.read(buffer);
  fis.close();
} catch (FileNotFoundException e) {             
  e.printStackTrace();
} catch (IOException e) {           
  e.printStackTrace();
}     
response.setContentType("audio/vnd.wave");     
try {               
  response.getOutputStream().write(buffer);            
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

有没有办法在春天尝试

  <embed id="player" style="height:100%;width:100%"src="PlayAudio.html" controller="true" autoplay="true" autostart="True" type="audio/wav" />

请求处理程序代码与servlet相同,但这里甚至没有收到PlayAudio.html的请求。我怎么能在Spring中做。

编辑: 控制器

@Controller
@RequestMapping("main")
public class ApplicationController {

  @RequestMapping(value="Login.html",method=RequestMethod.POST)
    public String showhome(){   
        return "home";
    }

  @RequestMapping(value="PlayFile.html",method=RequestMethod.GET)
    public void playAudio(HttpServletRequest request,HttpServletResponse response){
            System.out.println("--playFile");
        File file = new File("Z:/53611.wav");
            FileInputStream fis;
            byte[] buffer=null;
            try {
                fis = new FileInputStream(file);
                buffer= new byte[fis.available()];
                fis.read(buffer);
                fis.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }        


           response.setContentType("audio/vnd.wave");
        try{                
            response.getOutputStream().write(buffer);              
        } catch (IOException e) {
            e.printStackTrace();
        }       

    }
}

我在webcontent下的主文件夹中有Home.jsp,其中有

<embed id="player" style="height:100%;width:100%" src="PlayFile.html" controller="true" autoplay="true" autostart="True" type="audio/wav" />

和网址映射

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.html</url-pattern>    
  </servlet-mapping>

但它不起作用(没有收到PlayFile.html处理程序的请求)

2 个答案:

答案 0 :(得分:1)

我相信src需要指向Controller中的RequestMapping URL。

@RequestMapping(value = "/audio", method = RequestMethod.GET)  
public ModelAndView getAudio(HttpServletResponse) { ... response.getOutputStream().write(buffer); ... }

然后在src中引用它,如下所示:

<embed id="player" style="height:100%;width:100%"src="<your-context-path>/audio" controller="true" autoplay="true" autostart="True" type="audio/wav" />

答案 1 :(得分:0)

从embed标签中删除类型后,问题是使用type =“audio / wav”我可以播放音频

<embed id="player" style="height:100%;width:100%" src="PlayFile.html" controller="true" autoplay="true" autostart="True" />