mirror of
https://github.com/amigan/calls.git
synced 2025-01-31 13:32:37 -05:00
Daniel Ponte
f41790d631
commit56ec3e3c53
Author: Daniel Ponte <amigan@gmail.com> Date: Wed Aug 14 15:49:50 2024 -0400 Fix exception in just_audio_media_kit commitc80625d07e
Author: Daniel Ponte <amigan@gmail.com> Date: Wed Aug 14 15:37:25 2024 -0400 Fix justaudio commitb7af6f28bb
Author: Daniel Ponte <amigan@gmail.com> Date: Wed Aug 14 14:44:18 2024 -0400 use justaudio only commit87d5354943
Author: Daniel Ponte <amigan@gmail.com> Date: Wed Aug 14 14:36:58 2024 -0400 Multiple audio drivers commit1e6b0d0439
Author: Daniel Ponte <amigan@gmail.com> Date: Wed Aug 14 14:08:14 2024 -0400 Fix on macos commit47cca3e3c0
Author: Daniel Ponte <amigan@gmail.com> Date: Wed Aug 14 13:10:12 2024 -0400 osx
75 lines
1.9 KiB
Dart
75 lines
1.9 KiB
Dart
import 'package:flutter/services.dart';
|
|
import 'package:just_audio/just_audio.dart' as justaudio;
|
|
import 'package:just_audio_media_kit/just_audio_media_kit.dart';
|
|
//import 'package:audioplayers/audioplayers.dart' as auplay;
|
|
//import 'dart:io' show Platform;
|
|
|
|
import '../pb/stillbox.pb.dart';
|
|
|
|
abstract class AudioDriver {
|
|
Future<void> play(Call call);
|
|
}
|
|
|
|
class Player {
|
|
late AudioDriver driver;
|
|
Player() {
|
|
// if (Platform.isMacOS || Platform.isIOS) {
|
|
driver = JustAudioDriver();
|
|
// } else {
|
|
// driver = AudioPlayersDriver();
|
|
// }
|
|
}
|
|
|
|
Future<void> play(Call call) {
|
|
return driver.play(call);
|
|
}
|
|
// TODO make a queue
|
|
}
|
|
|
|
/*
|
|
class AudioPlayersDriver implements AudioDriver {
|
|
final player = auplay.AudioPlayer();
|
|
|
|
@override
|
|
Future<void> play(Call call) {
|
|
return player.play(auplay.BytesSource(Uint8List.fromList(call.audio)));
|
|
}
|
|
}
|
|
*/
|
|
|
|
class JustAudioDriver implements AudioDriver {
|
|
final player = justaudio.AudioPlayer();
|
|
|
|
JustAudioDriver() {
|
|
JustAudioMediaKit.ensureInitialized();
|
|
}
|
|
|
|
@override
|
|
Future<void> play(Call call) async {
|
|
player.setAudioSource(CallBytesSource(call));
|
|
return 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,
|
|
);
|
|
}
|
|
}
|