calls/lib/views/play.dart

50 lines
1.4 KiB
Dart
Raw Normal View History

2024-08-17 22:12:49 -04:00
import 'package:flutter/services.dart';
import 'package:just_audio/just_audio.dart' as justaudio;
import 'audio_none.dart'
if (dart.library.io) 'audio_mediakit.dart'
if (dart.library.html) 'audio_web.dart';
import '../pb/stillbox.pb.dart';
2024-08-18 08:27:03 -04:00
class JustAudioDriver {
2024-08-17 22:12:49 -04:00
final initializer = AudioInitializer();
2024-10-14 11:42:48 -04:00
late justaudio.AudioPlayer player;
2024-08-17 22:12:49 -04:00
JustAudioDriver() {
initializer.audioInit();
2024-10-14 11:42:48 -04:00
player = initializer.player;
2024-08-17 22:12:49 -04:00
}
Stream<justaudio.PlayerState> get playerStateStream {
return player.playerStateStream;
}
Future<void> play(Call call) async {
player.setAudioSource(CallBytesSource(call));
await player.play();
}
}
class CallBytesSource extends justaudio.StreamAudioSource {
late Uint8List _buffer;
final Call _call;
factory CallBytesSource(Call call) {
return CallBytesSource._(call, Uint8List.fromList(call.audio));
}
CallBytesSource._(this._call, this._buffer) : super(tag: 'CallBytesSource');
@override
Future<justaudio.StreamAudioResponse> request([int? start, int? end]) async {
// Returning the stream audio response with the parameters
return justaudio.StreamAudioResponse(
sourceLength: _buffer.length,
contentLength: (end ?? _buffer.length) - (start ?? 0),
offset: start ?? 0,
stream: Stream.fromIterable([_buffer.sublist(start ?? 0, end)]),
contentType: _call.audioType,
);
}
}