2012-09-12 40 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