I try to play a beep, I use Toolkit.getDefaultToolkit().beep() and it works with Java 7.
import java.awt.Toolkit;
public class testbeep {
public static void main(String[] args) {
Toolkit.getDefaultToolkit().beep();
}
}
Howevere, I need to use JavaFX, and JavaFX works on Java 8. Can some one help me to configure my project ? I work on eclipse, when I use Java 8 the beep works but not JavaFX, and when I use Java 7 the beep doesn't work and JavaFX not. I remark that icetea-sound.jar exist on Java 7 and doesn't existe on Java 8, can I find it somewhere ?
Mixing AWT and JavaFX can be a little tricky; however this seems to work fine for me (Java 1.8.0_20; Mac OS X 10.9.5):
import java.awt.Toolkit;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestBeep extends Application {
@Override
public void start(Stage primaryStage) {
Toolkit awtToolkit = Toolkit.getDefaultToolkit();
Button button = new Button("Beep");
button.setOnAction(e -> awtToolkit.beep());
StackPane root = new StackPane(button);
primaryStage.setScene(new Scene(root, 250, 75));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I have no idea if it's OK to call toolkit.beep()
from a thread other than the AWT event handling thread (here, I do that).
As you are writing a JavaFX application, you should probably use the built-in JavaFX AudioClip API rather than AWT based API.
AudioClip plonkSound = new AudioClip("http://somehost/path/plonk.mp3");
plonkSound.play();
See the JavaFX media Javadoc for a description of supported audio formats.
Ensure that your system meets the minimum requirements for JavaFX media playback .
For ubuntu, to use JavaFX, you either need to use the Oracle JDK or the Debian OpenJFX package as posted by Emmanuel in JavaFX and OpenJDK . The Debian OpenJFX package, is quite new, so at this stage, I'd recommend sticking with the Oracle Java runtime if you can.
Regarding your comment about icedtea-sound being present in Java 7 but not Java 8, perhaps that is because your Java 7 in openjdk and your Java 8 is Oracle jdk and perhaps Oracle jdk does not ship with icedtea-sound (I haven't checked and don't really know). You could try openjdk 8 + the debian openjfx package if you wish, perhaps that would include the icedtea-sound library. icedtea-sound is definitely not required for pure JavaFX media playback, but might be required to use the awt beep command. Regardless, I'd probably still recommend a JavaFX only solution using the JavaFX media libraries rather than mixing in AWT libraries.
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.