-1

I watched a great tutorial of Mattew on how to implement audio sounds in Java games.

The problem is that even after I decreased the volume of the wav file when I run the game the volume of the wav sound file is still very high in Java, I mean you can't even play the game because of the background music that is too loud.

Why the volume of the wav file is not maintaining in Java?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
stefanbanu
  • 53
  • 1
  • 10
  • It is probably better to use the Java Sound based `Clip` than the applet based `AudioClip`. The `Clip` interface supports controls, one of which should be a volume. Further, if you found a video tutorial recommending `AudioClip` it is either very old, or the author is not worth listening to, or both. – Andrew Thompson Jan 01 '14 at 20:48

2 Answers2

2

It is probably better to use the Java Sound based Clip than the applet based AudioClip. The Clip interface supports controls, one of which should be a MASTER_GAIN.

E.G.

import java.awt.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.event.*;
import javax.sound.sampled.*;

class ClipVolume {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
                "http://pscode.org/media/leftright.wav");
        final Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.getAudioInputStream(url);
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                final FloatControl control = (FloatControl) 
                        clip.getControl(FloatControl.Type.MASTER_GAIN);

                final JSlider volume = new JSlider(
                        JSlider.HORIZONTAL,
                        (int) control.getMinimum(),
                        (int) control.getMaximum(),
                        (int) control.getValue());
                volume.addChangeListener(new ChangeListener() {

                    public void stateChanged(ChangeEvent ce) {
                        control.setValue(volume.getValue());
                    }
                });

                JOptionPane.showMessageDialog(null, volume);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you very much, this is what i was looking for, i will try this solution and i'll let you know.Thanks again. – stefanbanu Jan 02 '14 at 18:05
0

So, this is the code that i'm using, is working and hope that will help others as well. happy coding and once again thanks to Andrew Thompson

 package com.stefanbanu;
    import java.io.*;
    import java.net.URL;

    import javax.sound.sampled.*;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;

    // To play sound using Clip, the process need to be alive.
    // Hence, we use a Swing application.
    public class SoundClipTest extends JFrame {

       /**
         * 
         */
        private static final long serialVersionUID = 1L;

    // Constructor
       public SoundClipTest() {
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setTitle("Test Sound Clip");
          this.setSize(300, 200);
          this.setVisible(true);

          try {
             // Open an audio input stream.
             URL url = this.getClass().getClassLoader().getResource("bgsong.wav");
             AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
             // Get a sound clip resource.
             Clip clip = AudioSystem.getClip();
             // Open audio clip and load samples from the audio input stream.
             clip.open(audioIn);
             clip.start();
             final FloatControl control = (FloatControl) 
                     clip.getControl(FloatControl.Type.MASTER_GAIN);
             control.setValue(-30.0f);

    //         final JSlider volume = new JSlider(
    //                 JSlider.HORIZONTAL,
    //                 (int) control.getMinimum(),
    //                 (int) control.getMaximum(),
    //                 (int) control.getValue());
    //         volume.addChangeListener(new ChangeListener() {
    //
    //             public void stateChanged(ChangeEvent ce) {
    //                 control.setValue(volume.getValue());
    //             }
    //         });
    //
    //         JOptionPane.showMessageDialog(null, volume);
          } catch (UnsupportedAudioFileException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          } catch (LineUnavailableException e) {
             e.printStackTrace();
          }
       }

       public static void main(String[] args) {
          new SoundClipTest();
       }
    }
stefanbanu
  • 53
  • 1
  • 10