Friday, March 14, 2014

JavaFX - AudioClip

To add an AudioClip to your JavaFX screen, create an AudioClip and call its method play().

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;

public class AudioClipTest extends Application {
 
    @Override
    public void start(Stage primaryStage) {
        //Creating the AudioClip
        //Either use absolute path of the audio clip or it should be in the same place
        //where the Class used to load it is located
        //Here the Kalimba.mp3 should be in the same directory as the AudioClipTest
        final AudioClip audioClip = new AudioClip(AudioClipTest.class.getResource("Kalimba.mp3").toString());
     
        Button btn = new Button();
        btn.setText("Song: Kalimba");
        btn.setOnAction(new EventHandler<ActionEvent>() {
         
            @Override
            public void handle(ActionEvent event) {
                //calling the AudioClip's play method
                audioClip.play();
            }
        });
     
        StackPane root = new StackPane();
        root.getChildren().add(btn);
     
        Scene scene = new Scene(root, 300, 250);
     
        primaryStage.setTitle("My favorate songs!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 
--------------------------------------------------------------------------------------------------------------  

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment