Solved

How to set gain (volume) using JMF?

Posted on 2004-03-22
6
4,796 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 36
hibernate insert example 13 37
Tomcat: Unable to run tomcat service. 2 28
American Express @Work site and Java 4 63
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

685 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