oi galera!
O arquivo abaixo, toca um arquivo wav qualquer. Isso ocorre ao clicar no botao play. O q to tentando fazer é tocar o arquivo, ao inicializar o Frame, ou ate mesmo sem precisar da janela!!! Tem como?
import java.applet.AudioClip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.GridBagLayout;
public class app extends JPanel
implements ActionListener,
ItemListener
{
SoundList soundList;
String umFile = "1.wav"; // arquivo a ser tocado
JButton playButton;
AudioClip onceClip, loopClip;
URL codeBase;
boolean looping = false;
String chosenFile;
public app() {
JPanel controlPanel = new JPanel();
JLabel rotulo = new JLabel("Musica");
playButton = new JButton("Play");
playButton.addActionListener(this);
controlPanel.add(rotulo);
controlPanel.add(playButton);
add(controlPanel);
startLoadingSounds();
}
public void stop() {
onceClip.stop(); //Cut short the one-time sound.
if (looping) {
loopClip.stop(); //Stop the sound loop.
}
}
public void start() {
if (looping) {
loopClip.loop(); //Restart the sound loop.
}
}
void startLoadingSounds() {
//Start asynchronous sound loading.
try {
codeBase = new URL("file:" + System.getProperty("user.dir") + "/");
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
System.out.println("Erro");
}
soundList = new SoundList(codeBase);
soundList.startLoading(umFile);
}
public void itemStateChanged(ItemEvent e){
soundList.startLoading("1.wav");
}
public void actionPerformed(ActionEvent event) {
//PLAY BUTTON
Object source = event.getSource();
if (source == playButton) {
//Try to get the AudioClip.
onceClip = soundList.getClip("1.wav");
onceClip.play(); //Play it once.
return;
}
}
public static void main(String s[]) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame("SoundApplication");
f.addWindowListener(l);
f.getContentPane().add(new app());
f.setSize(new Dimension(400,100));
f.show();
}
Desde ja obrigado!!