---
title: "Seek to an exact MP3 sample in C#"
url: https://recursiveai.org/oneaudio/cookbook/seek-to-sample-in-mp3/
description: "Seek to an exact decoded MP3 sample frame in C#, then compare the PCM bytes with a sequential decode at the same position."
version: "1.7.2"
---

# Seek to an exact MP3 sample in C#

**oneaudio** is a free, open-source audio library for .NET from Recursive AI. MIT or Apache-2.0, no bundled native binaries.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneAudio) · [GitHub](https://github.com/recursive-ai-oss/oneaudio-csharp) · [Docs](https://recursiveai.org/oneaudio/docs/) · [Download example (.zip)](https://recursiveai.org/oneaudio/cookbook/seek-to-sample-in-mp3/seek-to-sample-in-mp3.zip)

Use `Seek` with a decoded frame index. A frame is one time position across all channels. Try the included stereo `tone.mp3`.

```shell
dotnet add package RecursiveAI.OneAudio
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneAudio;

using var mp3 = Audio.Open("tone.mp3");
mp3.Seek(98765);
Console.WriteLine($"Position: {mp3.PositionFrames}");
var frame = new float[mp3.Format.Channels];
int read = mp3.Read(frame);
Console.WriteLine($"Frames read: {read}");
```

```text
Position: 98765
Frames read: 1
```

Unzip [the example](https://recursiveai.org/oneaudio/cookbook/seek-to-sample-in-mp3/seek-to-sample-in-mp3.zip), then `dotnet run`.

At index 98,765, `Read` fills the buffer with one left and one right sample from the 44.1 kHz tone.

## The full program

Two decoders open the same file. One seeks directly; the other reads and discards frames until it reaches the target. Both read the next 32 frames, and the program compares the bytes of the decoded floats, not just their values rounded for display.

```csharp
using System.Runtime.InteropServices;
using OneAudio;

long target = args.Length == 0 ? 98765 : long.Parse(args[0]);
using var seek = Audio.Open("tone.mp3");
using var linear = Audio.Open("tone.mp3");
int channels = seek.Format.Channels;
const int window = 32;
if (!seek.CanSeek || target < 0 || target > seek.LengthFrames - window)
    throw new ArgumentOutOfRangeException(nameof(target));

seek.Seek(target);
Console.WriteLine($"Decoded position: {seek.PositionFrames}");

var scratch = new float[4096 * channels];
while (linear.PositionFrames < target)
{
    int step = (int)Math.Min(4096, target - linear.PositionFrames);
    if (linear.Read(scratch.AsSpan(0, step * channels)) == 0)
        throw new EndOfStreamException();
}

var afterSeek = ReadWindow(seek);
var sequential = ReadWindow(linear);
bool equal = MemoryMarshal.AsBytes(afterSeek.AsSpan())
    .SequenceEqual(MemoryMarshal.AsBytes(sequential.AsSpan()));
Console.WriteLine($"Compared {window} frames, {channels} channels.");
Console.WriteLine($"Bit-identical: {equal}");

float[] ReadWindow(IAudioDecoder decoder)
{
    var samples = new float[window * channels];
    int frames = 0;
    while (frames < window)
    {
        int read = decoder.Read(samples.AsSpan(frames * channels));
        if (read == 0) throw new EndOfStreamException();
        frames += read;
    }
    return samples;
}
```

```text
Decoded position: 98765
Compared 32 frames, 2 channels.
Bit-identical: True
```

On a non-seekable input such as a pipe, `Seek` throws `NotSupportedException`; read forward instead or save the input to a seekable stream. A `Read` can return fewer frames than requested, so the full program keeps reading until the window is full or it reaches the end.

## Variations

**Seek to 750 ms.** At 44,100 frames per second that’s frame 33,075. Run the full program with that frame index:

```shell
dotnet run -- 33075
```

```text
Decoded position: 33075
Compared 32 frames, 2 channels.
Bit-identical: True
```

## Related recipes

- [Decode MP3 to WAV in C#](https://recursiveai.org/oneaudio/cookbook/decode-mp3/)
- [Decode audio in blocks](https://recursiveai.org/oneaudio/cookbook/streaming-decode/)

---

## The short answer

Open a seekable MP3 with `Audio.Open`, pass a zero-based decoded frame index to `Seek`, then call `Read`. An MP3 frame is a compressed block containing many such positions. You don’t need to round to a compressed block boundary.

## How do I seek to a specific time in an MP3?

Multiply seconds by `Format.SampleRate` and convert to a whole frame. Pick how to round based on your task: for the nearest position, round to the nearest integer; for the first frame at or after a cue, round up. Check the result against `LengthFrames` if it’s known. If you need a window of 32 frames, the last usable start is 32 frames before the end, not the index of the last sample. Don’t multiply the seek position by the channel count. That multiplication is for sizing or slicing the destination buffer, where left and right samples are stored next to each other.

## Why doesn’t seeking to a byte offset give the same audio as decoding from the start?

Compressed frames aren’t indexed by decoded sample, and a variable-bitrate file doesn’t use the same number of bytes for each block. Finding the correct compressed frame is only part of the job: the MP3 bit reservoir can borrow data from earlier frames, and the synthesis filter needs earlier audio to rebuild its state.

The [oneaudio MP3 decoder](https://recursiveai.org/oneaudio/docs/api/#mp3) builds its own index by scanning frame headers; it doesn’t use Xing or VBRI seek tables. It starts decoding before the target to rebuild decoder state, using a 48-compressed-frame lookback to select an index entry. For files with usable gapless information in a Xing/Info header with the LAME extension, the decoder removes the reported delay and padding from the decoded timeline; without that information the decoder doesn’t trim them. Compare against a sequential read from the same decoder so both sides use the same timeline. “Bit-identical” here means the two reads of the decoded MP3 agree, not that the MP3 restored the original uncompressed recording.
