|
@@ -2,10 +2,13 @@ package com.zhili.stationcontrol.tts.service.impl;
|
|
|
|
|
|
import com.zhili.stationcontrol.tts.service.MusicService;
|
|
import com.zhili.stationcontrol.tts.service.MusicService;
|
|
import com.zhili.stationcontrol.tts.util.WavPlayUtil;
|
|
import com.zhili.stationcontrol.tts.util.WavPlayUtil;
|
|
|
|
+import lombok.Synchronized;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
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 org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
@@ -17,33 +20,75 @@ import java.io.IOException;
|
|
*/
|
|
*/
|
|
@Service
|
|
@Service
|
|
@Slf4j
|
|
@Slf4j
|
|
|
|
+@EnableScheduling
|
|
public class MusicServiceImpl implements MusicService {
|
|
public class MusicServiceImpl implements MusicService {
|
|
Process p;
|
|
Process p;
|
|
|
|
+ String cmd;
|
|
|
|
+ //1:开始命令到达,0:无开始命令
|
|
|
|
+ Boolean startCmd;
|
|
|
|
+ //1:结束命令到达,0:无结束命令
|
|
|
|
+ Boolean endCmd;
|
|
|
|
+ //1:重复播放,0:不重复
|
|
|
|
+ Boolean repeat;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public Integer start(String path, Integer vol) {
|
|
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;
|
|
return 0;
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- return 1;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void stop() {
|
|
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) {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|