You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用.NET代码获取.spx音频文件的时长

问题描述

尝试使用NAudio及相关库读取.spx格式音频文件的时长,但未找到有效解决方法,当前使用的代码如下:

using NAudio.Wave;
using System;

class Program
{
    static void Main(string[] args)
    {
        string filePath = "example.spx"; // replace with your file path
        var reader = new NAudio.Vorbis.VorbisWaveReader(filePath);
        TimeSpan duration = reader.TotalTime;
        Console.WriteLine("Duration: " + duration.ToString());
    }
}
解决方法
  • .spx是采用Speex编码的音频文件,NAudio自带的VorbisWaveReader仅支持Ogg Vorbis格式,无法直接解析Speex编码内容。
  • 需使用专门支持Speex解码的NAudio.Speex扩展库,步骤如下:
    1. 通过NuGet包管理器安装NAudio.Speex包
    2. 使用SpeexWaveReader替代VorbisWaveReader读取文件,示例代码:
using NAudio.Wave;
using NAudio.Speex;
using System;

class Program
{
    static void Main(string[] args)
    {
        string filePath = "example.spx";
        using var reader = new SpeexWaveReader(filePath);
        TimeSpan duration = reader.TotalTime;
        Console.WriteLine($"Duration: {duration}");
    }
}
  • 若安装NAudio.Speex后仍有问题,需确认.spx文件是否为标准Speex编码格式,部分非标准封装的文件可能需要先转码为标准格式再处理。

内容的提问来源于stack exchange,提问作者Pratik Ingole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:07:09