javaplaybacksoxmute

Sox play stopping playback when called in Java


I need to play streamed audio from Java, primarily online radio stations. I used play --magic [url] 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


Solution

  • 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.