英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论