Results 1 to 5 of 5

Thread: sound clip

  1. #1
    f_the_cook's Avatar
    f_the_cook is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default sound clip

    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!

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    10

    Default

    Frankly saying,i didn't catch your question.can you explain more your issue problem?

  3. #3
    f_the_cook's Avatar
    f_the_cook is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    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.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    10

    Default

    Java 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){}
    		
    	}
    	
    }
    another class:
    Java 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");
            }
    }

  5. #5
    f_the_cook's Avatar
    f_the_cook is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

Similar Threads

  1. How to Clip an Image
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:24 AM
  2. add sound to my applet
    By anotsu in forum Java Applets
    Replies: 4
    Last Post: 03-25-2008, 12:26 AM
  3. How do i play an audio clip from a server?
    By JavaJenius in forum Networking
    Replies: 1
    Last Post: 03-08-2008, 07:40 PM
  4. Re-playing sound
    By JSK in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-06-2008, 12:34 PM
  5. Applets having sound
    By peiceonly in forum Java Applets
    Replies: 2
    Last Post: 03-31-2007, 10:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •