Browse Source

重复10次9

TitanWong 1 year ago
parent
commit
2796bff2f8

+ 2 - 5
Dockerfile

@@ -1,12 +1,9 @@
 FROM nas.fast-fun.cn:5000/station-control/tts:0.2
 
-#ENV PRO_ENV=test
+
 ENV LANG C.UTF-8
-#RUN mkdir -p /home/download/excel
-COPY ./lib /home/lib
-COPY ./playMusic.sh /home/playMusic.sh
 
-RUN chmod 755 /home/playMusic.sh
+COPY ./lib /home/lib
 
 COPY ./target/zhili-station-control-tts-1.0-SNAPSHOT.jar /home/App.jar
 

+ 0 - 5
playMusic.sh

@@ -1,5 +0,0 @@
-#!bin/bash
-while true
-do
-  play -v $2 $1
-done

+ 58 - 13
src/main/java/com/zhili/stationcontrol/tts/service/impl/MusicServiceImpl.java

@@ -2,10 +2,13 @@ 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;
@@ -17,33 +20,75 @@ import java.io.IOException;
  */
 @Service
 @Slf4j
+@EnableScheduling
 public class MusicServiceImpl implements MusicService {
     Process p;
+    String cmd;
+    //1:开始命令到达,0:无开始命令
+    Boolean startCmd;
+    //1:结束命令到达,0:无结束命令
+    Boolean endCmd;
+    //1:重复播放,0:不重复
+    Boolean repeat;
 
     @Override
     public Integer start(String path, Integer vol) {
-        stop();
-        try {
-//            String cmd = "play -v "+Float.valueOf(new Integer(vol).toString())/100+" "+path+" repeat 10";
-            String cmd = "/home/playMusic.sh "+path+" "+vol;
-            log.info("cmd:{}",cmd);
-            p = Runtime.getRuntime().exec(cmd);
+        synchronized (this) {
+            cmd = "play -v " + Float.valueOf(new Integer(vol).toString()) / 100 + " " + path;
+            startCmd = true;
+            repeat = true;
             return 0;
-        } catch (IOException e) {
-            e.printStackTrace();
-            return 1;
         }
     }
 
     @Override
     public void stop() {
-        if(p!=null){
-            p.destroyForcibly();
-            p=null;
+        synchronized (this) {
+            endCmd = true;
+            repeat = false;
         }
     }
 
+    @Scheduled(fixedRate = 500)
+    public void play() {
+        synchronized (this) {
+            if (startCmd) {
+                startCmd = false;
+                if (p != null) {
+                    p.destroyForcibly();
+                    p = null;
+                }
+                try {
+                    p = Runtime.getRuntime().exec(cmd);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (endCmd) {
+                endCmd = false;
+                if (p != null) {
+                    p.destroyForcibly();
+                    p = null;
+                }
+            }
+        }
+    }
+
+    @Scheduled(fixedRate = 500)
+    public void repeat() {
+        synchronized (this) {
+            if (p == null) {
+                try {
+                    p = Runtime.getRuntime().exec(cmd);
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+
     public static void main(String[] args) {
-        System.out.println(Float.valueOf(new Integer(2).toString())/10);
+        System.out.println(Float.valueOf(new Integer(2).toString()) / 10);
     }
 }