i was wondering how i would go about putting in a sound clip to play every time someone clicked the mouse? your help is greatly appreciated!
Printable View
i was wondering how i would go about putting in a sound clip to play every time someone clicked the mouse? your help is greatly appreciated!
Frankly saying,i didn't catch your question.can you explain more your issue problem?
ok so what i am trying to do is make a gunshot sound every time someone clicks there mouse but i don't no how to do that.
another class:Code:
/**
*
* GPL License
* Author:David Baum-serjant
*
*/
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioOnMouseOverTheButton extends Thread{
//the variables for sound of pop up
private AudioInputStream ais;
private AudioFormat af;
private SourceDataLine line;
private String name;
private int frameSize,frameRate,bufSize;
File soundFile;
public AudioOnMouseOverTheButton(File soundFile)throws IOException,UnsupportedAudioFileException,LineUnavailableException{
this.soundFile=soundFile;
}
public void run(){
try{
ais=AudioSystem.getAudioInputStream(soundFile);
af=ais.getFormat();
DataLine.Info info=new DataLine.Info(SourceDataLine.class,af);
if(!AudioSystem.isLineSupported(info)){
System.out.println("Not supported");
}
frameRate=(int)af.getFrameRate();
frameSize=af.getFrameSize();
bufSize=frameRate*frameSize;
line=(SourceDataLine)AudioSystem.getLine(info);
line.open(af,bufSize);
line.start();
byte[] date=new byte[bufSize];
int bytesRead;
while((bytesRead=ais.read(date,0,date.length))!=1000){
line.write(date,0,bytesRead);
}
line.drain();
line.stop();
line.close();
date=null;
}catch(Exception e){}
}
}
Code:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sampad extends JFrame{
private JPanel p=new JPanel();
private JButton b=new JButton("Click mouse here");
public Sampad(String makesound){
super(makesound);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.getContentPane().add(p);
p.add(b);
this.setSize(200,300);
this.setVisible(true);
b.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent evt) {
try{
//creates the audio streaming
au=newAudioOnMouseOverTheButton(newFile(Sampad.class.getResource("<your wav sound>").getPath()));
au.start();
}catch(Exception ev){}
}
});
}
public static void main(String[] args){
new Sampad("Make Sound");
}
}
Thank you! i owe you big time!