How to automatically play audio at startup of a program?

1

I want the audio to play as soon as the software starts. If you have any suggestions for placement of the audio button in the main frame, the will ...

Here is code for review:

Main class

package Interface;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ClasseFrame cf= new ClasseFrame();
        cf.setVisible(true);

        //Chama o método para reproduzir o audio
            new TocarSom();
        }               
    }

Class to play audio

package Interface;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TocarSom extends JFrame {

    JButton tocar = new JButton(new ImageIcon("/Imagens/Blue_Bird.jpg"));

    public TocarSom() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        tocar.setBounds(480, 250, 250, 250);//(coluna,linha,comprimento,largura);
        setLocationRelativeTo(null);
        setVisible(true);

        add(tocar);
        tocar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                play("BlueBird");
            }
        });
    }

    public void play(String nomeDoAudio) {
        URL url= TocarSom.class.getResource(nomeDoAudio+".wav");
        AudioClip audio= Applet.newAudioClip(url);
        audio.play();
    }
}
    
asked by anonymous 24.11.2017 / 13:41

1 answer

2

Try this:

package Interface;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ClasseFrame cf= new ClasseFrame();
        cf.setVisible(true);

        //Chama o método para reproduzir o audio
            new TocarSom().play("nome do arquivo de audio");
        }               
    }
    
24.11.2017 / 13:58