简体   繁体   中英

JavaFx: Pathing and Getting audio files to work in my .jar

I'm currently making a small game in JavFx and am trying to play a sound when objects collide currently this is my implementation:

if (paddle.getBoundsInParent().intersects(circle.getBoundsInParent())) {
        dx *= -1;
        Media sound = new Media(getClass().getResource("/Audio/Paddle.m4a").toString());
        MediaPlayer mediaPlayer = new MediaPlayer(sound);
        mediaPlayer.play();
}

The audio files are stored in a source folder I made named "Audio" that is alongside the src and bin folders. My issue is that I cannot figure out the correct path for the audio files or how to extract them into a .jar using eclipse.

An easy way to solve this would be to put your media files in the default package.

Then switch the file path in your code to just the name of the file, so /audio/Paddle.mp4 becomes Paddle.mp4

Since you did not specify a path, java will look in the default package and find your media file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM