gpt4 book ai didi

java - 无法使用 JMF 播放 mp3 流

转载 作者:行者123 更新时间:2023-12-01 15:57:29 24 4
gpt4 key购买 nike

我有一些像这样的简短音频播放器示例代码,运行良好:

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]

最佳答案

您能否实现 ControllerListener 接口(interface),然后检查 ControllerEvents。然后我们可以找出玩家创建的哪个阶段失败了

关于java - 无法使用 JMF 播放 mp3 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4815807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com