Load and play wav file - Java Media

Java examples for Media:Audio

Description

Load and play wav file

Demo Code

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.JFrame;

public class MouseClicker extends JFrame {
  AudioClip click;//ww w . j  av  a2  s.c om

  public static void main(String[] args) {
    new MouseClicker();
  }

  public MouseClicker() {
    this.setSize(400, 400);
    this.setTitle("Mouse Clicker");
    this.addMouseListener(new Clicker());

    URL urlClick = MouseClicker.class.getResource("hit.wav");
    click = Applet.newAudioClip(urlClick);

    this.setVisible(true);
  }

  private class Clicker extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
      click.play();
    }
  }
}

Related Tutorials