Saturday, December 09, 2006

Assignment Tips

Java Tip 24: How to play audio in applications


Here's a step-by-step guide to playing audio files in a Java application
By Chong Ser Wah and John D. Mitchell, JavaWorld.com, 02/01/97

Playing audio files in Java applications is not officially supported in the current release of Java. But fear not, there is a way! This tip will show you how -- starting with a description of the basic steps involved in playing audio clips in Java applets and then moving on to Java application support.


Playing audio clips in applets is quite simple and involves the following steps:

Create an AudioClip object
Load .au sound file into AudioClip
Play sounds once or loop continuously
Stop playback


Here's how the code for these steps looks:

=================================================================

import java.applet.*;
AudioClip ac = getAudioClip(getCodeBase(), soundFile);
ac.play(); //play once
ac.stop(); //stop playing
ac.loop(); //play continuously


=================================================================

It would seem logical to use this same code to play audio clips in a Java application. Unfortunately, if you do that you will get errors from the compiler. Why? Because the AudioClip object and the getAudioClip() method are part of the java.applet package -- and are not part of applications. The good news is we can dive down and make things work ourselves.

The trick to solving this problem is to use some undocumented features that are provided by Sun in its JDK. Taking a peek inside the classes.zip file from the Sun JDK (using any of the various zipfile utilities), we find not only the standard Java packages such as java.applet but also sun.audio. (These are in the directory sun/audio.)

The sun.audio package contains everything we need to be able to play audio clips! Here's the code:

=================================================================

import sun.audio.*; //import the sun.audio package
import java.io.*;
//** add this into your application code as appropriate
// Open an input stream to the audio file.

InputStream in = new FileInputStream(Filename);
// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);
/* Use the static class member "player" from class AudioPlayer to
play
clip*/.
AudioPlayer.player.start(as);
// Similarly, to stop the audio.
AudioPlayer.player.stop(as);

================================================================

To use a URL as the audio stream source, substitute the following for the input stream and audio stream setup:

AudioStream as = new AudioStream (url.openStream());

================================================================

Playing the audio stream continuously adds a bit more complexity:

// Create audio stream as discussed previously.
// Create AudioData source.

AudioData data = as.getData();
// Create ContinuousAudioDataStream.
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
// Play audio.
AudioPlayer.player.play (cas);
// Similarly, to stop the audio.

AudioPlayer.player.stop (cas);


=================================================================

And there you have it. Remember, this technique uses undocumented features; there are no guarantees that it will work with anything but the current Sun JDK.

Author Bio
-----------
Chong Ser Wah is a consultant at the Competency Centre for Java in Singapore. Check out the center's Java Cup Competition.

Friday, December 08, 2006

Wednesday, December 06, 2006

Practical 10 Question 2



public class p10q2 {


public static void main(String[] args) {
// Declare an array named sqArray to store 10 instances of the Square class.
Square[] sqArray = new Square[10];
System.out.println("Length\tArea");
for (int i=0; i<sqArray.length;i++){
// create 10 instances of the Square
int randLength = 10 + (int) (Math.random()*11);
sqArray[i] = new Square(randLength);
System.out.println(sqArray[i].getLength()
+ "\t" + sqArray[i].calculateArea());
}//end for

} //end main

} //end class

Tuesday, December 05, 2006

Return Array From Method


public class ReturnArrayFromMethod {

public static double[] doubleArray(double []array){

double newmarks[] = new double[array.length];

for (int i=0;i<array.length;i++)
newmarks[i] = array[i] * 2;

return newmarks;
} //end printArray

public static void printArray(double []array){
for (int i=0;i<array.length;i++)
System.out.println(array[i]);
} //end printArray

public static void main(String[] args) {

double marks[] = {45.5,46,49,13};
double marks2[] = doubleArray(marks);

printArray(marks);
System.out.println("========");
printArray(marks2);

}//end main

} //end class

PassArray To Method , Pass By Value


public class PassArrayToMethodExample {

public static void doubleInt(int num){
num= num*2;
System.out.println(num);
}

public static void doubleArray(int []array){
for (int i=0;i<array.length;i++)
array[i] = array[i] * 2;
} //end printArray


public static void printArray(int []array){
for (int i=0;i<array.length;i++)
System.out.println(array[i]);
} //end printArray



public static void main(String[] args) {
int num = 5;

doubleInt(num);
System.out.println(num);

int a[] = {5,10,20,30,40};
int b[] = {15,2,330,40};
int c[] = {40};
int d[] = {5,50,320,40,40,100,200,300,400};

doubleArray(a);
printArray(a);

} //end main


}//end class

Friday, December 01, 2006

Create JButton Array Example



import javax.swing.*;
import java.awt.*; // layout


public class JButtonArrayExample extends JFrame{

private JButton btn[] = new JButton[20];

public JButtonArrayExample(){

for (int i=0; i <btn.length;i++){
btn[i] = new JButton("Button "+ i);
getContentPane().add(btn[i]);
}
getContentPane().setLayout(new FlowLayout());

}

public static void main(String[] args) {
JButtonArrayExample e = new JButtonArrayExample();
e.setSize(300,300);
e.setVisible(true);
} //end main

}//end class

Practical 10 Question 1



import javax.swing.*;
class p10q1 {
public static void main(String[] args) {
int[] intArray = {11,0,10,2,2};
int highest = 0;
String output = "";

for (int i=0; i<intArray.length;i++){

if (intArray[i]>highest)
highest = intArray[i];//11

if (i!= intArray.length-1) //if it is not the last element
output += intArray[i] + ",";
else
output += intArray[i]; //don't put comma

} //end for

// TODO: Use JOptionPane to show results : 1 line
// Hint: use output + highest
JOptionPane.showMessageDialog(null,
"Elements: " + output+
"\nHighest: " + highest);


}//end main
}//end class

3 Different Ways of Declaring, Creating and Initialising an Array

public class ArrayExample2 {
public static void main(String[] args) {

// Step 1: Declare
// Step 2: Create the array
// Step 3: Initialise

// Step 1
double temp0[];

// Steps1 +2
double temp1[] = new double[7];

// Step 3: initialising
temp1[0] = 37.5;
temp1[1] = 38.5;

// 3-in-1 method
// Declare,
double temp2[] = {37.5, 38.5, 39.5, 40.5,41.5};

System.out.println(temp2[2]);

// Retrieve the length of an array
System.out.println(temp2.length);

for (int i=0;i<temp2.length;i++){
System.out.println(temp2[i]);
}//end for



} //end main
} //end class

Calc. Average temperature example

public class CompleteTemperatureExample {
public static void main(String[] args) {

double []temp1 = new double[7];
double temp2[] = {39.0,39.5, 26, 28};


double total=0;

for (int i=0;i<temp2.length;i++){
total+=temp2[i];
} //end for

System.out.println("Average is" + total/temp2.length);

} //end main
} //end class

Demo Default Values of Primitive + Object


public static void main(String args[]) {

double []temperatures;
int numstudents[] = new int[5];
boolean hungry[] = new boolean[10];
char []grades = new char[3];
String names[] = new String[8];

// Retrieve second value
System.out.println("String:" + names[1]);

// Retrieve second value
System.out.println("char:" + grades[1]);

// Retrieve second value
System.out.println("boolean:" + hungry[1]);

// Retrieve second value
System.out.println("int:" + numstudents[1]);

// Allocate space to store 7 double values
temperatures = new double[7];
// Retrieve second value
System.out.println(temperatures[1]);

} //end main