无法使用 JMF 播放 mp3 流
我有一些像这样运行良好的简短音频播放器示例代码:
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();
}
}
Update for @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]
I have some short audio player example code like this that runs fine:
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();
}
}
}
Now i wanted to stream the mp3 from a servlet with:
Manager.createPlayer(new URL("http://localhost:88/media/tone.mp3"));
The servlet receives the request i open a test file and send to the requester. The problem is the mp3 is not played (no sound) and no error message at all.
If i download the file via browser the file plays correct.
The servlet goes like this:
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();
}
}
Update for @jogabonito
That are the events
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否实现 ControllerListener 接口,然后检查 ControllerEvents。然后我们可以找出玩家创建的哪个阶段失败了
Could you implement the ControllerListener interface and then check for ControllerEvents. We can then find out which stage of player creation is failing