JAVA - 用滑块控制系统音量

问题描述

好吧,为了开始这个问题,首先我想澄清一下,我是Java编程的新手,所以,如果我不自觉地问了什么,请见谅。

最近我一直在做一个项目,主要是在一个库的帮助下管理 SIP VoIP 呼叫,该库完成与构建和管理 SIP 协议相关的所有繁重业务,简而言之,只是一种基本的软电话.该库还负责捕获、发送和传输音频到相应的源和目标。

我的问题是我想在我创建的图形界面中使用滑块控制系统音量,但由于以下情况,我不知道如何将 FloatControl 参数传递给滑块:

  • 如果从我启动程序开始就需要创建和显示滑块,我应该给 JSlider 对象提供什么 FloatControl 参数?,因为音频端口是由库方法打开的,或者换句话说, JavaSoundManager 对象的实例仅在我接听电话时创建。据我所知,要得到一条音频线,需要先得到System Mixer,然后是LineInfos,然后是SourceLine,最后打开这条线,就在此时,我可以创建SourceLine的de FloatControl参数并通过将它传递给 JSlider 对象,或者,还有其他方法吗?

  • 只是为了让您对情况有一个更大的了解,我仍然不清楚该库通常是如何工作的,但是检查它的类,我注意到 JavaSoundManager 类使用了 doPrivileged 和 Synchronized 方法,我是不认识,这些用于在通话建立时打开和关闭音频线路。

在简历中,这些是我的疑惑:

  • 如果这是私有的并且没有任何 getter o setter,我如何才能访问库类中的变量或参数?

  • 如何将 FloatControl 参数传递给 JSlider 对象以控制由 JavaSoundManager 类创建的线路的音量?

  • 最后,有没有办法修改库中的类?请记住,该库是一个 .jar 文件。

这是 JavaSoundManager 类的代码:

public class JavaSoundManager {

    private AudioFormat audioFormat;
    private TargetDataLine targetDataLine;
    private SourceDataLine sourceDataLine;
    private Object sourceDataLineMute;
    private DataLine.Info targetInfo;
    private DataLine.Info sourceInfo;
    private FileOutputStream microphoneOutput;
    private FileOutputStream speakerInput;
    private boolean mediaDebug;
    private Logger logger;

    public JavaSoundManager(boolean mediaDebug,Logger logger) 
        extends AbstractManager{
        this.mediaDebug = mediaDebug;
        this.logger = logger;

        audioFormat = new AudioFormat(8000,16,1,true,false);
        targetInfo = new DataLine.Info(TargetDataLine.class,audioFormat);
        sourceInfo = new DataLine.Info(SourceDataLine.class,audioFormat);
        sourceDataLineMute = new Object();
    }

    @Override
    public void init() {
        logger.debug("openAndStartLines");
        if (mediaDebug) {
            SimpleDateFormat simpleDateFormat
                    = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
            String date = simpleDateFormat.format(new Date());
            StringBuffer buf = new StringBuffer();
            buf.append(MEDIA_DIR).append(File.separator);
            buf.append(date).append("_");
            buf.append(audioFormat.getEncoding()).append("_");
            buf.append(audioFormat.getSampleRate()).append("_");
            buf.append(audioFormat.getSampleSizeInBits()).append("_");
            buf.append(audioFormat.getChannels()).append("_");
            buf.append(audioFormat.isBigEndian() ? "be" : "le");
            try {
                microphoneOutput = new FileOutputStream(buf.toString()
                        + "-microphone-output");
                speakerInput = new FileOutputStream(buf.toString()
                        + "-speaker-input");
            } catch (FileNotFoundException e) {
                logger.error("cannot-create-file",e);
                return;
            }
        }

        AccessController.doPrivileged(
                new PrivilegedAction<Void>() {

            @Override
            public Void run() {
                try {
                    targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
                    targetDataLine.open(audioFormat);
                } catch (LineUnavailableException e) {
                    logger.error("target-line-unavailable",e);
                    return null;
                } catch (SecurityException e) {
                    logger.error("security-exception",e);
                    return null;
                } catch (Throwable t) {
                    logger.error("throwable " + t.getMessage());
                    return null;
                }
                targetDataLine.start();
                synchronized (sourceDataLineMute) {
                    try {
                        sourceDataLine = (SourceDataLine) AudioSystem.getLine(sourceInfo);
                        sourceDataLine.open(audioFormat);
                    } catch (LineUnavailableException e) {
                        logger.error("source line unavailable",e);
                        return null;
                    }
                    sourceDataLine.start();
                }
                return null;
            }
        });

    }

    @Override
    public synchronized void close() {
        logger.debug("closeLines");
        if (microphoneOutput != null) {
            try {
                microphoneOutput.close();
            } catch (IOException e) {
                logger.error("cannot-close-file",e);
            }
            microphoneOutput = null;
        }
        if (speakerInput != null) {
            try {
                speakerInput.close();
            } catch (IOException e) {
                logger.error("cannot-close-file",e);
            }
            speakerInput = null;
        }

        AccessController.doPrivileged(new PrivilegedAction<Void>() {

            @Override
            public Void run() {
                if (targetDataLine != null) {
                    targetDataLine.close();
                    targetDataLine = null;
                }
                synchronized (sourceDataLineMute) {
                    if (sourceDataLine != null) {
                        sourceDataLine.drain();
                        sourceDataLine.stop();
                        sourceDataLine.close();
                        sourceDataLine = null;
                    }
                }
                return null;
            }
        });
    }

    @Override
    public synchronized byte[] readData() {
        if (targetDataLine == null) {
            return null;
        }
        int ready = targetDataLine.available();
        while (ready == 0) {
            try {
                Thread.sleep(2);
                ready = targetDataLine.available();
            } catch (InterruptedException e) {
                return null;
            }
        }
        if (ready <= 0) {
            return null;
        }
        byte[] buffer = new byte[ready];
        targetDataLine.read(buffer,buffer.length);
        if (mediaDebug) {
            try {
                microphoneOutput.write(buffer,buffer.length);
            } catch (IOException e) {
                logger.error("cannot-write-to-file",e);
                return null;
            }
        }
        return buffer;
    }

    @Override
    public int writeData(byte[] buffer,int offset,int length) {
        int numberOfBytesWritten;
        synchronized (sourceDataLineMute) {
            if (sourceDataLine == null) {
                return 0;
            }
            numberOfBytesWritten = sourceDataLine.write(buffer,offset,length);
        }
        if (mediaDebug) {
            try {
                speakerInput.write(buffer,numberOfBytesWritten);
            } catch (IOException e) {
                logger.error("cannot-write-to-file",e);
                return -1;
            }
        }
        return numberOfBytesWritten;
    }

}

如果您需要其他东西来更清楚,请告诉我,并提前感谢大家。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...