• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Follow up to playing audio code (error ';')

There is an error at the end bracket sayin ';' expected.  Have I built it the right way?

public class TestSounds extends Applet {
     private AudioClip clip, clip2;
   
     // Initialize the applet
     public void init() {
          try {
               jbInit();
          } catch (Exception e) {
               e.printStackTrace();
          }
     }

     // Component initialization
     private void jbInit() throws Exception {
         
               clip = java.applet.Applet.newAudioClip(new java.net.URL(getCodeBase(),"1000Hz.au"));
               clip2 = java.applet.Applet.newAudioClip(new java.net.URL(getCodeBase(),"2.au"));
     }
     
     private Thread thread;
   
     public void start()
     {
         clip.play();
         thread = new Thread()
           {
                     public void run()
                    {
                            try
                            {
                                   Thread.sleep(100);
                                   clip.stop();
                                 
                                   
                             }
                             catch (Exception ex)
                             {
                                  ex.printStackTrace();
                             }
      clip2.play();
      thread = new Thread()
          {
                    public void run()
                   {
                           try
                           {
                                  Thread.sleep(100);
                                  clip2.stop();

                              }catch (Exception ex)
                                       {
                                            ex.printStackTrace();
                                        }
                                    }
              };
   
           thread.start();
         }
     
         public void stop()
       {
       if (thread != null)
       thread.stop();
       }
           }
   
0
pigmentarts
Asked:
pigmentarts
  • 13
  • 11
1 Solution
 
aozarovCommented:
replace:
thread = new Thread()
           {
                     public void run()
                    {
                            try
                            {
                                   Thread.sleep(100);
                                   clip.stop();
                                 
                                   
                             }
                             catch (Exception ex)
                             {
                                  ex.printStackTrace();
                             }
      clip2.play();

with:
thread = new Thread()
           {
                     public void run()
                    {
                            try
                            {
                                   Thread.sleep(100);
                                   clip.stop();
                                 clip2.play();                                  
                             }
                             catch (Exception ex)
                             {
                                  ex.printStackTrace();
                             }
};

thread.start();
     
Also I think you want to move the other Thread logic just after clip2.play(); hence:
thread = new Thread()
           {
                     public void run()
                    {
                            try
                            {
                                   Thread.sleep(100);
                                   clip.stop();
                                 clip2.play();          
                                  Thread.sleep(100);
                                  clip2.stop();                        
                             }
                             catch (Exception ex)
                             {
                                  ex.printStackTrace();
                             }
};

thread.start();
 
0
 
aozarovCommented:
One small fix:
thread = new Thread()
           {
                     public void run()
                    {
                            try
                            {
                                   Thread.sleep(100);
                                   clip.stop();
                                 clip2.play();  

                                 // probably you want this
                                 Thread.sleep(100);
                                 clip2.stop();                                
                             }
                             catch (Exception ex)
                             {
                                  ex.printStackTrace();
                             }
                   }
       };

threas.start();
0
 
aozarovCommented:
threas.start(); -> thread.start();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pigmentartsAuthor Commented:
hehe, hang on lts see
0
 
pigmentartsAuthor Commented:
Havin trouble here, your post>>

Ive tried following you but i end up in a big mess, or do you mean to just use the last code u posted. Which I did but the second sound doesent come..unless they are still being played together.

0
 
pigmentartsAuthor Commented:
private Thread thread;
   
   
     public void stop()
{
if (thread != null)
thread.stop();
}

   
     public void start()
     {
         thread = new Thread()
            {
                      public void run()
                     {
                             try
                             {
                                    Thread.sleep(100);
                                    clip.stop();
                                  clip2.play();  
 
                                  // probably you want this
                                  Thread.sleep(100);
                                  clip2.stop();                                
                              }
                              catch (Exception ex)
                              {
                                   ex.printStackTrace();
                              }
                    }
        };
 
 thread.start();
     }
         }
     
0
 
pigmentartsAuthor Commented:
thread.stop(); also gives error '[depreciation] stop()'
0
 
pigmentartsAuthor Commented:
HOld the horses it works lol...I just had to put the time up and put in clip.play
0
 
pigmentartsAuthor Commented:
but i still get that error, its just an orange error tho..not a red one
0
 
aozarovCommented:
Yes, use my last code.

>> thread.stop(); also gives error '[depreciation] stop()'
That is not an error but rather a warning (which I think you can ignore in this case).
Thread.sleep(100); will sleep only for 100 milliseconds if you want it in seconds then multiply it by 1000


0
 
aozarovCommented:
We can change the "thread stopping" technique to remove that warning if you want...
0
 
aozarovCommented:
Also, anymore issues you have with the same logic you can add to here (no need to create a new questions ;-)
0
 
aozarovCommented:
to remove the warning you can change:

 public void stop()
       {
       if (thread != null)
             thread.interrupt();
       }

and change
 catch (Exception ex)
 {
          ex.printStackTrace();
}

to
catch (InterruptedException ignore)
{
// interrupt was called so I should stop
}
 catch (Exception ex)
 {
          ex.printStackTrace();
}
0
 
pigmentartsAuthor Commented:
ah ok..well more points the merrier ey :P
0
 
pigmentartsAuthor Commented:
That works fine...and can i add as many clips as i want to this?
0
 
aozarovCommented:
Sure...
Thread.sleep(100000);
clip2.stop();    
clip3.play();
Thread.sleep(100000);
clip3.stop();
clip4.play();
...
0
 
pigmentartsAuthor Commented:
nice one...could you start me off on how to add listener class to detect when the user clicks the mouse or presses a button while a certain clip is being playing?

causing an event to happen?

And can I then use an if & then statement to jump to another clip?
0
 
pigmentartsAuthor Commented:
an should i make a new question? ;P
0
 
aozarovCommented:
>> an should i make a new question?
No, that is fine, but I have to be off the computer for about two hours.

>> could you start me off on how to add listener class to detect when the user clicks the mouse or presses a button while a certain
// Component initialization
     private void jbInit() throws Exception {
         
               clip = java.applet.Applet.newAudioClip(new java.net.URL(getCodeBase(),"1000Hz.au"));
               clip2 = java.applet.Applet.newAudioClip(new java.net.URL(getCodeBase(),"2.au"));
               Button button = new Button("ClickMe");
               button.addActionListener(new ActionListener()
                 {
                             public void actionPerformed(ActionEvent e)
                             {
                                       // logic to handle the button click event
                           }
                 });
 
             add(button);             // add the button to the Applet
     }

>> And can I then use an if & then statement to jump to another clip?
Sure, but what exactly is no your mind?
0
 
pigmentartsAuthor Commented:
Do you mean for the final outcome?

Well, what i'd really like to do is have five clips playing different sounds. Each clip gets played at five different volumes.

But i need to store some data about the clip that was playing as the user presses a button. 2 variables
0
 
aozarovCommented:
>> Well, what i'd really like to do is have five clips playing different sounds. Each clip gets played at five different volumes.
I think for this one you have enough information.

>> But i need to store some data about the clip that was playing as the user presses a button. 2 variables
Just add to the Thread logic that stop/start clips an assigment to a variable that will hold the name of the current, no?
0
 
pigmentartsAuthor Commented:
ummm you mean assign variables to each clip?
0
 
aozarovCommented:
No, see my example in your other thread.
0
 
pigmentartsAuthor Commented:
roger
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now