不要重新发明轮子。使用外部库(例如 NAudio)来完成繁重的工作。
你可以这样使用它:
Mp3FileReader reader = new Mp3FileReader("<YourMP3>.mp3");
TimeSpan duration = reader.TotalTime;
Run Code Online (Sandbox Code Playgroud)
当然,另一种选择是这个答案。
谢谢大家的帮助。
我尝试使用 TagLib,它工作正常。
TagLib.File f = TagLib.File.Create(<pathToFile>, TagLib.ReadStyle.Average);
var duration = (int)f.Properties.Duration.TotalSeconds;
Run Code Online (Sandbox Code Playgroud)