Tocar arquivo wav

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!!

Tinha esquecido, precisa desses arquivos para rodar…

SoundLoader.java

[code]
import java.applet.;
import javax.swing.
;
import java.net.URL;
import java.net.MalformedURLException;

class SoundLoader {
SoundList soundList;
URL completeURL;
String relativeURL;

public SoundLoader(SoundList soundList,
                   URL baseURL, String relativeURL) {
    this.soundList = soundList;
    try {
        completeURL = new URL(baseURL, relativeURL);
    } catch (MalformedURLException e){
        System.err.println(e.getMessage());
    }
    this.relativeURL = relativeURL;
    AudioClip audioClip = Applet.newAudioClip(completeURL);
    soundList.putClip(audioClip, relativeURL);        
}

}[/code]

SoundList.java

[code]

import java.applet.AudioClip;
import javax.swing.*;
import java.net.URL;

/**

  • Loads and holds a bunch of audio files whose locations are specified

  • relative to a fixed base URL.
    */
    class SoundList extends java.util.Hashtable {
    JApplet applet;
    URL baseURL;

    public SoundList(URL baseURL) {
    super(1);
    this.baseURL = baseURL;
    }

    public void startLoading(String relativeURL) {
    new SoundLoader(this, baseURL, relativeURL);
    }

    public AudioClip getClip(String relativeURL) {
    return (AudioClip)get(relativeURL);
    }

    public void putClip(AudioClip clip, String relativeURL) {
    put(relativeURL, clip);
    }
    }[/code]

Depois de muito procurar… achei a resposta procurando no google!!! ai vai…

import java.io.*;
import javax.sound.sampled.*;
public class soundPLAY {
	
	private AudioFormat format;
	private byte[] samples;
	
	public soundPLAY(String filename){
		try{
			AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filename));
			format = stream.getFormat();
			samples = getSamples(stream);
		}
		catch (UnsupportedAudioFileException e){
			e.printStackTrace();
	}
	catch (IOException e){
			e.printStackTrace();
		}
	}
	
	public byte[] getSamples(){
		return samples;
	}
	
	public byte[] getSamples(AudioInputStream stream){
		int length = (int)(stream.getFrameLength() * format.getFrameSize());
		byte[] samples = new byte[length];
		DataInputStream in = new DataInputStream(stream);
		try{
			in.readFully(samples);
		}
		catch (IOException e){
			e.printStackTrace();
		}
		return samples;
	}
	
	public void play(InputStream source){
		// 100 ms buffer for real time change to the sound stream
		int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10);
		byte[] buffer = new byte[bufferSize];
		SourceDataLine line;
		try{
			DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
			line = (SourceDataLine)AudioSystem.getLine(info);
			line.open(format, bufferSize);
		}
		catch (LineUnavailableException e){
			e.printStackTrace();
			return;
		}
		line.start();
		try{
			int numBytesRead = 0;
			while (numBytesRead != -1){
				numBytesRead = source.read(buffer, 0, buffer.length);
				if (numBytesRead != -1)
					line.write(buffer, 0, numBytesRead);
			}
		}
		catch (IOException e){
			e.printStackTrace();
		}
		line.drain();
		line.close();
	}
	
	public static void main(String[] args){
		soundPLAY player = new soundPLAY("arq.wav");
		InputStream stream = new ByteArrayInputStream(player.getSamples());
		player.play(stream);
		System.exit(0);
	}
	
}

Toca o arquivo wav… sem construir a janela da aplicação…