Playing Sound

Create an applet that reads in a sound file and creates an AudioClip instance from it that will play in a loop until the user visits another page.
  1. you must create a Java applet with a class that extends the Applet class. (you must import the java.applet package).
  2. you should create an init() method that gets the sound file and creates an instance of the AudioClip class.
  3. you should create a start() method that starts the sound playing in a loop.
  4. you should create a stop() method that stops playing the sound.
  5. your method paint() can print out a simple message.


Here is the applet: Need Java Enabled Browser

Solution

//Purpose:  Open sound file from the same directory as the web-page
// that includes this applet and plays  it to the screen.


import java.awt.*;
import java.applet.*;


public class test8 extends Applet {

    AudioClip butch;

    //get audio clip
    public void init() {
      butch = getAudioClip(getDocumentBase(), "bark.au");
    }

    //tell people what they are hearing    
    public void paint(Graphics g) {
      g.drawString("Butch is barking again!", 25,25);
    }

    //start looping the sound
    public void start() {
      butch.loop();
    }


    //stop the sound
    public void stop() {
      butch.stop();
    }

}