Sox在Java中调用时停止播放。

huangapple go评论121阅读模式
英文:

Sox play stopping playback when called in Java

问题

I need to play streamed audio from Java, primarily online radio stations. I used play --magic

for this, which seemed to work fine on the terminal. So I used Runtime to start the process from Java.

Process p = Runtime.getRuntime().exec(new String[]{"play", "--magic", station}, new String[]{"AUDIODEV=pcm.radsound"});
p.waitFor();

This works fine for one or two minutes but after that the sound disappears. The process still continues to run and p.waitFor(); does not return. I get no exceptions, nothing in p.getErrorStream(), no indication of something not working. I have no clue what's going wrong here, specially after I went back and checked that calling the same command from the terminal just keeps on playing indefinitely.

I thought maybe it has something to do with getting the streamed data, so I split the thing in two. Used curl to fill a pipe and then play to play it. Didn't change a thing.

For clarification: This has to run on a RaspberryPi 4B running Raspbery Pi OS.

Any help and/or pointers very appreciated. Thank you

英文:

I need to play streamed audio from Java, primarily online radio stations. I used play --magic

for this, which seemed to work fine on the terminal. So I used Runtime to start the process from Java.

Process p = Runtime.getRuntime().exec(new String[]{"play", "--magic", station}, new String[]{"AUDIODEV=pcm.radsound"});
p.waitFor();

This works fine for one or two minutes but after that the sound disappears. The process still continues to run and p.waitFor(); does not return. I get no exceptions, nothing in p.getErrorStream(), no indication of something not working. I have no clue what's going wrong here, specially after I went back and checked that calling the same command from the terminal just keeps on playing indefinitely.

I thought maybe it has something to do with getting the streamed data, so I split the thing in two. Used curl to fill a pipe and then play to play it. Didn't change a thing.

For clarification: This has to run on a RaspberryPi 4B running Raspbery Pi OS.

Any help and/or pointers very appreciated. Thank you

答案1

得分: 1

为确保进程不会因为完整的标准输出或标准错误缓冲区而停滞,使用以下代码是个好主意:

ProcessBuilder builder = new ProcessBuilder("play", "--magic", station);
builder.inheritIO();
builder.environment().put("AUDIODEV", "pcm.radsound");
Process p = builder.start();

ProcessBuilder是Runtime.exec的现代替代品。重要的部分是inheritIO调用,使子进程使用调用程序的输入/输出/错误描述符。

英文:

To be certain that the process is not stuck because of a full stdout or stderr buffer, it’s a good idea to use:

ProcessBuilder builder = new ProcessBuilder("play, "--magic", station);
builder.inheritIO();
builder.environment().put("AUDIODEV", "pcm.radsound");
Process p = builder.start();

ProcessBuilder is the modern replacement for Runtime.exec. The important part is the inheritIO call, which makes the child process use the calling program’s input/output/error descriptors.

huangapple
  • 本文由 发表于 2023年5月10日 20:28:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定