簡體   English   中英

如何在嵌入Web瀏覽器的URL中播放.wav文件-Java

[英]How to play .wav file from URL in web browser embed - Java

我想在IE中嵌入默認媒體播放器中播放.wav聲音文件。 聲音文件位於某個HTTP位置。 我無法在該播放器中聽到聲音。

以下是代碼。

    URL url = new URL("http://www.concidel.com/upload/myfile.wav");
        URLConnection urlc = url.openConnection();
        InputStream is = (InputStream)urlc.getInputStream();
         fileBytes = new byte[is.available()];
         while (is.read(fileBytes,0,fileBytes.length)!=-1){}
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
         out.write(fileBytes);

這是HTML的嵌入代碼。

<embed src="CallStatesTreeAction.do?ivrCallId=${requestScope.vo.callId}&agentId=${requestScope.vo.agentId}" type="application/x-mplayer2" autostart="0" playcount="1" style="width: 40%; height: 45" />
  1. 如果我用FileOutputStream編寫,則播放效果很好
  2. 如果我替換了從URL獲取文件到本地硬盤的代碼。 那么它也可以正常工作。

我不知道為什么我無法從HTTP播放文件。 以及為什么它可以從本地硬盤正常播放。

請幫忙。

確保設置正確的響應類型。 IE在這方面非常挑剔。

[編輯]您的復制循環已損壞。 試試這個代碼:

URL url = new URL("http://www.concidel.com/upload/myfile.wav");
URLConnection urlc = url.openConnection();
InputStream is = (InputStream)urlc.getInputStream();
fileBytes = new byte[is.available()];
int len;
while ( (len = is.read(fileBytes,0,fileBytes.length)) !=-1){
    response.getOutputStream.write(fileBytes, 0, len);
}

您的代碼存在的問題是:如果沒有在對fileBytes is.read()的單次調用中獲取數據,則不會將其附加到fileBytes ,而是會覆蓋第一個字節。

同樣,您從響應中獲得的輸出流已經被緩沖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM