How to play sound in a java program?

Asked

Viewed 7,034 times

5

I need to make a program in Java, where the user puts the password and it is displayed on a TV monitor, this program will be used in a restaurant, so that when the order is completed is issued on TV the order number and emit a "sound".

How do I include a warning sound in the program?

  • 1

    Welcome to Stackoverflow! Please explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is too wide. See Help Center How to Ask.

  • I understood that the final question "how do I include a warning sound" is what the AP really wants to know. If that’s all, it doesn’t seem like a broad question.

  • Can someone who voted to close explain what is too broad here? By the way, I voted to reopen.

2 answers

9

This is an example where the program plays a sound in the . wav format (the source is that answer in the SOEN):

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class Teste {

    public static void main(String[] args) throws Exception {

        // Carrega o arquivo de áudio (não funciona com .mp3, só .wav) 
        URL oUrl = new URL("http://www.soundjay.com/button/beep-02.wav");
        Clip oClip = AudioSystem.getClip();
        AudioInputStream oStream = AudioSystem.getAudioInputStream(oUrl);
        oClip.open(oStream);

        oClip.loop(0); // Toca uma vez
        //clip.loop(Clip.LOOP_CONTINUOUSLY); // Toca continuamente (para o caso de músicas)

        // Para a execução (senão o programa termina antes de você ouvir o som)
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(null, "Clique pra fechar!");
            }
        });
    }
}

That audio system (AudioSystem) native Java does not support other formats such as . mp3. If you have an . mp3 and cannot/want to convert it, you can try using this other option with Javafx.

0

Unfortunately I tried to use this code above and it didn’t work, I only used the loop command to repeat even if I took it from here, I found another one that worked. I’m gonna send it down like mine did.

package jogo;

import java.io.File;
import javax.sound.sampled.*;

public class AudioAcerto {

        void AudioAcerto() { //Método AudioAcerto para chamar na classe executavel.
        try {
            //URL do som que no caso esta no pendrive, mais ainda é uma fase de teste.
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("G:\\ETEC\\2º MODULO\\4 QUINTA FEIRA\\PTCC\\EtecQuiz\\Musicas\\Urban-Future.wav").getAbsoluteFile());
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
            clip.loop(Clip.LOOP_CONTINUOUSLY); //Para repetir o som.
        } catch (Exception ex) {
            System.out.println("Erro ao executar SOM!");
            ex.printStackTrace();
        }
    }
}

From here is the executable class, unless they will put the audio in the same class without creating method.

package jogo;

public class Teste { //Classe executavel

    public static void main(String[] args) throws InterruptedException { //Executável do jogo

        AudioAcerto y = new AudioAcerto(); // Chamando a classe aonde está o audio.
        y.AudioAcerto (); // Chamando o método do audio

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.