freezer/lib/api/offline_audio_source.dart

35 lines
1 KiB
Dart

import 'dart:io';
import 'package:freezer/api/definitions.dart';
import 'package:just_audio/just_audio.dart';
class OfflineAudioSource extends StreamAudioSource {
final File file;
final StreamInfoCallback? onStreamObtained;
OfflineAudioSource(this.file, {this.onStreamObtained});
@override
Future<StreamAudioResponse> request([int? start, int? end]) async {
// determine content type
final first = await file.openRead(0, 4).join();
bool isFlac = false;
if (first == 'fLaC') isFlac = true;
final length = await file.length();
final stream = file.openRead(start, end);
onStreamObtained?.call(StreamQualityInfo(
format: isFlac ? Format.FLAC : Format.MP3,
source: Source.offline,
quality: null,
size: length));
return StreamAudioResponse(
sourceLength: length,
contentLength: (end ?? length) - (start ?? 0),
offset: start,
stream: stream,
contentType: isFlac ? 'audio/flac' : 'audio/mpeg');
}
}