How to use .aiff files in Java?

633 Views Asked by At

Here is what I currently am attempting:

final AudioClip note0 = new AudioClip(getClass().getResource("/Downloads/notes/A3.aiff").toString());
    key0.setOnMouseClicked(new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(MouseEvent me)
        {
            note0.play();
        }
    });

I want to tie a key (Rectangle object) to a note (.aiff audio file). However, I am not sure how to reference the file path in .getResource(). Could anyone offer me advice on how to proceed? Thank you!

1

There are 1 best solutions below

0
Stefan Haustein On

getRessource works for resources, which are files that are part of your program. For external files, you'll probably need a file URL:

final AudioClip note0 = new AudioClip("file:///Downloads/notes/A3.aiff");

The path to your home directory may be missing in the path.

Alternatively, copy A3.aiff to the resources (or source) root directory of your code, then access it as a resource:

final AudioClip note0 = new AudioClip(getClass().getResource("/A3.aiff").toString());