2010-08-23 69 views
2

我想在IE中嵌入默認媒體播放器播放.wav聲音文件。聲音文件位於某個HTTP位置。我無法在該播放器中發聲。如何從網頁瀏覽器中嵌入URL播放.wav文件 - Java

以下是驗證碼。

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的文件。以及爲什麼它從本地硬盤播放良好。

請幫忙。

回答

0

確保您設置了正確的響應類型。 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); 
} 

與您的代碼的問題是:如果數據不是一個單一的通話牽強is.read(),它不是追加到fileBytes而是第一個字節被覆蓋。

此外,您從響應中獲得的輸出流已經被緩衝。

+0

我也試過了。但沒有運氣。 – 2010-08-23 10:37:09

+0

檢查我的編輯。可以肯定的是,將數據也保存到一個文件(不僅僅是響應)並且嘗試播放該文件(以測試它沒有被破壞)。 – 2010-08-23 13:44:20

相關問題