简体   繁体   中英

Playing audio on Raspberry pi with java

Hi i have this code here

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Playmusic implements Runnable {
    public static void main(String[] args){
        Thread t = new Thread(new Playmusic());
        t.start();
    }   

    @Override
    public void run() {
        AudioInputStream audioIn;
        try {
            audioIn = AudioSystem.getAudioInputStream(new File("test.wav"));
            Clip clip;
            clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.start();
            Thread.sleep(clip.getMicrosecondLength()/1000);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException | InterruptedException  e1) {
            e1.printStackTrace();
        }
    }
}

to play a sound on the raspberry. But when i run it, it doesn't produce any output.
I've tested it on both Windows and Linux systems where it works.
The program does notice the file though since it sleeps for the whole duration of the sound and doesn't give me any Runtime exception.
It also can't be the speaker that's causing the problem because i can play the sound with
aplay test.wav and it gives me an output. I wanted to use the JavaFX library but it seems to be removed on the cut down java version of resbian.

This has nothing to do with Java or Raspbian... Check the RPi configuration sudo raspi-config , and ensure you are having your audio output well configured between HDMI or Jack Out. That should do the trick...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM