TitanWong пре 1 година
родитељ
комит
176a752c8b

+ 39 - 40
src/main/java/com/zhili/stationcontrol/tts/service/impl/MusicServiceImpl.java

@@ -1,17 +1,11 @@
 package com.zhili.stationcontrol.tts.service.impl;
 
 import com.zhili.stationcontrol.tts.service.MusicService;
-import com.zhili.stationcontrol.tts.util.WavPlayUtil;
-import lombok.Synchronized;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.EnableScheduling;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
-import java.io.IOException;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author :HuangBin
@@ -21,7 +15,7 @@ import java.io.IOException;
 @Service
 @Slf4j
 @EnableScheduling
-public class MusicServiceImpl implements MusicService {
+public class MusicServiceImpl implements MusicService, Runnable {
     Process p;
     String cmd;
     //1:开始命令到达,0:无开始命令
@@ -51,43 +45,48 @@ public class MusicServiceImpl implements MusicService {
         }
     }
 
-    @Scheduled(fixedRate = 500)
-    public void play() {
-        synchronized (this) {
-            log.info("startCmd:{}, endCmd:{}, repeat:{}", startCmd, endCmd, repeat);
-            log.info("p:{}, p.isAlive:{}", p, p != null ? p.isAlive() : null);
-            if (startCmd) {
-                startCmd = false;
-                if (p != null) {
-                    p.destroyForcibly();
-                    p = null;
-                }
-                try {
+
+    @Override
+    public void run() {
+        while (true) {
+            try {
+                Thread.sleep(500);
+                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) {
+                    startCmd = false;
+                    if (p != null) {
+                        p.destroyForcibly();
+                        p.waitFor();
+                        p = null;
+                    }
                     p = Runtime.getRuntime().exec(cmd);
-                } catch (IOException e) {
-                    e.printStackTrace();
                 }
-            }
-            if (endCmd) {
-                endCmd = false;
-                if (p != null) {
-                    p.destroyForcibly();
-                    p = null;
+                if (endCmd) {
+                    endCmd = false;
+                    if (p != null) {
+                        p.destroyForcibly();
+                        p.waitFor();
+                        p = null;
+                    }
                 }
-            }
-            if (!startCmd && !endCmd && repeat && p == null && cmd != null) {
-                log.info("repeat:{}", cmd);
-                try {
-                    p = Runtime.getRuntime().exec(cmd);
-                } catch (IOException e) {
-                    e.printStackTrace();
+                if (!startCmd && !endCmd && repeat && cmd != null) {
+                    if (p != null) {
+                        boolean exit = p.waitFor(500, TimeUnit.MILLISECONDS);
+                        if (exit) {
+                            log.info("repeat");
+                            p = Runtime.getRuntime().exec(cmd);
+                        } else {
+                            return;
+                        }
+                    } else {
+                        p = Runtime.getRuntime().exec(cmd);
+                    }
                 }
+            } catch (Exception e) {
+                e.printStackTrace();
             }
         }
     }
-
-
-    public static void main(String[] args) {
-        System.out.println(Float.valueOf(new Integer(2).toString()) / 10);
-    }
 }