20220702-NAudio 雜記
VS Code 常用快速鍵 備忘
解決 NAudio 轉 mp3 檔案時出現的 COMException
- 執行 MediaFoundationEncoder.EncodeToMp3 時出現以下錯誤訊息
1 | 未處理的例外狀況: System.Runtime.InteropServices.COMException: 因為已呼叫 Shutdown(),所以要求無效。 (發生例外狀況於 HRESULT: 0xC00D3E85) |
根據錯誤訊息,找到以下文章及解法
The request is invalid because Shutdown() has been called. (Exception from HRESULT: 0xC00D3E85)英文錯誤訊息
1
System.Runtime.InteropServices.COMException: 'The request is invalid because Shutdown() has been called. (Exception from HRESULT: 0xC00D3E85)'
參考解法
1
2
3
4
5
6using (var wfr = new WaveFileReader(ms))
{
MediaFoundationApi.Startup();
MediaFoundationEncoder.EncodeToMp3(wfr, @"C:\Temp\Test.mp3", 160000);
MediaFoundationApi.Shutdown();
}
Task / async 寫法練習
參考文章
Is there a difference with Task.Run a void method and Task method returning null?參考寫法
1
2
3
4
5
6
7
8private async void btnStart_Click(object sender, EventArgs e)
{
cts = new CancellationTokenSource();
token = cts.Token;
var task = Task.Run(() => WriteSomeLines(), token);
await task;
rtbLoops.Text += "Task complete";
}
本日最終寫法
1 | static async Task ConvertWavToMp3Async(string wavPath, string mp3Path) |
VS Code 常用快速鍵 備忘
CodePen / VS Code 常用快速鍵
往前縮排: Shift + tab