Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to set gain (volume) using JMF?

Posted on 2004-03-22
6
Medium Priority
?
4,860 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Accepted Solution

by:
krakatoa earned 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

722 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