Solved

How to set gain (volume) using JMF?

Posted on 2004-03-22
6
4,772 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 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