0
播放mp3流我有一些像這樣的短音頻播放器示例代碼運行正常:不能與JMF
public class AudioTest {
public static void main(String[] args) {
Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
Format input2 = new AudioFormat(AudioFormat.MPEG);
Format output = new AudioFormat(AudioFormat.LINEAR);
PlugInManager.addPlugIn(
"com.sun.media.codec.audio.mp3.JavaDecoder",
new Format[]{input1, input2},
new Format[]{output},
PlugInManager.CODEC);
try {
Player player = Manager.createPlayer(new File("tone.mp3").toURL());
player.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
現在,我想從一個servlet與流的MP3:
Manager.createPlayer(new URL("http://localhost:88/media/tone.mp3"));
的servlet收到請求後打開一個測試文件併發送給請求者。問題是mp3沒有播放(沒有聲音),也沒有任何錯誤信息。
如果我通過瀏覽器下載文件,文件播放正確。
該servlet是這樣的:
public class MediaSource extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.err.println(req);
File mp3 = new File("tone.mp3");
InputStream in = new FileInputStream(mp3);
resp.setContentType("audio/mpeg");
resp.addHeader("Content-Disposition",
"attachment; filename=" + mp3.getName());
resp.setContentLength((int) mp3.length());
ServletOutputStream out = resp.getOutputStream();
byte buf[] = new byte[1024];
int n = in.read(buf, 0, 1024);
while (n > 0) {
out.write(buf, 0, n);
out.flush();
n = in.read(buf, 0, 1024);
}
in.close();
out.close();
}
}
更新爲@jogabonito
這是
javax.media.TransitionEvent[[email protected],previous=Unrealized,current=Realizing,target=Started]
javax.media.CachingControlEvent[[email protected],[email protected]f9f9d8,progress=102400]
javax.media.DurationUpdateEvent[[email protected],[email protected]
javax.media.RealizeCompleteEvent[[email protected],previous=Realizing,current=Realized,target=Started]
javax.media.TransitionEvent[[email protected],previous=Realized,current=Prefetching,target=Started]
javax.media.CachingControlEvent[[email protected],[email protected]f9f9d8,progress=204800]
javax.media.CachingControlEvent[[email protected],[email protected]f9f9d8,progress=205889]
javax.media.PrefetchCompleteEvent[[email protected],previous=Prefetching,current=Prefetched,target=Started]
javax.media.StartEvent[[email protected],previous=Prefetched,current=Started,target=Started,[email protected],[email protected]]
javax.media.EndOfMediaEvent[[email protected],previous=Started,current=Prefetched,target=Prefetched,[email protected]]
看到我的更新... – PeterMmm 2011-01-27 12:03:49