Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

Using an ActionEvent

I've created a button to perform user actions on clips being played.

Button button = new Button("ClickMe");
               button.setPreferredSize(new Dimension(200, 80));
               add(button, BorderLayout.CENTER);
               button.addActionListener(new ActionListener()
                {
                            public void actionPerformed(ActionEvent e)
                            {
                button_actionPerformed(e);
                // logic to handle the button click event
                          }
                });


I'm playing the clips like this

  clip.play();
                                    Thread.sleep(2000);
                                    clip.stop();
                                  clip2.play();

                                  // probably you want this
                                  Thread.sleep(2000);
                                  clip2.stop();

When a user presses the button when a tone is being played I want to register this and take the user to say clip 5 from clip 2.

Could someone show me a tutorial on this or give me any tips on doing this?
Thx

0
pigmentarts
Asked:
pigmentarts
  • 19
  • 11
1 Solution
 
aozarovCommented:
pigmentarts, I explained that in the previous thread.

add a member variable;
private String currentClip;

then after each stop/play do

currentClip = "clip1";
clip.play();
                                    Thread.sleep(2000);
                                    clip.stop();
currentClip = "clip2";
                                  clip2.play();
...

in the button_actionPerformed(e) method you can access currentClip which will have the name of the current clip name.
0
 
pigmentartsAuthor Commented:
>add a member variable;

whats that?
0
 
aozarovCommented:
This declaration: private String currentClip;
which should be outside of any function (but inside your Applet class).
memeber variables can be accessed by any method (as opose to local variables which are defined in a method and can be accessed only by it).
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:
Ah I thought it was, and that rings bells with what i was reading earlier.

The String currentClip is declared under the main class (and can be used as a blueprint for any other methods :)

Ok so using this bit?

 public void actionPerformed(ActionEvent e)
                            {
                button_actionPerformed(e);
               
<<in here I say what should happen to each clip?
               
                          }
                });
0
 
aozarovCommented:
Right,

public void actionPerformed(ActionEvent e)
                            {
                button_actionPerformed(e);
               
                       <<in here I say what should happen to each clip?
                        << where the current clip that is playing is currentClip
                          }
                });
0
 
pigmentartsAuthor Commented:
not sure what you mean

something like

if currentClip = ("clip1" "clip2" clip3") then make currentClip = "clip5"  
0
 
aozarovCommented:
I mean update the value of currentClip with the name of the current clip just before you call play on it (in the Thread).
Is what you want when the user click the button is display the name of the current playing clip?
0
 
pigmentartsAuthor Commented:
no, for now because i'm playing a lot of clips in order, I want the action of a user clicking a button to take him to a clip further down the line.

for example i have 60 clips

if he clicks within the first 8 clips he'll be moved to clip 9. Then say he presses the button anywhere between 9 and 16 he'll be moved to clip 17
0
 
aozarovCommented:
In that case you shoud change the logic of the Thread instead of:

play1
pause
stop1
play2
...

to
define this as a member variable int index = 0;
AduioClip array_of_clips[] = new AudioClip[]{ clip, clip2, ..., clip60};

for (index <  array_of_clips.length;)
{
array_of_clips[index].play();
Thread.sleep(xxxxxx);
index++;
}

Then in your actionPerformed you can change the value of index which will effect the next chosen clip.
0
 
pigmentartsAuthor Commented:
I get a problem using the '< sign in -- for (index <  array_of_clips.length;)  
says i need source 1.5 i have 1.2  That must be the version of j-builder i'm using no?

plus other errors on the '{' which i cant seem to get rid of

 public void start()
     {
         thread = new Thread() {
             public void run() {
                 for (index < array_of_clips.length; ) {
                     array_of_clips[index].play();
                     Thread.sleep(xxxxxx);
                     index++;
                 }
     
             }
         };
     }
-------------------------------------------------------

>Then in your actionPerformed you can change the value of index which will effect the next chosen clip.

I'm really unsure how to change the value of index, something like this? any tutorials?

 public void actionPerformed(ActionEvent e)
                            {
                button_actionPerformed(e);
             
                for (index < 8 ) then change index = 9
                for (index < 16 ) then change index = 17




0
 
aozarovCommented:
sorry, change:
  for (index < array_of_clips.length; ) {
to
while (index < array_of_clips.length) {

>> for (index < 8 ) then change index = 9
>> for (index < 16 ) then change index = 17
if (index < 8)
index = 9;
else if (index < 16)
index = 17;
0
 
pigmentartsAuthor Commented:
the index of clips isnt being called for some reason. the clips arent playing

 public void start()
     {
         thread = new Thread()
            {
                      public void run()
                     {
                             
                             {
                                 while (index < array_of_clips.length) {

{
    array_of_clips[index].play();
    try {
    Thread.sleep(1000);
} catch (InterruptedException ex) {
}
index++;
}
                              }
                             
                             }
                    }
        };

 thread.start();
     }
0
 
aozarovCommented:
Did you initialize array_of_clips?
You should do that in the same place where you load the clips.
array_of_clips = new AudioClip[] {clip, clip2, ...};

public void start()
{
     thread = new Thread()
     {
          public void run()
          {
             while (index < array_of_clips.length)
             {
                 array_of_clips[index].play();
                 try
                 {
                    Thread.sleep(1000); // is one second enough??
                 }
                 catch (InterruptedException ex)
                 {
                      return; // I should be interrupted if the user want to stop the clips
                 }
                 
                 rray_of_clips[index].stop();
                 index++;
              }
           }
     };

     thread.start();
}
0
 
pigmentartsAuthor Commented:
heh i put it under the main class

public class TestSounds extends Applet {
    private AudioClip clip, clip2;
    private String currentClip;
    int index = 0;
   AudioClip array_of_clips[] = new AudioClip[] {clip, clip2};

    // Initialize the applet

If i put it under this i get errors
 // Component initialization
    private void jbInit() throws Exception {
       
0
 
pigmentartsAuthor Commented:
what does  .length bit do? there is nothing to measure is there?

>>(index < array_of_clips.length)

0
 
aozarovCommented:
array_of_clips.length == to the size of the array (number of elements).
If you initialize the array this way : AudioClip array_of_clips[] = new AudioClip[] {clip, clip2};
then array_of_clips.length will return 2;
and the element in the array (clip, clip2) can be accessed via array_of_clips[0] and array_of_clips[1]

>> i put it under the main class
>> AudioClip array_of_clips[] = new AudioClip[] {clip, clip2};
Under the main class just do:
AudioClip array_of_clips[]; // This is declaration part

and in jbInit (after you loaded the clips) do:
array_of_clips = new AudioClip[] {clip, clip2}; // This is the array initalization part
0
 
pigmentartsAuthor Commented:
ahhhhhhhhhhhhhhhhhh
0
 
pigmentartsAuthor Commented:
its all so clear now!

aozarov No1! aozarov No1!

^_^
0
 
pigmentartsAuthor Commented:
i'll do it after dinner :)
0
 
pigmentartsAuthor Commented:
before i get in trouble
0
 
pigmentartsAuthor Commented:
I think the clips are getting stopped before they can play
0
 
pigmentartsAuthor Commented:
hmmm it all seems fine..I can see how the clip index is used.don't understand why its not working
0
 
aozarovCommented:
>> I think the clips are getting stopped before they can play
increase the delay

>> don't understand why its not working
simple...
you have an array of clips. we iteratre over the array
In each iteration we start a clip then sleep for the specific delay duration and then stop that clip.
After index is get increamented so the next iteration will do the same for the second clip in the array.
This will happend until index become bigger then the number of available clips in that array.
0
 
pigmentartsAuthor Commented:
yeah, thats what the while statement is for...and the length.

But no sound plays at all..
0
 
pigmentartsAuthor Commented:
Unless i've made a mistake??


public class TestSounds extends Applet {
    private AudioClip clip, clip2;
    private String currentClip;
      int index = 0;
    AudioClip array_of_clips[];

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

    // Component initialization
    private void jbInit() throws Exception {
        array_of_clips = new AudioClip[] {clip, clip2};
     
        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") { // add the button to the Applet
            public Dimension getPreferredSize() {
                return new Dimension(200, 80);
            }
        };

        add(button, BorderLayout.CENTER);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                button_actionPerformed(e);

                if (index < 8) {
                    index = 9;
                } else if (index < 16) {
                    index = 17;
                } else if (index < 24) {
                    index = 17;
                }

                // logic to handle the button click event
            }
        });
    }

    public void start() {
        thread = new Thread() {
            public void run() {
                while (index < array_of_clips.length) {
                    array_of_clips[index].play();
                    try {
                        Thread.sleep(1000); // is one second enough??
                    } catch (InterruptedException ex) {
                        return; // I should be interrupted if the user want to stop the clips
                    }

                    array_of_clips[index].stop();
                    index++;
                }
            }
        };

        thread.start();
    }

    private Thread thread;
    public void stop() {
        if (thread != null) {
            thread.interrupt();
        }
    }
0
 
aozarovCommented:
put this line:
array_of_clips = new AudioClip[] {clip, clip2};
after this line:      
        clip2 = java.applet.Applet.newAudioClip(new java.net.URL(getCodeBase(),
                "2.au"));

The assigment to the array copies the references of clip and clip1 to the array elements but when
it comes before loading the clips those reference are equal to null.
0
 
pigmentartsAuthor Commented:
oh
0
 
pigmentartsAuthor Commented:
:D
0
 
pigmentartsAuthor Commented:
ok, you take a break ;)

I'm gonna make some sounds
0
 
aozarovCommented:
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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