Results 1 to 7 of 7
  1. #1
    shanky_sanks is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default Problem in adding sound.

    I cannot paste the entire code since its too big.But i can provide with relevant stuff.

    Java Code:
    import java.net.*;
    import java.applet.*;
    
    class pawn extends chesspiece 
    {
    	
    	public pawn(int c)
    	{
    		player_id=c;
    		piece_code=6;
    	}
    	public int get_id()
    	{
    		return  player_id;
    	}
    	public int get_code()
    	{
    		return piece_code;
    	}
    	
    		
    	
    	public boolean validity(int x1,int y1,int x2,int y2,chesspiece p[][])
    	{
    		AudioClip au1;
    		URL codeBase = getCodeBase();
    		au1 = getAudioClip (codeBase, "sounds/pawn.WAV");
    		
    		if(player_id==0)
    		{	
    			if(p[x2][y2]==null)//for moving ahead
    			{	
    				if(y1==1)
    				{
    				if(x1==x2&&y2-y1==1||x1==x2&&y2-y1==2)
    				{
    	                   		au1.play();
    				return true;
    				}		
    				}
    				else
    				{
    				if(x1==x2&&y2-y1==1)
    					{
    					au1.play();
    					return true;	
    					}
    				}
    
    			}
    			else if(p[x2][y2]!=null)//for killing
    			{	
    				if((Math.abs(x2-x1)==1&&y2-y1==1)&&(p[x2][y2].get_id()!=p[x1][y1].get_id()))
    				{
    				au1.play();
    				return true;
    				}
    			
    			else
    				return false;
    		}
    		else
    		{
    			if(p[x2][y2]==null)
    			{	
    				if(y1==6)
    				{
    				if(x1==x2&&y1-y2==1||x1==x2&&y1-y2==2)
    				{
    				au1.play();
    				return true;	
    				}
    				}
    				else
    				{
    				if(x1==x2&&y1-y2==1)
    				{
    				au1.play();
    				return true;
    				}
    				}
    			}
    			else if(p[x2][y2]!=null)
    			{
    				if(Math.abs(x2-x1)==1&&y1-y2==1&&p[x1][y1].get_id()!=p[x2][y2].get_id())
    				{
    				au1.play();
    				return true;
    				}
    			}
    			else
    				return false ;
    		}
    		return false;
    	}
    	return false;
    }
    From what I have read on the internet I can tell that since the getCodeBase() and getAudioClip() are in a class and there are various classes so it is not able to get the functions from the applet package. It gives the following errors

    cannot find symbol method getCodeBase()
    cannot find symbol method getAudioClip(java.net.URL,java.lang.String)
    Please Help.

  2. #2
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default Sounds

    the getCodeBase() method can only be called in the main applet/Japplet. From there you would have to pass it to the class you wanna use it in. You can use:

    AudioClip ac = getAudioClip(getCodeBase(), file); //load sound file
    ac.play(); //play file, you can also have ac.stop(); & ac.loop();

    'file' is just a string with the file name stored in it- "yourSound.wav"

  3. #3
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default

    maybe you could add the sound to the top of:

    public boolean validity(int x1,int y1,int x2,int y2,chesspiece p[][],AudioClip ac)
    {

  4. #4
    shanky_sanks is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    thanx for the reply but I have one more problem that I am using this function validity a lot number of times in my code so I cant add the 'ac'.

  5. #5
    shanky_sanks is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by jamesfrize View Post
    the getCodeBase() method can only be called in the main applet/Japplet. From there you would have to pass it to the class you wanna use it in.
    Can you please be a bit precise with some example if you have or can provide.

  6. #6
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default example

    I can give you the code for my stupidly simple animation button. I use it as the enter screen for my personal website. When you click on the applet it plays back a sound and opens my main web page.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.applet.*; 
    import java.awt.event.*;
    import java.net.*;
    
    public class index extends JApplet implements ActionListener
    { 
         private MediaTracker mt;
         private Image enter;
         private Image enter2;
         private Image enter3;
         private Image enter4;
         private Image enter5;
         private Image enter6;
         private Timer timer;
         private int delay = 120;
         private int frame = 1;
         private String file = "opened.wav";
    
         public void init()  
         { 
         setBounds(0,0,640,480);  
         timer = new Timer(delay, this);
         mt = new MediaTracker(this);
         enter = getImage(getCodeBase(), "sonodrome1.jpg");
         mt.addImage(enter,1);
         enter2 = getImage(getCodeBase(), "sonodrome2.jpg");
         mt.addImage(enter2,1);
         enter3 = getImage(getCodeBase(), "sonodrome3.jpg");
         mt.addImage(enter3,1);
         enter4 = getImage(getCodeBase(), "sonodrome4.jpg");
         mt.addImage(enter4,1);
         enter5 = getImage(getCodeBase(), "sonodrome5.jpg");
         mt.addImage(enter5,1);
         enter6 = getImage(getCodeBase(), "sonodrome6.jpg");
         mt.addImage(enter6,1);
         timer.start(); 
       
              addMouseListener ////[COLOR="Red"]mouse clicks[/COLOR]
              (
                  new MouseAdapter()
                  {
                  public void mousePressed(MouseEvent me)
                  {
                  System.out.println("Mouse!"); 
                  AudioClip ac = getAudioClip(getCodeBase(), file); ///[COLOR="red"]Locate file[/COLOR]
                  ac.play(); /////[COLOR="red"]Playback[/COLOR]
                          try
                          { 
                          AppletContext a = getAppletContext();                    
                          URL u = new URL("http://jamesfrize.43host.info/sonodrome.html");
                          a.showDocument(u,"_self");
                          }
                          catch (Exception e) 
                          {
                          System.out.println("Page Url Incorrect");
                          }
                  }
                  }
              );
         
             try
             {            
             mt.waitForAll();           
             } 
              
             catch (InterruptedException  e) {}
         }
    
         public void paint(Graphics g)  
         { 
         frame = (frame + 1); 
              
                if(frame == 1)
                {
                g.drawImage(enter,0,0,this); 
                }
                else if(frame == 2)
                {
                g.drawImage(enter2,0,0,this); 
                }
                else if(frame == 3)
                {
                g.drawImage(enter3,0,0,this); 
                }
                else if(frame == 4)
                {
                g.drawImage(enter4,0,0,this); 
                }
                else if(frame == 5)
                {
                g.drawImage(enter5,0,0,this); 
                }
                else if(frame == 6)
                {
                g.drawImage(enter6,0,0,this); 
                }
                 else if(frame == 7)
                {
                g.drawImage(enter5,0,0,this); 
                }
                 else if(frame == 8)
                {
                g.drawImage(enter4,0,0,this); 
                }
                 else if(frame == 9)
                {
                g.drawImage(enter3,0,0,this); 
                }
                 else if(frame == 10)
                {
                g.drawImage(enter2,0,0,this);   
                frame = 2;
                }
         
         }
         
                public static void main (String args[])
                {
                index img1 = new index();
                img1.setVisible(true);
                }  
                
                public void actionPerformed(ActionEvent e)
                {
                repaint();
                }
    
    }
    Hope this is of some help. Here is a link to see the applet in action-Sonodrome

  7. #7
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default

    I'm not as sure about passing the audio file outside the main class. I have not had the chance to try this code on a server. But it should work like this:

    import java.awt.*;
    import javax.swing.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.net.*;

    public class index extends JApplet
    {
    private String file = "opened.wav";
    public AudioClip ac;

    public void init()
    {
    setBounds(0,0,640,480);
    ac = getAudioClip(getCodeBase(), file); ///Locate file

    addMouseListener ////mouse clicks
    (
    new MouseAdapter()
    {
    public void mousePressed(MouseEvent me)
    {
    System.out.println("Mouse!");
    play(ac);// run playback method
    }
    }
    );
    }

    public void play(AudioClip aClip) //Arguments that are required
    {
    aClip.play(); /////Playback
    }
    }

Similar Threads

  1. add sound to my applet
    By anotsu in forum Java Applets
    Replies: 4
    Last Post: 03-24-2008, 11:26 PM
  2. Re-playing sound
    By JSK in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-06-2008, 11:34 AM
  3. applet sound
    By marco in forum Java Applets
    Replies: 1
    Last Post: 09-28-2007, 10:31 PM
  4. Sound in applet Java
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 05:52 AM
  5. Applets having sound
    By peiceonly in forum Java Applets
    Replies: 2
    Last Post: 03-31-2007, 09: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
  •