FileStream.read()没有获得某些语音文件的持续时间

时间:2015-03-12 06:22:40

标签: c#-4.0

下面是我想要获得.DSS语音文件的持续时间的代码。一些文件获取持续时间,但在某些文件中它返回零值,因此我收到“输入字符串格式不正确”的错误。

const int durationOffset = 62;
const int durationLength = 6;
const int headerLength = durationOffset + durationLength;
using (var fileStream = File.OpenRead(Path.Combine(uploadPath, newFileName)))
{
      var fileHeader = new byte[headerLength + 1];
      fileStream.Read(fileHeader, 0, headerLength);

      var hoursData = new byte[2];
      var minutesData = new byte[2];
      var secondsData = new byte[2];

      Array.Copy(fileHeader, durationOffset, hoursData, 0, 2);
      Array.Copy(fileHeader, durationOffset + 2, minutesData, 0, 2);
      Array.Copy(fileHeader, durationOffset + 4, secondsData, 0, 2);
      TimeSpan fileTimeDuration = new  TimeSpan(int.Parse(Encoding.ASCII.GetString(hoursData)), int.Parse(Encoding.ASCII.GetString(minutesData)), int.Parse(Encoding.ASCII.GetString(secondsData)));

      timeDuration = fileTimeDuration.ToString();
}

1 个答案:

答案 0 :(得分:0)

将您的代码放在try catch块

try
{
   //your code
}
catch(IOException ex)
{
  //Handle io exception
}