diff --git a/Sources/CSFBAudioEngine/Decoders/SFBFLACDecoder.mm b/Sources/CSFBAudioEngine/Decoders/SFBFLACDecoder.mm index 78377b75..a5af1270 100644 --- a/Sources/CSFBAudioEngine/Decoders/SFBFLACDecoder.mm +++ b/Sources/CSFBAudioEngine/Decoders/SFBFLACDecoder.mm @@ -449,6 +449,16 @@ - (BOOL)decodeIntoBuffer:(AVAudioPCMBuffer *)buffer frameLength:(AVAudioFrameCou return YES; } + if (_streamInfo.total_samples > 0) { + if (_framePosition >= static_cast(_streamInfo.total_samples)) { + return YES; + } + const auto framesRemaining = _streamInfo.total_samples - static_cast(_framePosition); + if (framesRemaining < frameLength) { + frameLength = static_cast(framesRemaining); + } + } + AVAudioFrameCount framesProcessed = 0; for (;;) {