I am doing a little program in Java on a Raspberry pi 3 Jessie, and I am trying to use omxplayer to play a sound from inside the java program.
I have the following code:
ProcessBuilder p = new ProcessBuilder("omxplayer", "/path/to/wav");
p.redirectErrorStream(true);
Process pr = p.start();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
String line = "";
while((line = bufferedReader.readLine()) != null) {
System.out.println("Reading " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
The program will play the sound correctly, but will not output anything until it reaches the end of the wav and then prints all. However, when I launch the same command directly in a terminal, I can see text showing during the wav.
For example, when I press "+" or "-" in a terminal with omxplayer running, it changes the volume and prints "Current volume : blabla mB", but when I send through the ProcessBuilder
"+" or "-", I can hear the sound of my wav changes, but still no outputs.
It is weird because I have used ProcessBuilder
for many other uses, and have never encountered a problem as such.
Do you think the problem is with omxplayer's implementation? Or am I doing something wrong here?
EDIT:
I have tried reading the stream without the BufferedReader as such:
InputStream inputStream = pr.getInputStream();
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
int read;
while((read = inputStream.read()) >= 0) {
System.out.println("Reading " + (char)read);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
in case the omxplayer did not send any line returns, but the problem remains.
I had exactly the same problem and could fix it with the parameter -s (statistics). In this case, the player continuously puts status information and also all the start infos.