Skip to content

Commit 87eaed6

Browse files
committed
Bug fix: handle offset frame sync.
1 parent 60f2a47 commit 87eaed6

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.3
2+
3+
- Bug fix: handle offset frame sync.
4+
15
## 0.2.2
26

37
- Begin adding support for ID3 metadata.

lib/src/mp3_processor.dart

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,21 @@ class MP3Processor {
408408
return e;
409409
}
410410

411+
int _findNextFrameSync(Uint8List bytes) {
412+
int result = -1;
413+
414+
for (var b = 0; b < bytes.length - 1; b++) {
415+
final frameSync1 = bytes[b] & frameSyncA;
416+
final frameSync2 = bytes[b+1] & frameSyncB;
417+
418+
if (frameSync1 == 0xFF && frameSync2 == 0xE0) {
419+
result = b;
420+
}
421+
}
422+
423+
return result;
424+
}
425+
411426
/// Processes raw bytes to extract [MP3Info].
412427
MP3Info _processBytes(Uint8List bytes, {bool includeID3 = true}) {
413428
var firstFrameOffset = 0;
@@ -425,12 +440,11 @@ class MP3Processor {
425440
throw InvalidMP3FileException('File is too short to be a valid MP3 file');
426441
}
427442

428-
final frameHeaderBytes =
429-
bytes.sublist(firstFrameOffset, firstFrameOffset + 10);
430-
final frameSync1 = frameHeaderBytes[0] & frameSyncA;
431-
final frameSync2 = frameHeaderBytes[1] & frameSyncB;
443+
final nextFrame = _findNextFrameSync(bytes);
432444

433-
if (frameSync1 == 0xFF && frameSync2 == 0xE0) {
445+
if (nextFrame != -1) {
446+
final frameHeaderBytes =
447+
bytes.sublist(nextFrame, nextFrame + 10);
434448
final fileSize = bytes.length - firstFrameOffset;
435449
final version = _processMpegVersion(frameHeaderBytes);
436450
final layer = _processMpegLayer(frameHeaderBytes);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: mp3_info
22
description: A package for extracting key meta information from an MP3 file including sample rate, bitrate and duration. Written in pure Dart.
3-
version: 0.2.2
3+
version: 0.2.3
44
homepage: https://github.com/amugofjava/mp3_info
55

66
environment:

0 commit comments

Comments
 (0)