【问题标题】:Running SoX from Processing/Java on Windows computer在 Windows 计算机上从 Processing/Java 运行 SoX
【发布时间】:2015-05-09 21:42:06
【问题描述】:

这可能是一件容易的事 - 但在这一点上它让我发疯了。我正在尝试从我的 Mac 计算机上运行平稳且没有问题的处理中运行 SoX。我需要将代码迁移到 Windows 7 机器,但由于某种原因无法正常工作。从处理中与终端交谈工作正常。我在正确的文件夹中(也安装了 SoX 的草图数据文件夹),因为我可以运行“dir”等命令并打印正确的内容 - 但是一旦我尝试运行 sox.exe,什么也没有发生(得到退出值 1)。直接从 cmd 终端运行 sox.exe 可以正常工作。这是我正在尝试做的一个示例:

void playBackYear (){

soxPlay = "cmd /c sox.exe year.wav -d";
  println (soxPlay);
 try {
    File workingDir = new File(sketchPath("data"));
    Process p=Runtime.getRuntime().exec(soxPlay, null, workingDir);
    p.waitFor(); 
    BufferedReader reader=new BufferedReader(
    new InputStreamReader(p.getInputStream())
      ); 
    String line; 
    while ( (line = reader.readLine ()) != null) 
    { 
      println(line);
    }
   int exitVal = p.waitFor();
   System.out.println("Exited with error code "+exitVal);
  } 

  catch(IOException e1) {
   System.err.println("Caught IOException: " + e1.getMessage());
   System.out.println( "error 1" );
  } 
  catch(InterruptedException e2) {
   System.err.println("Caught IOException: " + e2.getMessage());
   System.out.println( "error 2" );
  } 

}

所以问题是我在这里做错了什么? 任何帮助表示赞赏。

【问题讨论】:

  • 第一步应该是在这些 catch 块内放一些东西。到目前为止,您正在隐藏您遇到的任何错误。
  • @mattuck 你试过open()吗?
  • 嗨,凯文和乔治,非常感谢您的回复。我在 catch() 块中添加了一些消息,但它没有返回任何错误。在运行 try() 之前还尝试了 open() 函数。不幸的是没有运气。我想知道如何判断它是否真的在打开 SoX 以及 int exitVal = p.waitFor(); System.out.println("Exited with error code "+exitVal); 中的错误代码 1 是什么意思

标签: java windows cmd processing sox


【解决方案1】:

我编写了一个小型包装应用程序,用 java 包装 sox 二进制文件。如果您对完整项目感兴趣,请在 GitHub 上查看:sox java wrapper project

这是,我是如何解决这个问题的:

private List<String> arguments = new ArrayList<String>();
// add sox arguments to this list above

public void execute() throws IOException {
    File soxBinary = new File(soXBinaryPath);
    if (!soxBinary.exists()) {
        throw new FileNotFoundException("Sox binary is not available under the following path: " + soXBinaryPath);
    }

    arguments.add(0, soXBinaryPath);
    logger.debug("Sox arguments: {}", arguments);
    ProcessBuilder processBuilder = new ProcessBuilder(arguments);
    processBuilder.redirectErrorStream(true);
    Process process = null;
    IOException errorDuringExecution = null;
    try {
        process = processBuilder.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            logger.debug(line);
        }
    } catch (IOException e) {
        errorDuringExecution = e;
        logger.error("Error while running Sox. {}", e.getMessage());
    } finally {
        arguments.clear();
        if (process != null) {
            process.destroy();
        }
        if (errorDuringExecution != null) {
            throw errorDuringExecution;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多