1

I'm trying to play audio on rpi from java. I'm building a chatbot so this is for a text2speech part. I've tried directing the sound through aux, hdmi and even usb doesn't change to problem.

For USB audio I tried: this guide

I'm having alot of problems with this. Does anyone know how to play audio (wav or flac) files using rpi in a consistent way?

System information:

  • Raspberry Pi 3 model B+
  • OS version: Raspbian latest
  • Java: 1.8/8

What I've tried that doesn't work:

  • The normal Java way (works on my machine):

    try {
        final Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
    
        clip.addLineListener(new LineListener() {
            @Override
            public void update(LineEvent event) {
                if (event.getType() == LineEvent.Type.STOP)
                    clip.close();
            }
        });
    
        clip.open(AudioSystem.getAudioInputStream(queue.remove()));
    
        clip.start();
    
    } catch (Exception e) {
        e.printStackTrace();
    }
    

What I've tried that semi works:

  • Starting a process and play through omxplayer or aplay

    try {
         process = Runtime.getRuntime().exec("aplay " + file.getAbsolutePath()); // or omxplayer
    
         BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
         while ((reader.readLine()) != null) {}
    
         System.out.println("waiting");
         process.waitFor(120, TimeUnit.SECONDS); // timeout sometimes
         System.out.println("done waiting");
    
         process.destroy();
    } catch (IOException | InterruptedException e) {
         e.printStackTrace();
    }
    

    This works a couple of times and suddenly It stops playing and the process just hangs. Even if I do a killall/kill -9 on the PID and if I try to play from the terminal the same happens at this point. I have to reboot before the audio will play a couple times again. Though I do see that process.destroy() doesn't always kill the process? But besides that I can't really see why this isn't working.

Can anyone shed some light on this and possibly how to get it working?

Mat0
  • 1,165
  • 2
  • 11
  • 27
  • 1
    does it work flawlessly when u run aplay "filename" on terminal? – nafas Sep 19 '17 at 13:39
  • Yes if I have not run the program after reboot it works like it should, but then after running the above code (starting process) a couple times I need to reboot again – Mat0 Sep 19 '17 at 13:52
  • my guess is that you not closing/opening program properly, try to use ProcessBuilder class, which gives u more control over it. alternatively follow this: https://stackoverflow.com/questions/3468987/executing-another-application-from-java – nafas Sep 19 '17 at 14:15
  • Thanks I will try that and see how it goes – Mat0 Sep 19 '17 at 16:51
  • I would recommend that you use a separate thread to slurp up the output from omxplayer/aplay. Otherwise you're relying on your readLine() loop to get underway before aplay writes anything to stdout. And then you're relying on it not writing to stdout again before it finishes. I suspect that both these assumptions are dubious. – Kevin Boone Sep 20 '17 at 10:51
  • It's already on its own thread. I also tried using process builder didn't change that it hangs. I've also tried starting the process using Apaches exec framework and that also shows same issue. What I've discovered is that omxplayer is being closed/destroyed correctly each time after use and when its done I checked using "ps ax | grep "omxplayer" and that found zero processes. Then when I shutdown the Java application this is the weird part. It has started a new omxplayer process? That was also found using a "ps ax" command. Can anyone use that information to uncover was is going on here? – Mat0 Sep 22 '17 at 19:13

0 Answers0