2023-05-02 15:34:36 -04:00
|
|
|
import 'dart:convert';
|
2023-05-04 08:44:40 -04:00
|
|
|
import 'dart:io' show Platform;
|
2023-05-02 15:34:36 -04:00
|
|
|
|
|
|
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
|
|
|
import 'package:contacts_plus_plus/models/message.dart';
|
2023-05-06 13:24:28 -04:00
|
|
|
import 'package:contacts_plus_plus/widgets/messages/message_state_indicator.dart';
|
2023-05-15 09:45:41 -04:00
|
|
|
import 'package:dynamic_color/dynamic_color.dart';
|
2023-05-02 15:34:36 -04:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:just_audio/just_audio.dart';
|
|
|
|
|
|
|
|
class MessageAudioPlayer extends StatefulWidget {
|
2023-05-15 09:45:41 -04:00
|
|
|
const MessageAudioPlayer({required this.message, this.foregroundColor, super.key});
|
2023-05-02 15:34:36 -04:00
|
|
|
|
|
|
|
final Message message;
|
2023-05-15 09:45:41 -04:00
|
|
|
final Color? foregroundColor;
|
2023-05-02 15:34:36 -04:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<MessageAudioPlayer> createState() => _MessageAudioPlayerState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MessageAudioPlayerState extends State<MessageAudioPlayer> {
|
|
|
|
final AudioPlayer _audioPlayer = AudioPlayer();
|
|
|
|
double _sliderValue = 0;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-05-04 08:44:40 -04:00
|
|
|
if (Platform.isAndroid) {
|
2023-05-04 15:54:51 -04:00
|
|
|
_audioPlayer.setUrl(
|
|
|
|
Aux.neosDbToHttp(AudioClipContent
|
|
|
|
.fromMap(jsonDecode(widget.message.content))
|
|
|
|
.assetUri),
|
|
|
|
preload: true).whenComplete(() => _audioPlayer.setLoopMode(LoopMode.off));
|
2023-05-04 08:44:40 -04:00
|
|
|
}
|
2023-05-02 15:34:36 -04:00
|
|
|
}
|
|
|
|
|
2023-05-04 15:54:51 -04:00
|
|
|
Widget _createErrorWidget(String error) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Icon(Icons.error_outline, color: Theme
|
|
|
|
.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.error,),
|
|
|
|
const SizedBox(height: 4,),
|
|
|
|
Text(error, textAlign: TextAlign.center,
|
|
|
|
softWrap: true,
|
|
|
|
maxLines: 3,
|
|
|
|
style: Theme
|
|
|
|
.of(context)
|
|
|
|
.textTheme
|
|
|
|
.bodySmall
|
|
|
|
?.copyWith(color: Theme
|
|
|
|
.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.error),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-02 15:34:36 -04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-04 08:44:40 -04:00
|
|
|
if (!Platform.isAndroid) {
|
2023-05-04 15:54:51 -04:00
|
|
|
return _createErrorWidget("Sorry, audio-messages are not\n supported on this platform.");
|
2023-05-04 08:44:40 -04:00
|
|
|
}
|
2023-05-02 15:34:36 -04:00
|
|
|
return IntrinsicWidth(
|
|
|
|
child: StreamBuilder<PlayerState>(
|
2023-05-04 15:54:51 -04:00
|
|
|
stream: _audioPlayer.playerStateStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
final playerState = snapshot.data as PlayerState;
|
|
|
|
return Column(
|
2023-05-15 09:45:41 -04:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2023-05-04 15:54:51 -04:00
|
|
|
children: [
|
|
|
|
Row(
|
2023-05-15 09:45:41 -04:00
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
2023-05-04 15:54:51 -04:00
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
switch (playerState.processingState) {
|
|
|
|
case ProcessingState.idle:
|
|
|
|
case ProcessingState.loading:
|
|
|
|
case ProcessingState.buffering:
|
|
|
|
break;
|
|
|
|
case ProcessingState.ready:
|
2023-05-15 09:45:41 -04:00
|
|
|
if (playerState.playing) {
|
|
|
|
_audioPlayer.pause();
|
|
|
|
} else {
|
|
|
|
_audioPlayer.play();
|
|
|
|
}
|
2023-05-04 15:54:51 -04:00
|
|
|
break;
|
|
|
|
case ProcessingState.completed:
|
|
|
|
_audioPlayer.seek(Duration.zero);
|
|
|
|
_audioPlayer.play();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2023-05-15 09:45:41 -04:00
|
|
|
color: widget.foregroundColor,
|
2023-05-04 15:54:51 -04:00
|
|
|
icon: SizedBox(
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
child: playerState.processingState == ProcessingState.loading
|
|
|
|
? const Center(child: CircularProgressIndicator(),)
|
|
|
|
: Icon(((_audioPlayer.duration ?? Duration.zero) - _audioPlayer.position).inMilliseconds <
|
|
|
|
10 ? Icons.replay
|
|
|
|
: (playerState.playing ? Icons.pause : Icons.play_arrow)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
StreamBuilder(
|
|
|
|
stream: _audioPlayer.positionStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
_sliderValue = (_audioPlayer.position.inMilliseconds /
|
|
|
|
(_audioPlayer.duration?.inMilliseconds ?? 0)).clamp(0, 1);
|
2023-05-15 09:45:41 -04:00
|
|
|
return StatefulBuilder( // Not sure if this makes sense here...
|
2023-05-04 15:54:51 -04:00
|
|
|
builder: (context, setState) {
|
2023-05-15 09:45:41 -04:00
|
|
|
return SliderTheme(
|
|
|
|
data: SliderThemeData(
|
|
|
|
inactiveTrackColor: widget.foregroundColor?.withAlpha(100),
|
|
|
|
),
|
|
|
|
child: Slider(
|
|
|
|
thumbColor: widget.foregroundColor,
|
|
|
|
value: _sliderValue,
|
|
|
|
min: 0.0,
|
|
|
|
max: 1.0,
|
|
|
|
onChanged: (value) async {
|
|
|
|
_audioPlayer.pause();
|
|
|
|
setState(() {
|
|
|
|
_sliderValue = value;
|
|
|
|
});
|
|
|
|
_audioPlayer.seek(Duration(
|
|
|
|
milliseconds: (value * (_audioPlayer.duration?.inMilliseconds ?? 0)).round(),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
),
|
2023-05-04 15:54:51 -04:00
|
|
|
);
|
|
|
|
}
|
2023-05-02 15:34:36 -04:00
|
|
|
);
|
|
|
|
}
|
2023-05-04 15:54:51 -04:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
2023-05-15 09:45:41 -04:00
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
2023-05-04 15:54:51 -04:00
|
|
|
children: [
|
2023-05-15 09:45:41 -04:00
|
|
|
const SizedBox(width: 4,),
|
2023-05-04 15:54:51 -04:00
|
|
|
StreamBuilder(
|
|
|
|
stream: _audioPlayer.positionStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return Text("${snapshot.data?.format() ?? "??"}/${_audioPlayer.duration?.format() ??
|
2023-05-15 09:45:41 -04:00
|
|
|
"??"}",
|
|
|
|
style: Theme
|
|
|
|
.of(context)
|
|
|
|
.textTheme
|
|
|
|
.bodySmall
|
|
|
|
?.copyWith(color: widget.foregroundColor?.withAlpha(150)),
|
|
|
|
);
|
2023-05-04 15:54:51 -04:00
|
|
|
}
|
|
|
|
),
|
|
|
|
const Spacer(),
|
2023-05-15 09:45:41 -04:00
|
|
|
MessageStateIndicator(message: widget.message, foregroundColor: widget.foregroundColor,),
|
2023-05-04 15:54:51 -04:00
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
FlutterError.reportError(FlutterErrorDetails(exception: snapshot.error!, stack: snapshot.stackTrace));
|
|
|
|
return _createErrorWidget("Failed to load audio-message.");
|
|
|
|
} else {
|
|
|
|
return const Center(child: CircularProgressIndicator(),);
|
|
|
|
}
|
2023-05-02 15:34:36 -04:00
|
|
|
}
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|