【问题标题】:spring how to send audio in reponse stream春天如何在响应流中发送音频
【发布时间】:2013-11-08 15:54:26
【问题描述】:

我有一个像这样的嵌入标签

<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" />

和url映射

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

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

【问题讨论】:

  • PlayAudio.html 是如何生成的?我在您的代码中没有看到任何映射到此 URL 的内容。

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


【解决方案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" />

【讨论】:

  • 感谢您的回复,它不起作用。请查看编辑
  • 假设 Login.html 映射正常工作,更改 src="${pageContext.request.scheme}://${pageContext.request.serverName}${pageContext.request.contextPath}/main /PlayFile.html"
【解决方案2】:

问题在于 type="audio/wav" 从嵌入标签中删除类型后我可以播放音频

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2015-02-12
    • 2019-10-17
    • 2021-10-16
    • 2013-06-07
    • 1970-01-01
    相关资源
    最近更新 更多