下面列出了怎么用javafx.scene.media.AudioClip的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 大破警告
*
* @param badlyShips 大破艦
*/
private static void displayAlert(List<Ship> badlyShips) {
try {
Path dir = Paths.get(AppConfig.get().getAlertSoundDir());
Path p = Audios.randomAudioFile(dir);
if (p != null) {
AudioClip clip = new AudioClip(p.toUri().toString());
clip.setVolume(AppConfig.get().getSoundLevel() / 100D);
clip.play();
}
} catch (Exception e) {
LoggerHolder.get().warn("サウンド通知に失敗しました", e);
}
for (Ship ship : badlyShips) {
ImageView node = new ImageView(Ships.shipWithItemImage(ship));
String message = Messages.getString("ship.badly", Ships.shipMst(ship) //$NON-NLS-1$
.map(ShipMst::getName)
.orElse(""), ship.getLv());
Tools.Conrtols.showNotify(node, "大破警告", message, Duration.seconds(30));
}
}
public Beeper(ClipFactory factory, Node alert, Animation alertanimation) {
this.alert = alert;
this.alertanimation = alertanimation;
// http://www.soundjay.com/tos.html
beep = factory.createClip(getClass().getResource("/com/adr/helloiot/sounds/beep-01a.wav").toExternalForm(), AudioClip.INDEFINITE);
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create three buttons
Button play = new Button("Play");
Button loop = new Button("Loop");
Button stop = new Button("Stop");
// Create a pane and set its properties
HBox pane = new HBox(5);
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(10, 10, 10, 10));
pane.getChildren().addAll(play, loop, stop);
// Create a audio clip
AudioClip audio = new AudioClip(
"http://cs.armstrong.edu/liang/common/audio/anthem/anthem3.mp3");
// Create and register handlers
play.setOnAction(e -> {
audio.play();
});
stop.setOnAction(e -> {
audio.stop();
});
loop.setOnAction(e -> {
audio.setCycleCount(AudioClip.INDEFINITE);
});
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Exercise_16_22"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* 大破警告
*
* @param badlyShips 大破艦
*/
private static void displayAlert(List<Ship> badlyShips) {
try {
Path dir = Paths.get(AppConfig.get().getAlertSoundDir());
Path p = Audios.randomAudioFile(dir);
if (p != null) {
AudioClip clip = new AudioClip(p.toUri().toString());
clip.setVolume(AppConfig.get().getSoundLevel() / 100D);
clip.play();
}
} catch (Exception e) {
LoggerHolder.get().warn("サウンド通知に失敗しました", e);
}
for (Ship ship : badlyShips) {
ImageView node = new ImageView(Ships.shipWithItemImage(ship));
String message = Messages.getString("ship.badly", Ships.shipMst(ship) //$NON-NLS-1$
.map(ShipMst::getName)
.orElse(""), ship.getLv());
Tools.Conrtols.showNotify(node, "大破警告", message, Duration.seconds(30));
}
}
/**
* デフォルトサウンドを再生するタスクを返します。
*
* @return デフォルトサウンドを再生するタスク
*/
public static Runnable playDefaultNotifySound() {
return () -> {
try {
Path p = defaultNotifySound();
if (p != null) {
AudioClip clip = new AudioClip(p.toUri().toString());
clip.setVolume(AppConfig.get().getSoundLevel() / 100D);
clip.play();
}
} catch (Exception e) {
LoggerHolder.get().warn("サウンド通知に失敗しました", e);
}
};
}
/**
* サウンド通知
*/
private void soundNotify(Path dir) {
if (this.clip == null || !this.clip.isPlaying()) {
try {
Path p = Audios.randomAudioFile(dir);
if (p != null) {
this.clip = new AudioClip(p.toUri().toString());
this.clip.setVolume(AppConfig.get().getSoundLevel() / 100D);
this.clip.play();
}
} catch (Exception e) {
LoggerHolder.get().warn("サウンド通知に失敗しました", e);
}
}
}
public static void audio(File file, double volumn, int cycle) {
AudioClip clip = new AudioClip(file.toURI().toString());
clip.setVolume(volumn);
clip.setCycleCount(cycle);
clip.play();
}
StandardClip(String url, int cyclecount) {
clip = new AudioClip(url);
clip.setCycleCount(cyclecount);
}
private void playSound(String soundURI) {
if (userContext.isPlaySounds()) {
new AudioClip(soundURI).play(1);
}
}