Example usage for javax.media.j3d MediaContainer MediaContainer

List of usage examples for javax.media.j3d MediaContainer MediaContainer

Introduction

In this page you can find the example usage for javax.media.j3d MediaContainer MediaContainer.

Prototype

public MediaContainer(InputStream stream) 

Source Link

Document

Constructs and initializes a MediaContainer object using specified parameters.

Usage

From source file:SimpleSounds.java

/**
 * This adds a continuous background sound to the branch group.
 * //from  w ww .  j  a  v  a2 s. c o  m
 * @param b
 *            BranchGroup to add the sound to.
 * @param soundFile
 *            String that is the name of the sound file.
 */
protected void addBackgroundSound(BranchGroup b, String soundFile) {
    //Create a media container to load the file
    MediaContainer droneContainer = new MediaContainer(soundFile);
    //Create the background sound from the media container
    BackgroundSound drone = new BackgroundSound(droneContainer, 1.0f);
    //Activate the sound
    drone.setSchedulingBounds(bounds);
    drone.setEnable(true);
    //Set the sound to loop forever
    drone.setLoop(BackgroundSound.INFINITE_LOOPS);
    //Add it to the group
    b.addChild(drone);
}

From source file:SoundBug.java

void setupSounds() {
    soundSwitch = new Switch(Switch.CHILD_NONE);
    soundSwitch.setWhichChild(Switch.CHILD_NONE);
    soundSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // set up the BoundingSphere for all the sounds
    BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);

    // Set up the sound media container
    java.net.URL soundURL = null;
    String soundFile = "techno_machine.au";
    try {/*from   w  ww  .  java  2s . c o  m*/
        java.net.URL codeBase = null;
        try {
            codeBase = getCodeBase();
        } catch (Exception ex) {
            // got an exception, probably running as an application,
            // keep code base null...
        }
        if (codeBase != null) {
            soundURL = new java.net.URL(codeBase.toString() + soundFile);
        }
    } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
    if (soundURL == null) { // application, try file URL
        try {
            soundURL = new java.net.URL("file:./" + soundFile);
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }
    System.out.println("soundURL = " + soundURL);
    MediaContainer soundMC = new MediaContainer(soundURL);

    // set up the Background Sound
    soundBackground = new BackgroundSound();
    soundBackground.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundBackground.setSoundData(soundMC);
    soundBackground.setSchedulingBounds(bounds);
    soundBackground.setEnable(true);
    soundBackground.setLoop(Sound.INFINITE_LOOPS);
    soundSwitch.addChild(soundBackground);

    // set up the point sound
    soundPoint = new PointSound();
    soundPoint.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundPoint.setSoundData(soundMC);
    soundPoint.setSchedulingBounds(bounds);
    soundPoint.setEnable(true);
    soundPoint.setLoop(Sound.INFINITE_LOOPS);
    soundPoint.setPosition(-5.0f, -5.0f, 0.0f);
    Point2f[] distGain = new Point2f[2];
    // set the attenuation to linearly decrease volume from max at
    // source to 0 at a distance of 5m
    distGain[0] = new Point2f(0.0f, 1.0f);
    distGain[1] = new Point2f(5.0f, 0.0f);
    soundPoint.setDistanceGain(distGain);
    soundSwitch.addChild(soundPoint);

}

From source file:SimpleSounds.java

/**
 * Add a sound to the transform group. This takes a point sound object and
 * loads into it a sounds from a given file. The edge of the sound's extent
 * is also defined in a parameter.// ww w.j av a2s. co  m
 * 
 * @param tg
 *            TransformGroup that the sound is to be added to
 * @param sound
 *            PointSound to be used
 * @param soundFile
 *            String that is the name of the sound file to be loaded
 * @param edge
 *            float that represents the sound's maximum extent
 */
protected void addObjectSound(TransformGroup tg, PointSound sound, String soundFile, float edge) {
    //First we get the current transform so that we can
    //position the sound in the same place
    Transform3D objXfm = new Transform3D();
    Vector3f objPosition = new Vector3f();
    tg.getTransform(objXfm);
    objXfm.get(objPosition);
    //Create the media container to load the sound
    MediaContainer soundContainer = new MediaContainer(soundFile);
    //Use the loaded data in the sound
    sound.setSoundData(soundContainer);
    sound.setInitialGain(1.0f);
    //Set the position to that of the given transform
    sound.setPosition(new Point3f(objPosition));
    //Allow use to switch the sound on and off
    sound.setCapability(PointSound.ALLOW_ENABLE_READ);
    sound.setCapability(PointSound.ALLOW_ENABLE_WRITE);
    sound.setSchedulingBounds(bounds);
    //Set it off to start with
    sound.setEnable(false);
    //Set it to loop forever
    sound.setLoop(BackgroundSound.INFINITE_LOOPS);
    //Use the edge value to set to extent of the sound
    Point2f[] attenuation = { new Point2f(0.0f, 1.0f), new Point2f(edge, 0.1f) };
    sound.setDistanceGain(attenuation);
    //Add the sound to the transform group
    tg.addChild(sound);
}

From source file:ExSound.java

public Group buildScene() {
    // Get the initial sound volume
    float vol = ((Float) volumes[currentVolume].value).floatValue();

    // Turn off the example headlight
    setHeadlightEnable(false);/*from w  w w .  j  a v  a2 s  .  c o m*/

    // Default to walk navigation
    setNavigationType(Walk);

    // Build the scene group
    Group scene = new Group();

    //
    // Preload the sounds
    //
    if (debug)
        System.err.println("  sounds...");
    String path = getCurrentDirectory();
    MediaContainer backgroundMedia = new MediaContainer(path + "canon.wav");
    backgroundMedia.setCacheEnable(true);

    MediaContainer pointMedia = new MediaContainer(path + "willow1.wav");
    pointMedia.setCacheEnable(true);

    // BEGIN EXAMPLE TOPIC
    // Create influencing bounds
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    // Background sound
    backgroundSound = new BackgroundSound();
    backgroundSound.setEnable(backgroundSoundOnOff);
    backgroundSound.setLoop(Sound.INFINITE_LOOPS);
    backgroundSound.setSoundData(backgroundMedia);
    backgroundSound.setInitialGain(vol);
    backgroundSound.setSchedulingBounds(worldBounds);
    backgroundSound.setCapability(Sound.ALLOW_ENABLE_WRITE);
    backgroundSound.setCapability(Sound.ALLOW_INITIAL_GAIN_WRITE);
    scene.addChild(backgroundSound);

    // Create a distance gain array for the point sound
    Point2f[] distanceGain = { new Point2f(9.0f, 1.0f), // Full volume
            new Point2f(10.0f, 0.5f), // Half volume
            new Point2f(20.0f, 0.25f), // Quarter volume
            new Point2f(30.0f, 0.0f), // Zero volume
    };

    // Point sound
    pointSound = new PointSound();
    pointSound.setEnable(pointSoundOnOff);
    pointSound.setPosition(new Point3f(pointX, soundHeight, 0.0f));
    pointSound.setLoop(Sound.INFINITE_LOOPS);
    pointSound.setSoundData(pointMedia);
    pointSound.setInitialGain(vol);
    pointSound.setDistanceGain(distanceGain);
    pointSound.setSchedulingBounds(worldBounds);
    pointSound.setCapability(Sound.ALLOW_ENABLE_WRITE);
    pointSound.setCapability(Sound.ALLOW_INITIAL_GAIN_WRITE);
    scene.addChild(pointSound);
    // END EXAMPLE TOPIC

    // Build a few lights, one per sound. We'll turn them
    // on when the associated sound is on.
    ambientLight = new AmbientLight();
    ambientLight.setEnable(backgroundSoundOnOff);
    ambientLight.setColor(Gray);
    ambientLight.setInfluencingBounds(worldBounds);
    ambientLight.setCapability(Light.ALLOW_STATE_WRITE);
    scene.addChild(ambientLight);

    pointLight = new PointLight();
    pointLight.setEnable(pointSoundOnOff);
    pointLight.setColor(White);
    pointLight.setPosition(0.0f, soundHeight, 0.0f);
    pointLight.setInfluencingBounds(worldBounds);
    pointLight.setCapability(Light.ALLOW_STATE_WRITE);
    scene.addChild(pointLight);

    // Add a basic ambient light for when all sounds (and
    // their lights) are off so that the world isn't dark
    AmbientLight amb = new AmbientLight();
    amb.setEnable(true);
    amb.setColor(Gray);
    amb.setInfluencingBounds(worldBounds);
    amb.setCapability(Light.ALLOW_STATE_WRITE);
    scene.addChild(amb);

    // Build foreground geometry
    scene.addChild(buildForeground());

    return scene;
}

From source file:EnvironmentExplorer.java

void setupSounds() {
    soundSwitch = new Switch(Switch.CHILD_NONE);
    soundSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Set up the sound media container
    java.net.URL soundURL = null;
    String soundFile = "techno_machine.au";
    try {/*w  w  w. j  a v  a2 s. c om*/
        soundURL = new java.net.URL(codeBaseString + soundFile);
    } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
    if (soundURL == null) { // application, try file URL
        try {
            soundURL = new java.net.URL("file:./" + soundFile);
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }
    //System.out.println("soundURL = " + soundURL);
    MediaContainer soundMC = new MediaContainer(soundURL);

    // set up the Background Sound
    soundBackground = new BackgroundSound();
    soundBackground.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundBackground.setSoundData(soundMC);
    soundBackground.setSchedulingBounds(infiniteBounds);
    soundBackground.setEnable(false);
    soundBackground.setLoop(Sound.INFINITE_LOOPS);
    soundSwitch.addChild(soundBackground);

    // set up the point sound
    soundPoint = new PointSound();
    soundPoint.setCapability(Sound.ALLOW_ENABLE_WRITE);
    soundPoint.setSoundData(soundMC);
    soundPoint.setSchedulingBounds(infiniteBounds);
    soundPoint.setEnable(false);
    soundPoint.setLoop(Sound.INFINITE_LOOPS);
    soundPoint.setPosition(-5.0f, 5.0f, 0.0f);
    Point2f[] distGain = new Point2f[2];
    // set the attenuation to linearly decrease volume from max at
    // source to 0 at a distance of 15m
    distGain[0] = new Point2f(0.0f, 1.0f);
    distGain[1] = new Point2f(15.0f, 0.0f);
    soundPoint.setDistanceGain(distGain);
    soundSwitch.addChild(soundPoint);

    // Create the chooser GUI
    String[] soundNames = { "None", "Background", "Point", };

    soundChooser = new IntChooser("Sound:", soundNames);
    soundChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            int value = event.getValue();
            // Should just be able to use setWhichChild on
            // soundSwitch, have to explictly enable/disable due to
            // bug.
            switch (value) {
            case 0:
                soundSwitch.setWhichChild(Switch.CHILD_NONE);
                soundBackground.setEnable(false);
                soundPoint.setEnable(false);
                break;
            case 1:
                soundSwitch.setWhichChild(0);
                soundBackground.setEnable(true);
                soundPoint.setEnable(false);
                break;
            case 2:
                soundSwitch.setWhichChild(1);
                soundBackground.setEnable(false);
                soundPoint.setEnable(true);
                break;
            }
        }
    });
    soundChooser.setValue(Switch.CHILD_NONE);

}

From source file:KeyNavigateTest.java

private MediaContainer loadSoundFile(String szFile) {
    try {// www .  ja v a  2  s . co  m
        File file = new File(System.getProperty("user.dir"));
        URL url = file.toURL();

        URL soundUrl = new URL(url, szFile);
        return new MediaContainer(soundUrl);
    } catch (Exception e) {
        System.err.println("Error could not load sound file: " + e);
        System.exit(-1);
    }

    return null;
}