我有一些像这样的简短音频播放器示例代码运行良好:
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[source=com.sun.media.content.unknown.Handler@c2ea3f,previous=Unrealized,current=Realizing,target=Started]
javax.media.CachingControlEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,cachingControl=com.sun.media.protocol.DataSource$CachingControl@f9f9d8,progress=102400]
javax.media.DurationUpdateEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,duration=javax.media.Time@1decdec
javax.media.RealizeCompleteEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,previous=Realizing,current=Realized,target=Started]
javax.media.TransitionEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,previous=Realized,current=Prefetching,target=Started]
javax.media.CachingControlEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,cachingControl=com.sun.media.protocol.DataSource$CachingControl@f9f9d8,progress=204800]
javax.media.CachingControlEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,cachingControl=com.sun.media.protocol.DataSource$CachingControl@f9f9d8,progress=205889]
javax.media.PrefetchCompleteEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,previous=Prefetching,current=Prefetched,target=Started]
javax.media.StartEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,previous=Prefetched,current=Started,target=Started,mediaTime=javax.media.Time@691f36,timeBaseTime=javax.media.Time@18020cc]
javax.media.EndOfMediaEvent[source=com.sun.media.content.unknown.Handler@c2ea3f,previous=Started,current=Prefetched,target=Prefetched,mediaTime=javax.media.Time@e94e92]
答案 0 :(得分:1)
您是否可以实现ControllerListener接口,然后检查ControllerEvents。然后我们可以找出玩家创建的哪个阶段失败