JavaFx 通信ITC数字广播 SAPI 使用NeoSpeech Liang包生成语音

JavaFx 通信ITC数字广播 SAPI 使用NeoSpeech Liang包生成语音

Java调用Windows SAPI.spVoice。

Java作为跨平台语音对调用Windows是没有原生支持的,所以需要使用第三方框架来做中转实现目的。

Jacob

Jacob是一个Java库,可让Java应用程序与Microsoft Windows DLL或COM库进行通信。它通过使用定制的DLL来实现此目的,Jacob Java类通过JNI与之通信。Java库和dll将Java开发人员与基础Windows库隔离开来,因此Java开发人员不必编写自定义JNI代码。Jacob不用于创建ActiveX插件或Microsoft Windows应用程序内部的其他模块。

项目地址:https://github.com/freemansoft/jacob-project

使用

Jacob 1.8下载后里面拥有以下文件

![image-20210203110122775](

)

将对应当前Java版本的DLL文件放到Java/bin目录,将jacob.jar作为项目依赖引入。

SAPI (Microsoft Speech API )

文档:https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee125663(v=vs.85)?redirectedfrom=msdn

使用 jacob 来调用SAPI。

关于如何使用这里的博客讲的很详细:https://blog.csdn.net/asuyunlong/article/details/50083421/

但是我是用的是第三方语音库,在切换语音包时会报错。

  @Test
    public void test2() {
        MSTTSSpeech speech = new MSTTSSpeech();
        String[] vs = speech.getVoices();
        System.out.println(Arrays.toString(vs));
        String text = "TTS TEST";

        speech.setFormatType(6);
        speech.changeVoice(1);
        // speech.setRate(-1);
        // speech.setRate(-1);
        System.out.println(getClass().getResource("mp3").getPath() + "\\" + UUID.randomUUID() + ".wav");
        String url = getClass().getResource("mp3").getPath();
        File wavFile = new File(url);
        if (!wavFile.exists()) {
            wavFile.getParentFile().mkdirs();
        }

        speech.saveToWav(text, url.substring(1) + "\\" + UUID.randomUUID() + ".wav");
    }

运行测试后出错:

A COM exception has been encountered:
At Invoke of: Voice
Description: 80020003 / 找不到成员。

com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Voice
Description: 80020003 / 找不到成员。

	at com.jacob.com.Dispatch.invokev(Native Method)
	...

Description: 80020003 / 找不到成员。 这个问题我找了很久,在我的电脑无法使用,但是可以使用控制面板项修改系统默认语音达到效果:

C:\Windows\SysWOW64\Speech\SpeechUX\sapi.cpl

Wav转Mp3重新编码并合并多个Mp3文件

Wav转Mp3现成的就是lame库,lame库可以很轻松搞定这件事情,首先要下载lame.exe,我这里是在命令行中使用。

https://lame.sourceforge.io/

现在的需求是,我有一段这样的文本: [P]Call.mp3;[T] 呼叫 XXX 前往 SMT 3号线

其中 [P]Call.mp3;[T] 是现有mp3文件,而其他文本是要通过sapi生成的,而且文本中可能还会出现外部的mp3文件。

所以思路是将mp3文件名称和需要转语音的文本按顺序抽取出来,将文字转wav,wav转mp3,最后再合并mp3文件。

代码 : AudioUtil4.java

public class AudioUtil4 {
    public static String path = null;
    public static String lame = null;
    static MSTTSSpeech speech = new MSTTSSpeech();

    static Logger LOG = LoggerFactory.getLogger(AudioUtil4.class);
	//合并mp3
    public static File mergeMp3(List<PlayModel> playModels) throws IOException {
        Vector<FileInputStream> fileInputStreams = new Vector<>();
        for (PlayModel playModel : playModels) {
            if (playModel.isMp3()) {
                playModel.setPath(path + "\\" + playModel.getContent());
            } else {
                playModel.setPath(AudioUtil4.textToMp3(playModel.getContent()));
            }
            fileInputStreams.add(new FileInputStream(playModel.getPath()));
           LOG.info("合并文件:" + playModel.getPath());
        }
        SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStreams.elements());
        String mergePath = path + "\\" + UUID.randomUUID() + ".mp3";
        File afterMergeMp3 = new File(mergePath);
        boolean isSuccess = afterMergeMp3.createNewFile();
        if (isSuccess) {
            FileOutputStream fileOutputStream = new FileOutputStream(afterMergeMp3);//destinationfile
            int temp;

            while ((temp = sequenceInputStream.read()) != -1) {
                // System.out.print( (char) temp ); // to print at DOS prompt
                fileOutputStream.write(temp);   // to write to file
            }
            for (FileInputStream f : fileInputStreams) {
                f.close();
            }
            fileOutputStream.close();
            sequenceInputStream.close();
            playModels.forEach(playModel -> {
                if (!playModel.isMp3()) {
                    new File(playModel.getPath()).delete();
                }
            });
        } else {
            throw new IOException("mp3创建失败! " + mergePath);
        }
        return new File(mergePath);
    }
	//获取播放列表 抽取
    public static List<PlayModel> getPlayList(String message) {
        int i = 0;
        char[] messageArr = message.toCharArray();
        List<PlayModel> playModels = new ArrayList<>();
        StringBuilder temp = new StringBuilder();
        while (i < messageArr.length) {
            if (messageArr[i] == '[') {
                if (temp.length() > 0) {
                    playModels.add(new PlayModel(false, temp.toString(), null));
                    temp = new StringBuilder();
                }
                Map<String, Object> mp3 = getEndIndex(i, messageArr);
                i = (int) mp3.get("endIndex");
                playModels.add(new PlayModel(true, mp3.get("mp3").toString(), null));
            } else {
                temp.append(messageArr[i]);
            }
            i += 1;
        }
        if (temp.length() > 0) {
            playModels.add(new PlayModel(false, temp.toString(), null));
        }
        return playModels;
    }
	//抽取
    public static Map<String, Object> getEndIndex(int index, char[] chars) {
        StringBuilder mp3 = new StringBuilder();
        int mp3EndIndex = -1;
        int endIndex = -1;
        for (int i = index + 3; i < chars.length; i++) {
            if (chars[i] == ']') {
                endIndex = i;
                break;
            } else if (chars[i] == ';') {
                mp3EndIndex = i;
            } else {
                if (mp3EndIndex == -1) {
                    mp3.append(chars[i]);
                }
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("endIndex", endIndex);
        result.put("mp3", mp3);
        return result;
    }
	//wav转mp3
    public static String textToMp3(String text) throws FileNotFoundException {
       LOG.info("转语音:" + text);
        File wavFile = new File(textToWav(text));
        if (!wavFile.exists()) {
            throw new FileNotFoundException("转语音失败!");
        } else {
            //to mp3 use lame
            String wavPathStr = wavFile.getPath();
            String mp3PathStr = wavFile.getPath().
                    replace(".wav", ".mp3");
            String cmds1 = lame +
                    String.format(" -b128 --resample 24 -m j  %s %s", wavPathStr, mp3PathStr);
//                Arguments = string.Format("-b128 --resample 24 -m j \"{0}\" \"{1}\"", str3, str4)
            //                    String.format(" --silent -b 160 -m m -q 9-resample -tt  %s %s", wavPathStr, mp3PathStr);

            String cmds2 = lame +
                    String.format(" --scale 4 --mp3input %s %s", mp3PathStr, mp3PathStr.replace(".mp3", "L.mp3"));

           LOG.info("lame to mp3 : " + cmds1);
           LOG.info("large voice : " + cmds2);
            CommandUtil.exec(cmds1);
            CommandUtil.exec(cmds2);
            new File(wavPathStr).delete();
            new File(mp3PathStr).delete();
            return mp3PathStr.replace(".mp3", "L.mp3");
        }
    }

	//文本转wav
    private static String textToWav(String text) {
        speech.setFormatType(16);
        File wavFile = new File(path);
        if (!wavFile.exists()) {
            wavFile.getParentFile().mkdirs();
        }
       LOG.info(path);
        String filePath = path + "\\" + UUID.randomUUID() + ".wav";
        speech.saveToWav(text, filePath);
       LOG.info("wav文件保存到 : " + filePath);
        return filePath;
    }

测试:

   @Test
    public void getPlayList() throws IOException {
        File directory = new File("");//设定为当前文件夹
        String mp3Path = directory.getAbsolutePath() + "\\mp3";
        String lamePath = directory.getAbsolutePath() + "\\lame.exe";

        AudioUtil4.path = mp3Path;
        AudioUtil4.lame = lamePath;


        List<PlayModel> playModels = AudioUtil4.getPlayList("[P]PromptRing.MP3;[T]请 袁振 速到 SMD 8号线 ");

        System.out.println("合并mp3:" + AudioUtil4.mergeMp3(playModels));
    }
2021-02-03 11:30:58.615 [main] INFO  cn.ciemis.util.AudioUtil4 - 合并文件:E:\He3\BroadcastR\BroadcastR\mp3\PromptRing.MP3
2021-02-03 11:30:58.617 [main] INFO  cn.ciemis.util.AudioUtil4 - 转语音:请 袁振 速到 SMD 8号线 
2021-02-03 11:30:58.617 [main] INFO  cn.ciemis.util.AudioUtil4 - E:\He3\BroadcastR\BroadcastR\mp3
2021-02-03 11:30:59.287 [main] INFO  cn.ciemis.util.AudioUtil4 - wav文件保存到 : E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.wav
2021-02-03 11:30:59.288 [main] INFO  cn.ciemis.util.AudioUtil4 - lame to mp3 : E:\He3\BroadcastR\BroadcastR\lame.exe -b128 --resample 24 -m j  E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.wav E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.mp3
2021-02-03 11:30:59.288 [main] INFO  cn.ciemis.util.AudioUtil4 - large voice : E:\He3\BroadcastR\BroadcastR\lame.exe --scale 4 --mp3input E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.mp3 E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5L.mp3
2021-02-03 11:30:59.293 [main] INFO  cn.ciemis.base.CommandUtil - <error></error>
2021-02-03 11:30:59.326 [main] INFO  cn.ciemis.base.CommandUtil - LAME 3.99.4 32bits (http://lame.sf.net)
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - LAME 3.99.4 32bits (http://lame.sf.net)
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - CPU features: MMX (ASM used), SSE (ASM used), SSE2
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - CPU features: MMX (ASM used), SSE (ASM used), SSE2
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - Resampling:  input 16 kHz  output 24 kHz
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - Resampling:  input 16 kHz  output 24 kHz
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - polyphase lowpass filter disabled
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - polyphase lowpass filter disabled
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - Encoding E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.wav
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - Encoding E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.wav
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil -       to E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.mp3
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil -       to E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.mp3
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - Encoding as 24 kHz single-ch MPEG-2 Layer III (3x) 128 kbps qval=3
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - Encoding as 24 kHz single-ch MPEG-2 Layer III (3x) 128 kbps qval=3
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil -     Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA 
2021-02-03 11:30:59.327 [main] INFO  cn.ciemis.base.CommandUtil -     Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA 
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil -      0/       ( 0%)|    0:00/     :  |    0:00/     :  |         x|     :  
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil -      0/       ( 0%)|    0:00/     :  |    0:00/     :  |         x|     :  
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil - 00:05--------------------------------------------------------------------------
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil - 00:05--------------------------------------------------------------------------
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil -    kbps      %     %                                                           
2021-02-03 11:30:59.328 [main] INFO  cn.ciemis.base.CommandUtil -    kbps      %     %                                                           
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil -     0.0                                                                        
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil -     0.0                                                                        
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil -    100/230    (43%)|    0:00/    0:00|    0:00/    0:00|   200.00x|    0:00 
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil -    100/230    (43%)|    0:00/    0:00|    0:00/    0:00|   200.00x|    0:00 
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil - ---------------------------------00:03-----------------------------------------
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil - ---------------------------------00:03-----------------------------------------
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.335 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.347 [main] INFO  cn.ciemis.base.CommandUtil -   128.0      100.0        83.0  10.0   7.0                                     
2021-02-03 11:30:59.347 [main] INFO  cn.ciemis.base.CommandUtil -   128.0      100.0        83.0  10.0   7.0                                     
2021-02-03 11:30:59.347 [main] INFO  cn.ciemis.base.CommandUtil -    200/230    (87%)|    0:00/    0:00|    0:00/    0:00|   200.00x|    0:00 
2021-02-03 11:30:59.347 [main] INFO  cn.ciemis.base.CommandUtil -    200/230    (87%)|    0:00/    0:00|    0:00/    0:00|   200.00x|    0:00 
2021-02-03 11:30:59.347 [main] INFO  cn.ciemis.base.CommandUtil - -----------------------------------------------------------------00:00---------
2021-02-03 11:30:59.348 [main] INFO  cn.ciemis.base.CommandUtil - -----------------------------------------------------------------00:00---------
2021-02-03 11:30:59.348 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.348 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.356 [main] INFO  cn.ciemis.base.CommandUtil -   128.0      100.0        76.0  12.5  11.5                                     
2021-02-03 11:30:59.357 [main] INFO  cn.ciemis.base.CommandUtil -   128.0      100.0        76.0  12.5  11.5                                     
2021-02-03 11:30:59.357 [main] INFO  cn.ciemis.base.CommandUtil -    230/230   (100%)|    0:00/    0:00|    0:00/    0:00|   204.44x|    0:00 
2021-02-03 11:30:59.357 [main] INFO  cn.ciemis.base.CommandUtil -    230/230   (100%)|    0:00/    0:00|    0:00/    0:00|   204.44x|    0:00 
2021-02-03 11:30:59.357 [main] INFO  cn.ciemis.base.CommandUtil - -------------------------------------------------------------------------------
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil - -------------------------------------------------------------------------------
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil -   128.0      100.0        76.1  12.2  11.7                                     
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil -   128.0      100.0        76.1  12.2  11.7                                     
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil - Writing LAME Tag...done
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil - Writing LAME Tag...done
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil - ReplayGain: -1.5dB
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil - ReplayGain: -1.5dB
2021-02-03 11:30:59.358 [main] INFO  cn.ciemis.base.CommandUtil - Process exitValue:0
2021-02-03 11:30:59.362 [main] INFO  cn.ciemis.base.CommandUtil - <error></error>
2021-02-03 11:30:59.387 [main] INFO  cn.ciemis.base.CommandUtil - LAME 3.99.4 32bits (http://lame.sf.net)
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - LAME 3.99.4 32bits (http://lame.sf.net)
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - CPU features: MMX (ASM used), SSE (ASM used), SSE2
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - CPU features: MMX (ASM used), SSE (ASM used), SSE2
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - Using polyphase lowpass filter, transition band:  8226 Hz -  8516 Hz
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - Using polyphase lowpass filter, transition band:  8226 Hz -  8516 Hz
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - Encoding E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.mp3
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - Encoding E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5.mp3
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil -       to E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5L.mp3
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil -       to E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5L.mp3
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - Encoding as 24 kHz single-ch MPEG-2 Layer III (12x)  32 kbps qval=3
2021-02-03 11:30:59.388 [main] INFO  cn.ciemis.base.CommandUtil - Encoding as 24 kHz single-ch MPEG-2 Layer III (12x)  32 kbps qval=3
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -     Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -     Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -      0/       ( 0%)|    0:00/     :  |    0:00/     :  |         x|     :  
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -      0/       ( 0%)|    0:00/     :  |    0:00/     :  |         x|     :  
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 00:05--------------------------------------------------------------------------
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 00:05--------------------------------------------------------------------------
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -    kbps      %     %                                                           
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -    kbps      %     %                                                           
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -     0.0                                                                        
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -     0.0                                                                        
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -      0/230    ( 0%)|    0:00/    0:00|    0:00/    0:00|   0.0000x|    0:00 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -      0/230    ( 0%)|    0:00/    0:00|    0:00/    0:00|   0.0000x|    0:00 
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 00:05--------------------------------------------------------------------------
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil - 00:05--------------------------------------------------------------------------
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -    kbps      %     %                                                           
2021-02-03 11:30:59.389 [main] INFO  cn.ciemis.base.CommandUtil -    kbps      %     %                                                           
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil -     0.0                                                                        
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil -     0.0                                                                        
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil -    100/230    (43%)|    0:00/    0:00|    0:00/    0:00|   200.00x|    0:00 
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil -    100/230    (43%)|    0:00/    0:00|    0:00/    0:00|   200.00x|    0:00 
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil - ---------------------------------00:03-----------------------------------------
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil - ---------------------------------00:03-----------------------------------------
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.400 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.415 [main] INFO  cn.ciemis.base.CommandUtil -    32.0      100.0        83.0  10.0   7.0                                     
2021-02-03 11:30:59.416 [main] INFO  cn.ciemis.base.CommandUtil -    32.0      100.0        83.0  10.0   7.0                                     
2021-02-03 11:30:59.416 [main] INFO  cn.ciemis.base.CommandUtil -    200/230    (87%)|    0:00/    0:00|    0:00/    0:00|   165.52x|    0:00 
2021-02-03 11:30:59.416 [main] INFO  cn.ciemis.base.CommandUtil -    200/230    (87%)|    0:00/    0:00|    0:00/    0:00|   165.52x|    0:00 
2021-02-03 11:30:59.416 [main] INFO  cn.ciemis.base.CommandUtil - -----------------------------------------------------------------00:00---------
2021-02-03 11:30:59.416 [main] INFO  cn.ciemis.base.CommandUtil - -----------------------------------------------------------------00:00---------
2021-02-03 11:30:59.416 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.416 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    32.0      100.0        77.5  12.5  10.0                                     
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    32.0      100.0        77.5  12.5  10.0                                     
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    230/230   (100%)|    0:00/    0:00|    0:00/    0:00|   167.27x|    0:00 
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    230/230   (100%)|    0:00/    0:00|    0:00/    0:00|   167.27x|    0:00 
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil - -------------------------------------------------------------------------------
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil - -------------------------------------------------------------------------------
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    kbps       mono %     long switch short %                                   
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    32.0      100.0        77.4  12.2  10.4                                     
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil -    32.0      100.0        77.4  12.2  10.4                                     
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil - ReplayGain: -13.1dB
2021-02-03 11:30:59.426 [main] INFO  cn.ciemis.base.CommandUtil - ReplayGain: -13.1dB
2021-02-03 11:30:59.428 [main] INFO  cn.ciemis.base.CommandUtil - Process exitValue:0
2021-02-03 11:30:59.428 [main] INFO  cn.ciemis.util.AudioUtil4 - 合并文件:E:\He3\BroadcastR\BroadcastR\mp3\5d857231-2e82-4fc8-99e4-554303209fe5L.mp3
合并mp3:E:\He3\BroadcastR\BroadcastR\mp3\c843c7ce-8ad1-4ade-9bbd-428a17655670.mp3

命令行代码:

CommandUtil.java

public class CommandUtil {
    static Logger LOG = LoggerFactory.getLogger(CommandUtil.class);

    public static int exec(String comm) {
        int finished = 0;
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(comm);
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            LOG.info("<error></error>");
            while ((line = br.readLine()) != null) {
                String encoded = new String(line.getBytes("gbk"), "GB18030");//GB2312/CP936/GB18030
                LOG.info(encoded);
                LOG.info(line);
            }
            int exitVal = proc.waitFor();
            LOG.info("Process exitValue:" + exitVal);
            finished = exitVal;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return finished;
    }
}

JavaFx Log日志输出

界面 :

这个log日志的实现参考:https://stackoverflow.com/questions/24116858/most-efficient-way-to-log-messages-to-javafx-textarea-via-threads-with-simple-cu

项目中使用了两个LOG,一个是GUI的LOG界面LogViewer,还有Log4j用来生成log文件。

完毕。

posted @ 2021-02-03 11:54  杨超杰  阅读(381)  评论(0编辑  收藏  举报