Solved

How to set gain (volume) using JMF?

Posted on 2004-03-22
6
4,783 Views
Last Modified: 2012-06-27
Hi,

I want to play some sound files with java using the JMF (as backgroundmusic) so I want to use sliders to change the volume (or gain) while playing the music.
How do I do this?

I am using code like this to play the sound

public SoundDemo(String resourceName)
      {
            System.out.println("Playing: " + resourceName);
            
            try
            {
                  URL url = this.getClass().getResource(resourceName);
                  System.out.println(url);
                  
//                  Player p = Manager.createPlayer(url);
                  p.start();
                  int state = p.getState();
                  while (state != Controller.Started)
                  {
                        try
                        {
                              Thread.sleep(100);
                              state = p.getState();
                        }
                        catch (Exception e)
                        {
                              e.printStackTrace();
                        }
                  }
                              }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
      }

so the sound is played fine, I do not know how to access (or use) the getDB() and setDB() methods provided from JMF.
I tried p.getGainControl(); to use these methods.

Anybody any idea how to set the volume from a player object from the Java Media Framework?

Thank you for your help!

CU gnurl
0
Comment
Question by:gnurl
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 10652788
Take a look at the GainControl interface.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10652816
and ...

getControlComponent
public java.awt.Component getControlComponent()Get the Component associated with this Control object. For example, this method might return a slider for volume control or a panel containing radio buttons for CODEC control. The getControlComponent method can return null if there is no GUI control for this Control.
0
 
LVL 7

Author Comment

by:gnurl
ID: 10653060
Hi,

I played around with these objects, but I couldn't get it working.

How do I connect the slider to the player? Do you have any code examples?

Thanks!
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Accepted Solution

by:
krakatoa earned 125 total points
ID: 10653695
// To create a Clip object, see e723 Loading and Playing Sampled Audio
   
    // Set Volume
    FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
    double gain = .5D;    // number between 0 and 1 (loudest)
    float dB = (float)(Math.log(gain)/Math.log(10.0)*20.0);
    gainControl.setValue(dB);
   
    // Mute On
    BooleanControl muteControl = (BooleanControl)clip.getControl(BooleanControl.Type.MUTE);
    muteControl.setValue(true);
   
    // Mute Off
    muteControl.setValue(false);


from java almanac


Home > List of Packages > javax.sound.midi  [7 examples] > Properties  [4 examples]

e721. Setting the Volume of Playing Midi Audio
    // To create a Sequencer object, see e716 Loading and Playing Midi Audio
   
    if (sequencer instanceof Synthesizer) {
        Synthesizer synthesizer = (Synthesizer)sequencer;
        MidiChannel[] channels = synthesizer.getChannels();
   
        // gain is a value between 0 and 1 (loudest)
        double gain = 0.9D;
        for (int i=0; i<channels.length; i++) {
            channels[i].controlChange(7, (int)(gain * 127.0));
        }
    }

from same source. Hopefully these have one or two pointers for you.
0
 
LVL 7

Author Comment

by:gnurl
ID: 10681444
Thanks,

it seems that it works this way... :)

gnurl
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10681513
Great! Glad to hear!

k.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question