Administrator
2023-08-05 bc8ca5f34b3c8f163bbfdda49a1b085aa8b4f019
ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java
@@ -15,6 +15,7 @@
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
@@ -33,33 +34,47 @@
 * @Version: 1.0
 **/
@Service
@Slf4j
@Slf4j(topic = "cmd")
public class MediaService implements IMediaService {
    @Resource
    MediaClient mediaClient;
    @Value("${mediamtx.host}")
    String mediamtxHost;
    @Value("${mediamtx.enabled}")
    Boolean mediamtxEnabled;
    String processName = "mediamtx.exe";
    @PostConstruct
    public void initMediaMtx() {
        if (Platform.isWindows()) {
            String processName="mediamtx.exe";
            String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + "mediamtx.exe";
            String ymlPath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + "mediamtx.yml";
        if (mediamtxEnabled) {
            if (Platform.isWindows()) {
                String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + "mediamtx.exe";
                String ymlPath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "mediamtx" + File.separator + "mediamtx.yml";
            List<String> cmd = new ArrayList<>();
            cmd.add(exePath);
            cmd.add(ymlPath);
            if (CmdUtils.isProcessRunning(processName)) {
                // 进程已经在运行,结束该进程
                CmdUtils.stopProcess(processName);
            }
            // 启动后台进程
             CmdUtils.commandStart(processName,cmd);
            // 启动cmd窗口
                List<String> cmd = new ArrayList<>();
                cmd.add(exePath);
                cmd.add(ymlPath);
                if (CmdUtils.isProcessRunning(processName)) {
                    // 进程已经在运行,结束该进程
                    CmdUtils.stopProcess(processName);
                }
                // 启动后台进程
                CmdUtils.commandStart(processName, cmd, null);
                // 启动cmd窗口
//            String[] command = {"cmd","/c","start",exePath,ymlPath};
//            CmdUtils.commandStart(command);
            }
        }
    }
    @PreDestroy
    public void destroyMediaMtx() {
        log.info("销毁mediaMtx");
        if (CmdUtils.isProcessRunning(processName)) {
            // 进程已经在运行,结束该进程
            CmdUtils.stopProcess(processName);
        }
    }