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

How to put example code into your program

I'm learning how to play multiple sound files (.au) but don't know how to translate the code in the example across to my own applet code. The code appears in one long string, how should i be doing this?


------------------------------------------------------------------------------------------------------------------
aozarov I'm trying to use this I took from your previous ans

>>
AudioClip clip1 = getAudioClip(url1);
AudioClip clip2 = getAudioClip(url2);
AudioClip clip3 = getAudioClip(url3);
and then do: clip1.play(); clip2.play(); clip3.play();


Where do i call the audio clips in the applet? under the 'main' class?

the problem maybe that i'm trying to form this around the code i alreayd have. I've tried a few different things but I just cant get it to play 2 clips.

public class TestSounds extends Applet implements ActionListener {
     private AudioClip clip, clip2;
     private Button bnStart;


     // Initialize the applet
     public void init() {
          try {
               jbInit();
          } catch (Exception e) {
               e.printStackTrace();
          }
     }

     // Component initialization
     private void jbInit() throws Exception {
          try {
               clip = java.applet.Applet.newAudioClip(new java.net.URL(getCodeBase(),"1000Hz.au"));
               clip2 = java.applet.Applet.newAudioClip(new java.net.URL(getCodeBase(),"1000Hz.au"));
               bnStart = new Button("Start");

               bnStart.addActionListener(this);

               add(bnStart);

          } catch (Exception e) {
               e.printStackTrace();
          }
     }
     public void actionPerformed(ActionEvent e) {
          if (e.getSource() == bnStart) {
               clip.play();
               clip2.play();
          }
0
pigmentarts
Asked:
pigmentarts
  • 6
  • 5
1 Solution
 
aozarovCommented:
The code here indicate that the audio clips should be played once you clicked on the bnStart button.
If you want them to play automatically once the Applet started. then add this method

public void start()
{
clip1.play();
clip2.play();
}
0
 
aozarovCommented:
Applet do not have a main method but rather their life cycle is controlled by the browser.
there are four "main" methods:
init(); // get called once the applet gets loaded
destroy(); // get called once the applet get removed
start(); // get called each time you visit the page
stop(); // get called when you move to another page
see: http://java.sun.com/docs/books/tutorial/applet/overview/lifeCycle.html
0
 
aozarovCommented:
I assume the code you have was used by some visual IDE.
Based on it you should have one button. clicking on it should trigger the audio clips.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
pigmentartsAuthor Commented:
ha i've just read about the life cycle. finnally know about the init() now :P

yes thats right, Its playing both the files at the same time tho
0
 
pigmentartsAuthor Commented:
What should be done in the initialization part then?

Heres my code now, How do i make the clips come one after the other instead of playing both at the same time.

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"));
     }
     
     public void start()
 {
 clip.play();
 clip2.play();
 }
}
0
 
aozarovCommented:
That is a problem.
The AudioClip class doesn't give you a method to know when a played clips is completed (though it can cycle it
automatically when its done :-(
As I said before you can either resort to the more complex (JVM > 1.4) javax.sound api or just use some
audio editor and combine those two files into one file.
0
 
aozarovCommented:
Another option (which I personally don't like) if you know how long the first clip should take is to do:

private Thread thread;

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

 public void start()
 {
     clip.play();
     thread = new Thread()
       {
                 public void run()
                {
                        try
                        {
                               Thread.sleep(the_time_that_it_takes_clip1_to_play_in_milliseconds);
                               clip.stop();
                               clip1.play();
                         }
                         catch (Exception ex)
                         {
                              ex.printStackTrace();
                         }
                       
                }
       };

       thread.start();
     }

0
 
pigmentartsAuthor Commented:
Yes i was thinking of doing that if i couldnt get it to work any other way. Do you know of one that can put together sound files and maybe add time gaps between the files


The thing is, I want a user to interact with the sounds, where they press a button when they hear a certain sound and some information gets logged into an array.

I was thinking the quick way to do this is to set a timer to start when the clip starts playing.
When the user presses a button between 20 secs and 30 secs when clip 3 is playing.

something like if time >20 <30 then store in the array  (1000Hz, 20dB)

However if I put the sound files in one long audio then it wont matter if a user presses a button, the whole file will continue to play, when i would really like it to stop and start on a different set of clips.
0
 
pigmentartsAuthor Commented:
Hmmm that might work quite well, I could then create gaps between sound files..

Would it be possible to put in a listener for user interaction during each clip being played? So I can say, if a user presses a button then jump to clip 5..
0
 
aozarovCommented:
Sure, you can add a button with a listener (the same way you did for bnStart) and then in
the actionPerformed add logic to stop the current clip and play any other clip.
0
 
pigmentartsAuthor Commented:
ok great, could u look at my next Q pls :P

just b4 i goto bed
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now