Solved

How to set gain (volume) using JMF?

Posted on 2004-03-22
6
4,706 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
Comment Utility
Take a look at the GainControl interface.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Accepted Solution

by:
krakatoa earned 125 total points
Comment Utility
// 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
Comment Utility
Thanks,

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

gnurl
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Great! Glad to hear!

k.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
Convert ArrayList of custom objects to JSON 4 53
IT Company 5 68
Starting to learn JAVA, 7 47
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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now