Selaa lähdekoodia

重复10次22

TitanWong 1 vuosi sitten
vanhempi
commit
1e25155e17

+ 52 - 71
src/main/java/com/zhili/stationcontrol/tts/service/impl/MusicServiceImpl.java

@@ -1,13 +1,15 @@
 package com.zhili.stationcontrol.tts.service.impl;
 
 import com.zhili.stationcontrol.tts.service.MusicService;
-import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.stereotype.Service;
 
-import javax.annotation.PostConstruct;
-import java.util.concurrent.TimeUnit;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import static java.lang.System.out;
 
 /**
  * @author :HuangBin
@@ -17,94 +19,73 @@ import java.util.concurrent.TimeUnit;
 @Service
 @Slf4j
 @EnableScheduling
-public class MusicServiceImpl implements MusicService, Runnable {
+public class MusicServiceImpl implements MusicService {
     Process p;
-    String cmd;
-    //1:开始命令到达,0:无开始命令
-    Boolean startCmd = false;
-    //1:结束命令到达,0:无结束命令
-    Boolean endCmd = false;
-    //1:重复播放,0:不重复
-    Boolean repeat = false;
-
-    Thread thread;
 
-    @PostConstruct
-    public void init() {
-        this.thread = new Thread(this);
-        this.thread.start();
-    }
 
     @Override
     public Integer start(String path, Integer vol) {
         synchronized (this) {
-            cmd = "play -v " + Float.valueOf(new Integer(vol).toString()) / 100 + " " + path;
-            log.info("start music cmd:{}", cmd);
-            startCmd = true;
-            repeat = true;
-            return 0;
+            try {
+                String cmd = "play -v " + Float.valueOf(new Integer(vol).toString()) / 100 + " " + path + " repeat 10";
+                log.info("start music cmd:{}", cmd);
+                if (p != null) {
+                    p.destroyForcibly();
+                    p.waitFor();
+                    p = null;
+                }
+                p = Runtime.getRuntime().exec(cmd);
+                inputProcess(p.getInputStream(), "inputStream");
+                inputProcess(p.getErrorStream(), "errorStream");
+                return 0;
+            } catch (Exception e) {
+                e.printStackTrace();
+                return 1;
+            }
         }
     }
 
     @Override
     public void stop() {
         synchronized (this) {
-            log.info("stop music");
-            endCmd = true;
-            repeat = false;
+            try {
+                log.info("stop music");
+                if (p != null) {
+                    p.destroyForcibly();
+                    p.waitFor();
+                    p = null;
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+
         }
     }
 
-
-    @Override
-    @SneakyThrows
-    public void run() {
-        while (true) {
-            Thread.sleep(100);
-            synchronized (this) {
+    void inputProcess(final InputStream in, String name) {
+        //必须是新线程
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                if (in == null) return;
                 try {
-                    log.info("----------");
-                    log.info("startCmd:{},endCmd:{},repeat:{},cmd:{}", startCmd, endCmd, repeat, cmd);
-                    log.info("p:{},p.isAlive:{}", p, p == null ? null : p.isAlive());
-                    if (startCmd) {
-                        log.info("start");
-                        startCmd = false;
-                        if (p != null) {
-                            p.destroyForcibly();
-                            p.waitFor();
-                            p = null;
-                        }
-                        p = Runtime.getRuntime().exec(cmd);
-                    }
-                    if (endCmd) {
-                        log.info("end");
-                        endCmd = false;
-                        if (p != null) {
-                            p.destroyForcibly();
-                            p.waitFor();
-                            p = null;
-                        }
-                    }
-                    if (!startCmd && !endCmd && repeat && cmd != null) {
-                        if (p != null) {
-                            boolean exit = p.waitFor(500, TimeUnit.MILLISECONDS);
-                            if (exit) {
-                                log.info("p exit repeat");
-                                p = Runtime.getRuntime().exec(cmd);
-                            } else {
-                                log.info("p not exit");
-                                continue;
-                            }
-                        } else {
-                            log.info("p is not null");
-                            p = Runtime.getRuntime().exec(cmd);
+                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
+                    String line = null;
+                    while ((line = br.readLine()) != null) {
+                        if (line != null) {
+                            out.println(line);
                         }
                     }
                 } catch (Exception e) {
-                    e.printStackTrace();
+
+                } finally {
+                    try {
+                        if (in != null) in.close();
+                    } catch (Exception ec) {
+
+                    }
                 }
             }
-
-        }
+        }, name).start();
     }
 }