diff --git a/lib/views/audio_mediakit.dart b/lib/views/audio_mediakit.dart new file mode 100644 index 0000000..a7f2476 --- /dev/null +++ b/lib/views/audio_mediakit.dart @@ -0,0 +1,7 @@ +import 'package:just_audio_media_kit/just_audio_media_kit.dart'; + +class AudioInitializer { + void audioInit() { + JustAudioMediaKit.ensureInitialized(); + } +} diff --git a/lib/views/audio_none.dart b/lib/views/audio_none.dart new file mode 100644 index 0000000..6c6f840 --- /dev/null +++ b/lib/views/audio_none.dart @@ -0,0 +1,3 @@ +class AudioInitializer { + void audioInit() {} +} diff --git a/lib/views/audio_web.dart b/lib/views/audio_web.dart new file mode 100644 index 0000000..6c6f840 --- /dev/null +++ b/lib/views/audio_web.dart @@ -0,0 +1,3 @@ +class AudioInitializer { + void audioInit() {} +} diff --git a/lib/views/play.dart b/lib/views/play.dart new file mode 100644 index 0000000..ff05ad5 --- /dev/null +++ b/lib/views/play.dart @@ -0,0 +1,83 @@ +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 'package:audioplayers/audioplayers.dart' as auplay; +//import 'dart:io' show Platform; + +import '../pb/stillbox.pb.dart'; + +abstract class AudioDriver { + Future play(Call call); + Stream get playerStateStream; +} + +class Player { + late AudioDriver driver; + Player() { +// if (Platform.isMacOS || Platform.isIOS) { + driver = JustAudioDriver(); +// } else { +// driver = AudioPlayersDriver(); +// } + } + + Future play(Call call) async { + await driver.play(call); + } +} + +/* +class AudioPlayersDriver implements AudioDriver { + final player = auplay.AudioPlayer(); + + @override + Future play(Call call) { + return player.play(auplay.BytesSource(Uint8List.fromList(call.audio))); + } +} +*/ + +class JustAudioDriver implements AudioDriver { + final player = justaudio.AudioPlayer(); + final initializer = AudioInitializer(); + + JustAudioDriver() { + initializer.audioInit(); + } + + @override + Stream get playerStateStream { + return player.playerStateStream; + } + + @override + Future 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 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, + ); + } +}