play Audio Clip - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

play Audio Clip

Demo Code


//package com.java2s;
import javax.sound.sampled.*;

public class Main {
    private static final float AUDIO_VOLUME = -5.0f;
    private static final float DECAY_FACTOR = 0.12f;

    public static void playAudio(Clip clip, float distance) {
        FloatControl volume = (FloatControl) clip
                .getControl(FloatControl.Type.MASTER_GAIN);

        float volumeAmount = AUDIO_VOLUME
                - (distance * distance * DECAY_FACTOR);

        if (volumeAmount < -80)
            volumeAmount = -80;//w w w . ja  v  a2s  .c  o  m

        volume.setValue(volumeAmount);

        if (clip.isRunning())
            clip.stop();

        clip.setFramePosition(0);
        clip.start();
    }
}

Related Tutorials