0

I've searched and read another topics but i still can't solve my problems. I had one java main class and one java jframe. I wanna add jslider to change volume in my mp3 player. What should i do?

my_player2_func

class my_player2_func{ static Player player;

static void play() {
    try {
        FileInputStream fe = new FileInputStream(my_player2_main.str);
        player = new Player(fe);
    } catch (Exception ex) {
        System.out.println(ex);
    }

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                player.play();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }).start();
}     

static void stop() {
    if (player != null)
        player.close();
}    }

my_player2_main

public my_player2_main() {
    initComponents();     
}
static String str;                              

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    my_player2_func.play();
}                                        

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//what should i do here?}
leppie
  • 115,091
  • 17
  • 196
  • 297
  • JLayer doesn't support it. Why not use another library? – obataku Aug 24 '12 at 05:42
  • @veer JLayer doesn't support it because its not the job of a *decoder* to manage volume. Its the job of the audio system playing back the decoded audio. Suggesting another library (which by the way?) will not really change that. – Durandal Aug 24 '12 at 15:43
  • @Durandal I know very well that JLayer is a decoder, but it also has a very basic *player* interface -- which is what he's clearly using. I'd rather suggest he use a different library than tell him to hack together a volume control in the JLayer source. [JMF](http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140239.html) supports MP3 and also enables you to change volume via `GainControl`. Alternatively, he could search for an MP3 decoder to use with the `javax.sound` API, and use `FloatControl.Type.VOLUME` :-) – obataku Aug 24 '12 at 17:34
  • @veer Guess what JLayer is using under the hood? It just doesn't expose volume control through its AudioDevice. AFAIK JMF's supported formats are platform dependend, but I've never bothered with it, so maybe JMF is just the right thing for the TC, why not put your comment into an answer? – Durandal Aug 26 '12 at 12:32
  • see my answer on http://stackoverflow.com/questions/3134167/adjusting-volume-using-jlayer/17570819#17570819 – pstanton Jul 11 '13 at 02:25

1 Answers1

0

JLayer is a decoder. By its very definition it does not know anything about volume, thats the job of the audio hardware that plays the sound.

Your problem stems from the fact that you are using the Player and defaul JavaSoundAudioDevice classes, which were meant as short, simple examples how to play an MP3, not as building blocks for a full fledged audio player.

You will have to copy or modify the source of JavaSoundAudioDevice (part of JLayer source) and hack it to support volume control. Et voila you can control volume.

Durandal
  • 19,919
  • 4
  • 36
  • 70