谁能告诉我为什么我的音量控制不起作用? [英] Can anyone tell me why my volume control doesn't work?

查看:138
本文介绍了谁能告诉我为什么我的音量控制不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么我的音量控制不工作...?

 进口java.io. *;
进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。
进口javax.sound.sampled中*。
javax.swing.event中导入*。
/ *<小程序code =KKKHEIGHT = 400宽度= 400>< /小程序> * /
公共类KKK扩展JComponent的
{
    静态文件F1;
    INT PROG;
    静态的JFrame JF;
    INT人;
    JLabel的时间;
    计时器TR;
    按钮B;
    INT POS = 0;
    剪辑C;
    一个的AudioInputStream;
    JSlider的S;
    公共静态无效的主要(字符串ARGS [])
    {
        F1 =新的文件(mm.wav);
        JF =新的JFrame();
        jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        KKK KP =新KKK(F1);
        。jf.getContentPane()加(KP,中心);
        jf.setSize(400,400);
        jf.setVisible(真);
    }
    KKK(文件F1)
    {
        尝试
        {
            A = AudioSystem.getAudioInputStream(F1);
            AudioFormat的AF = a.getFormat();
            DataLine.Info DI =新DataLine.Info(Clip.class,AF);
            C =(剪辑)AudioSystem.getLine(DI);
            c.open(一);
        }
        赶上(例外五)
        {
            的System.out.println(捕获到异常);
        }
        最后
        {
            尝试
            {
                a。关闭();
            }
            赶上(例外五)
            {
                的System.out.println(捕获到异常);
            }
        }
        人=(int)的(c.getMicrosecondLength()/ 1000);
        S = JSlider的新();
        按钮B =新按钮(播放);
        时间=新的JLabel();
        箱排= Box.createHorizo​​ntalBox();
        row.add(多个);
        row.add(二);
        row.add(时间);
        b.addActionListener(新的ActionListener(){
        公共无效的actionPerformed(ActionEvent的EEE)
        {
            玩();
        }});
        s.addChangeListener(新的ChangeListener(){
        公共无效stateChanged(EE的ChangeEvent)
        {
            //重绘();
            PROG = s.getValue();
            time.setText(PROG / 1000 +(PROG%1000)/ 100。);
            //如果(PROG!=美联社)
            //跳过(PROG);
        }});
        TR =新javax.swing.Timer中(100,新的ActionListener(){
                公共无效的actionPerformed(ActionEvent的五){
                蜱();
                }
                });
        setLayout的(新的BoxLayout(这一点,BoxLayout.Y_AXIS));
                this.add(行);
    }
    公共无效播放()
    {
        尝试
        {
            FloatControl体积=(FloatControl)c.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(-20.63f);
        }
        赶上(例外五)
        {}
        c.start();
        tr.start();
    }
    //公共无效跳过(
    公共无效打勾()
    {
          POS =(int)的(c.getMicrosecondPosition()/ 1000);
              s.setValue(POS)
    }
}


解决方案

量不会改变,只是因为......你永远不会改变的!
我想您要修改的卷时滑块状态发生变化,所以你只需要设置你的stateChanged方法音量。为了做到这一点,你可以用下面的指令:volume.setValue(-20.63f)你已经在你的程序的其它地方使用。只是你想要的值替换参数(例如滑块的值)。

希望这有助于。

Can anyone tell me why my volume control doesn't work...?

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import javax.swing.event.*;
/*<Applet code="kkk" height=400 width=400></Applet>*/
public class kkk extends JComponent
{
    static File f1;
    int prog;
    static JFrame jf;
    int al;
    JLabel time;
    Timer tr;
    Button b;
    int pos=0;
    Clip c;
    AudioInputStream a;
    JSlider s;
    public static void main(String args[]) 
    {
        f1=new File("mm.wav");
        jf=new JFrame();
        jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        kkk kp=new kkk(f1);
        jf.getContentPane().add(kp, "Center");
        jf.setSize(400,400);
        jf.setVisible(true);
    }
    kkk(File f1)
    {
        try
        {
            a=AudioSystem.getAudioInputStream(f1);
            AudioFormat af=a.getFormat();
            DataLine.Info di=new DataLine.Info(Clip.class,af);
            c=(Clip)AudioSystem.getLine(di);
            c.open(a);
        }
        catch(Exception e)
        {
            System.out.println("Exception caught ");
        }
        finally 
        {
            try
            {
                a.close();
            }
            catch(Exception e)
            {
                System.out.println("Exception caught ");
            }
        }
        al=(int)(c.getMicrosecondLength()/1000); 
        s=new JSlider();
        Button b=new Button("play");
        time=new JLabel();
        Box row = Box.createHorizontalBox();
        row.add(s);
        row.add(b);
        row.add(time);
        b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent eee)
        {
            play();
        }});
        s.addChangeListener(new ChangeListener(){
        public void stateChanged(ChangeEvent ee)
        {
            //repaint();
            prog=s.getValue();
            time.setText(prog / 1000 + "." + (prog % 1000) / 100);
            //if(prog!=ap)
            //skip(prog);
        }});
        tr = new javax.swing.Timer(100, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                tick();
                }
                });
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
                this.add(row);
    }
    public void play()
    {
        try
        {
            FloatControl volume = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(-20.63f);
        }
        catch(Exception e)
        {}
        c.start();
        tr.start();
    }
    //public void skip(
    public void tick()
    {
          pos = (int) (c.getMicrosecondPosition() / 1000);
              s.setValue(pos);
    }
}

解决方案

The volume never changes just because...you're never changing it ! I suppose you want the volume to be modified when the slider state changes, so you just have to set the volume in your stateChanged method. In order to do this, you can use the following instruction: volume.setValue(-20.63f) that you have already used elsewhere in your program. Just replace the parameter by the value you want (e.g. the value of the slider).

Hope this helps.

这篇关于谁能告诉我为什么我的音量控制不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆