Below are some exercises to practice your skills with sound and image manipulation. Make a
new scenario for a (very simple) MP3 player. Create an actor for a start button. Place an MP3 file
into the scenario\'s sound folder. Program it so that clicking the button starts playing the MP3
file. Clicking it while it is playing stops playing the file. Make sure the button image changes: It
should be the typical \"play\" triangle while the sound is not playing, and change to the usual
\"pause\" symbol (double vertical bars) when it is playing. Add a stop button. This button should
stop the sound. When you then press play again, the sound starts from the beginning (not from
the point where it was stopped). Make sure the play button\'s image changes approximately when
the sound is stopped. Add two buttons to change the volume up and down. Add a display that
shows the current volume. Add three buttons to select three different songs (which you must
place in your sounds folder).
Solution
import java.applet.Applet;
example 10.45 and 10.47 :-
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class sound extends Applet implemets ActionListener{
Button play,stop;
AudioClip clip;
private static final string PLAY = \"PLAY\";
private static final string STOP = \"STOP\";
public void init()
{
play= new Button();
play.setLabel(PLAY);
play.setActionCommand(PLAY);
play.addActionListener(this);
add(play);
stop= new Button();
stop.setLabel(STOP);
stop.setActionCommand(STOP);
stop.addActionListener(this);
add(stop);
audioclip = getAudioclip(getcodebase() ,\"one.mp3\");
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equlas(PLAY))
{
audioClip.PLAY();
}
elseif((e.getActionCommand().equlas(STOP))
{
audioClip.STOP();
};
else{
audioClip.STOP();
}`
}
}
exercise 10.48 :
import java.Windows.Forms;
import java.Runtime.InteropServices;
public class form1 extends form{
private const int APPCOMMAND_VOLUME_MUTE=53422;
private const int APPCOMMAND_VOLUME_UP=52222;
private const int APPCOMMAND_VOLUME_DOWN=23422;
private const int WM_APPCOMMAND = 345;;
public static extern Intptr send messageW(IntPtr, int Msg , IntPtr, wPara , Intptr, lparam);
public form1() {
InitializeComponent();
}
private final void mute_CLICK(object sender, EventArgs e) {
Form1.SendMessage(this.Handle , WM_APPCOMMAND , this.Handle,
((IntPtr)(APPCOMMAND_VOLUME_MUTE)));
}
private final void decvol_CLICK(object sender, EventArgs e) {
Form1.SendMessage(this.Handle , WM_APPCOMMAND , this.Handle,
((IntPtr)(APPCOMMAND_VOLUME_MUTE)));
}
private final void incvol_CLICK(object sender, EventArgs e) {
Form1.SendMessage(this.Handle , WM_APPCOMMAND , this.Handle,
((IntPtr)(APPCOMMAND_VOLUME_MUTE)));
}
EXRECISE 10.49:
ADD A DISPLAY THAT SHOWS CURRENT VOLUME :-
import javax.sound.sampled.FloatControl;
protected FloatControl {
system.out.println(\"value is \"+float getValue());
}
Exercise 10.46:- change image on pressing t.
Andreas Schleicher presents at the launch of What does child empowerment mean...
Below are some exercises to practice your skills with sound and image.pdf
1. Below are some exercises to practice your skills with sound and image manipulation. Make a
new scenario for a (very simple) MP3 player. Create an actor for a start button. Place an MP3 file
into the scenario's sound folder. Program it so that clicking the button starts playing the MP3
file. Clicking it while it is playing stops playing the file. Make sure the button image changes: It
should be the typical "play" triangle while the sound is not playing, and change to the usual
"pause" symbol (double vertical bars) when it is playing. Add a stop button. This button should
stop the sound. When you then press play again, the sound starts from the beginning (not from
the point where it was stopped). Make sure the play button's image changes approximately when
the sound is stopped. Add two buttons to change the volume up and down. Add a display that
shows the current volume. Add three buttons to select three different songs (which you must
place in your sounds folder).
Solution
import java.applet.Applet;
example 10.45 and 10.47 :-
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class sound extends Applet implemets ActionListener{
Button play,stop;
AudioClip clip;
private static final string PLAY = "PLAY";
private static final string STOP = "STOP";
public void init()
{
play= new Button();
play.setLabel(PLAY);
play.setActionCommand(PLAY);
play.addActionListener(this);
add(play);
stop= new Button();
stop.setLabel(STOP);
stop.setActionCommand(STOP);
stop.addActionListener(this);
2. add(stop);
audioclip = getAudioclip(getcodebase() ,"one.mp3");
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equlas(PLAY))
{
audioClip.PLAY();
}
elseif((e.getActionCommand().equlas(STOP))
{
audioClip.STOP();
};
else{
audioClip.STOP();
}`
}
}
exercise 10.48 :
import java.Windows.Forms;
import java.Runtime.InteropServices;
public class form1 extends form{
private const int APPCOMMAND_VOLUME_MUTE=53422;
private const int APPCOMMAND_VOLUME_UP=52222;
private const int APPCOMMAND_VOLUME_DOWN=23422;
private const int WM_APPCOMMAND = 345;;
public static extern Intptr send messageW(IntPtr, int Msg , IntPtr, wPara , Intptr, lparam);
public form1() {
InitializeComponent();
}
private final void mute_CLICK(object sender, EventArgs e) {
Form1.SendMessage(this.Handle , WM_APPCOMMAND , this.Handle,
((IntPtr)(APPCOMMAND_VOLUME_MUTE)));
}
private final void decvol_CLICK(object sender, EventArgs e) {
Form1.SendMessage(this.Handle , WM_APPCOMMAND , this.Handle,
3. ((IntPtr)(APPCOMMAND_VOLUME_MUTE)));
}
private final void incvol_CLICK(object sender, EventArgs e) {
Form1.SendMessage(this.Handle , WM_APPCOMMAND , this.Handle,
((IntPtr)(APPCOMMAND_VOLUME_MUTE)));
}
EXRECISE 10.49:
ADD A DISPLAY THAT SHOWS CURRENT VOLUME :-
import javax.sound.sampled.FloatControl;
protected FloatControl {
system.out.println("value is "+float getValue());
}
Exercise 10.46:- change image on pressing the button
import java.util.*;
public class changeimage extends japanel implements ActionListener{
private static string[] images lists{ //keep the path for images location// like
images/0ne.jpeg.....}
//extend the first example program but in action listener palce the following code:-
image=new ImageIcon("images lists[1]);
//implement the same code in play button also