2012-09-12 41 views
1
try 
{ 
     string filename = "E:\\sox-14-4-0\\mysamplevoice.wav"; 
     Process p = new Process(); 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe "; 
     p.StartInfo.Arguments = filename + " -n stat"; 
     p.Start(); 
     string output = p.StandardOutput.ReadToEnd(); 
     p.WaitForExit(); 
} 
catch(Exception Ex) 
{ 
     Console.WriteLine(Ex.Message); 
} 

输出始终为空。当我运行在命令提示符sox命令我能得到这样的回应:Sox返回一些值,但StandardOutput.ReadToEnd()返回空

E:\sox-14-4-0>sox mysamplevoice.wav -n stat 
Samples read:    26640 
Length (seconds):  3.330000 
Scaled by:   2147483647.0 
Maximum amplitude:  0.515625 
Minimum amplitude: -0.734375 
Midline amplitude: -0.109375 
Mean norm:   0.058691 
Mean amplitude:  0.000122 
RMS  amplitude:  0.101146 
Maximum delta:   0.550781 
Minimum delta:   0.000000 
Mean delta:   0.021387 
RMS  delta:   0.041831 
Rough frequency:   526 
Volume adjustment:  1.362 

当运行在C#中相同的命令,我得到了相同的结果,而是“输出”值为空。

+0

我不知道,即时通讯新的N I认为some1一直错误地编辑它。为此道歉,不会再这样做。 –

+0

在这些名称末尾带有♦的用户是主持人,因此您通常不应该回复他们的操作。如果您有问题/不明白编辑的原因,您可以使用包含@TheEditorsNickname的注释“编辑”编辑器。 – Mat

+0

@Mat:谢谢你的信息。 –

回答

4

你确定sox.exe写入STDOUT而不写入STDERR吗?

您可以尝试使用OutputDataReceived事件读取数据。

string filename = "E:\\sox-14-4-0\\mysamplevoice.wav"; 
Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardError = true; 
p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe "; 
p.StartInfo.Arguments = filename + " -n stat"; 

p.OutputDataReceived += process_OutputDataReceived; 
p.ErrorDataReceived += process_ErrorDataReceived; 

p.Start(); 
p.BeginErrorReadLine(); 
p.BeginOutputReadLine(); 


void process_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    string s = e.Data; 
} 

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    string s = e.Data; 
} 
+0

谢谢,试了你的代码。我收到一个异常错误“标准错误未被重定向”。调试到达p.BeginErrorReadLine()时会发生这种情况; –

+2

加'p.StartInfo.RedirectStandardError = true;' –

+0

是的。错过了那条线。现在添加。谢谢@ChibuezeOpata –

0

我也碰到过这个问题。为什么SoX写入StandardError?

万一别人运行到这个问题也解决原来的问题可能只是增加2线

p.StartInfo.RedirectStandardError = true; // <-- this 
... 
string output = p.StandardOutput.ReadToEnd(); 
if(output == "") output = p.StandardError.ReadToEnd(); // <-- and this