tags:

views:

251

answers:

3

Hi, I am trying to play audio in a Java applet but it is not working. What could be the problem?

EDIT: Besides the limited number of files that Java can play, the problem was that I didn't realize that the bin folder (in Eclipse workspace) contains the .class file that is run. So my code referring to the getDocumentBase() couldn't find the audio file. -_-"

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

public class Viewer extends Applet
    implements MouseListener, MouseMotionListener {
    AudioClip sound1;
    AudioClip tap;
    AudioClip clap;

    public void init() {
     sound1 = this.getAudioClip(getDocumentBase(),"boom.au");
     tap = getAudioClip(getDocumentBase(), "tap.au");
     clap = getAudioClip(getDocumentBase(), "clap.au");

     this.resize(600,600);

     addMouseListener(this);

     setBackground(Color.BLACK);
    }

    public void paint (Graphics g) {

    }

    public void mouseClicked(MouseEvent e) {
     sound1.play();
     System.out.println("BOOM!");
    }
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
    public void mousePressed(MouseEvent e) {
     tap.play();
     System.out.println("tap!");
    }
    public void mouseReleased(MouseEvent e) { }
    public void mouseDragged(MouseEvent e) { }
    public void mouseMoved(MouseEvent e) { }

}
+2  A: 

It appears that Sun's applet implementation is very choosy about exactly what sounds formats it will play. Specifically "Currently, the Java [applet] API supports only one sound format: 8 bit, µ-law, 8000 Hz, one-channel, Sun ".au" files."

Tom Hawtin - tackline
+1  A: 

If it's the problem mentioned by Tom (quite probably), use the JavaSound API to play your audio.

Neil Coffey
A: 

EDIT: Besides the limited number of files that Java can play, the problem was that I didn't realize that the bin folder (in Eclipse workspace) contains the .class file that is run. So my code referring to the getDocumentBase() couldn't find the audio file. -_-"

Devoted