简体   繁体   中英

spring how to send audio in reponse stream

I have an embed tag like

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

I can able to play a file from 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();
}

is there a way to do in spring i tried

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

with the requesthandler code same as in servlet,but here not even the request received for PlayAudio.html .how can i do in Spring.

EDIT : controller

@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();
        }       

    }
}

i have Home.jsp in main folder under webcontent which has

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

and the url mapping

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

but its not working(no request is received for PlayFile.html handler)

I believe the src needs to point to a RequestMapping URL in your Controller.

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

Then reference it in the src like so:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM