Gradle. src/main/java src/main/resources . -java-, , ..
jar ( ). , click.mp3 bgm.mp3 .
, , ( ) . . , SoundPlayer
, .. sound
, SoundPlayer
, :
public SoundPlayer(String filename) {
URL resource = SoundPlayer.class.getResource(filename);
Media soundMedia = new Media(resource.toExternalForm());
mediaPlayer = new MediaPlayer(soundMedia);
}
Javadocs
public String toExternalForm()
URL-. toExternalForm .
, toExternalForm() URL .
.
apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
mainClassName = 'sound.Main'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
}
jar { manifest { attributes 'Main-Class': 'sound.Main' } }
SoundPlayer
package sound;
import java.net.URL;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class SoundPlayer {
private MediaPlayer mediaPlayer;
public SoundPlayer(String filename) {
URL resource = SoundPlayer.class.getResource(filename);
Media soundMedia = new Media(resource.toExternalForm());
mediaPlayer = new MediaPlayer(soundMedia);
}
public void play(){
mediaPlayer.play();
}
}
SoundPlayer
package sound;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
SoundPlayer s = new SoundPlayer("test.mp3");
System.out.println("Hello World");
s.play();
}
}