java如何调用本地扬声器

博主的毕设系统在做一个餐厅的点餐管理系统,在记性移动端页面开发的时候突发奇想做一个呼叫服务员,扬声器发声的一个功能类似于:” 工作人员请注意,桌号8001顾客正在寻求帮助!“。

实现方式

接下来就对这个小功能进行分析和实现。先写一个Demo。

这里说到了调用扬声器发声,不放还可以想一下如何调用麦克风收音。

public class EngineeCore {
    String filePath = "E:\\voice\\voice_cache.wav";
    AudioFormat audioFormat;
    TargetDataLine targetDataLine;
    boolean flag = true;

    private void stopRecognize() {
        flag = false;
        targetDataLine.stop();
        targetDataLine.close();
    }
    private AudioFormat getAudioFormat() {
        float sampleRate = 16000;
        // 8000,11025,16000,22050,44100
        int sampleSizeInBits = 16;
        // 8,16
        int channels = 1;
        // 1,2
        boolean signed = true;
        // true,false
        boolean bigEndian = false;
        // true,false
        return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    }// end getAudioFormat

    private void startRecognize() {
        try {
            // 获得指定的音频格式
            audioFormat = getAudioFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
            targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
            // Create a thread to capture the microphone
            // data into an audio file and start the
            // thread running. It will run until the
            // Stop button is clicked. This method
            // will return after starting the thread.

            flag = true;
            new CaptureThread().start();
        } catch (Exception e) {
            e.printStackTrace();
        } // end catch
    }// end captureAudio method

    class CaptureThread extends Thread {
        public void run() {
            AudioFileFormat.Type fileType = null;
            File audioFile = new File(filePath);

            fileType = AudioFileFormat.Type.WAVE;
            //声音录入的权值
            int weight = 2;
            //判断是否停止的计数
            int downSum = 0;

            ByteArrayInputStream bais = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            AudioInputStream ais = null;
            try {
                targetDataLine.open(audioFormat);
                targetDataLine.start();
                byte[] fragment = new byte[1024];

                ais = new AudioInputStream(targetDataLine);
                while (flag) {

                    targetDataLine.read(fragment, 0, fragment.length);
                    //当数组末位大于weight时开始存储字节(有声音传入),一旦开始不再需要判断末位
                    if (Math.abs(fragment[fragment.length-1]) > weight || baos.size() > 0) {
                        baos.write(fragment);
                        System.out.println("守卫:"+fragment[0]+",末尾:"+fragment[fragment.length-1]+",lenght"+fragment.length);
                        //判断语音是否停止
                        if(Math.abs(fragment[fragment.length-1])20){
                            System.out.println("停止录入");
                            break;
                        }

                    }
                }

                //取得录音输入流
                audioFormat = getAudioFormat();
                byte audioData[] = baos.toByteArray();
                bais = new ByteArrayInputStream(audioData);
                ais = new AudioInputStream(bais, audioFormat, audioData.length / audioFormat.getFrameSize());
                //定义最终保存的文件名
                System.out.println("开始生成语音文件");
                AudioSystem.write(ais, AudioFileFormat.Type.WAVE, audioFile);
                downSum = 0;
                stopRecognize();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭流

                try {
                    ais.close();
                    bais.close();
                    baos.reset();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }// end run
    }// end inner class CaptureThread

这个测试没测试,偷个懒找的”哈哈”。

还有一点是Java操作语音文件 .wav先不要研究了 :laugh and cry:,这里涉及到了语音识别,但是有百度那么些api,有兴趣的试试吧!

好了,在这里就结束了

博主把自己的毕设项目打包放到自己的服务器上,这个扬声器出现了新的问题。

本来所有的基础都是在本地运行的,通过调用本地dll文件实现扬声器发声,现在部署到centOS上将会失去这个dll的支持,目前所存在的问题是如何不使用dll文件实现这个功能,中间借助了 .wav后缀的音视频文件。

  • 如何在Linux上生成 .wav的文件。
  • 如何获取这个文件并输出。(解释一下,用餐顾客点击手机网页的菜单,然后再餐厅的主机来播放这个声音)
  • 如何在输出主机不进行任何操作就能播放这个声音或者能够恢复之前的工作状态。

现在的临时解决办法是本地跑一个呼叫服务的接口,当需要这个功能的时候远程服务器调用本地跑的接口,进而实现餐厅主机发声。

这个和上面描述的并无差别,不一样的是存在了两台主机的调用(当然两台主机都应该链接网络,能够互相通信)

先写到这了,当有解决办法的时候再更新吧!

Original: https://www.cnblogs.com/dearsusu/p/14768165.html
Author: 青衫染红尘
Title: java如何调用本地扬声器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/713724/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球