blue thief
blue thief

Reputation: 123

Java audio playing error

I am java newbie.

I was reading a tutorial book, and tried almost all code given as examples, and they all worked perfectly. But, when I tried this audio playing tutorial, even though I understood most of it, I still can't make it play. It gives me error, saying

Exception in thread "main" java.lang.Error: Unresolved compilation problem: at MouseClicker.main(MouseClicker.java:9)

Here is the code.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.net.URL;

public class MouseClicker extends Jframe{
    AudioClip click;
    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("hello.wav");
        click = Applet.newAudioClip(urlClick);
        this.setVisible(true);
    }


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

}

enter image description here

Upvotes: 1

Views: 241

Answers (2)

Reimeus
Reimeus

Reputation: 159844

You're missing an opening brace in the definition of the Clicker class

private class Clicker extends MouseAdapter {
                                           ^

A Java IDE can highlight these syntax errors.

Also ensure that the audio file hello.wav is located in the same location as MouseClicker.class (the bin folder in this case) so that it can be read as a resource.

Upvotes: 1

tckmn
tckmn

Reputation: 59303

public class MouseClicker extends Jframe{

It's a JFrame, not a Jframe. (capital F)

Remember, Java is case sensitive!

Upvotes: 1

Related Questions