OpenContacts/lib/widgets/messages/message_audio_player.dart

228 lines
9.3 KiB
Dart
Raw Normal View History

2023-05-02 15:34:36 -04:00
import 'dart:convert';
import 'dart:io' show Platform;
2023-05-02 15:34:36 -04:00
import 'package:contacts_plus_plus/auxiliary.dart';
2023-05-20 15:09:22 -04:00
import 'package:contacts_plus_plus/clients/audio_cache_client.dart';
2023-05-02 15:34:36 -04:00
import 'package:contacts_plus_plus/models/message.dart';
import 'package:contacts_plus_plus/widgets/messages/message_state_indicator.dart';
2023-05-02 15:34:36 -04:00
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
2023-05-20 15:09:22 -04:00
import 'package:provider/provider.dart';
2023-05-02 15:34:36 -04:00
class MessageAudioPlayer extends StatefulWidget {
const MessageAudioPlayer({required this.message, this.foregroundColor, super.key});
2023-05-02 15:34:36 -04:00
final Message message;
final Color? foregroundColor;
2023-05-02 15:34:36 -04:00
@override
State<MessageAudioPlayer> createState() => _MessageAudioPlayerState();
}
2023-05-25 09:50:38 -04:00
class _MessageAudioPlayerState extends State<MessageAudioPlayer> with WidgetsBindingObserver, AutomaticKeepAliveClientMixin {
2023-05-02 15:34:36 -04:00
final AudioPlayer _audioPlayer = AudioPlayer();
2023-05-20 15:09:22 -04:00
Future? _audioFileFuture;
2023-05-02 15:34:36 -04:00
double _sliderValue = 0;
@override
void initState() {
super.initState();
2023-05-18 09:17:05 -04:00
WidgetsBinding.instance.addObserver(this);
2023-05-02 15:34:36 -04:00
}
2023-05-18 09:17:05 -04:00
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
_audioPlayer.stop();
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
2023-05-20 15:09:22 -04:00
final audioCache = Provider.of<AudioCacheClient>(context);
_audioFileFuture = audioCache.cachedNetworkAudioFile(AudioClipContent.fromMap(jsonDecode(widget.message.content)))
.then((value) => _audioPlayer.setFilePath(value.path)).whenComplete(() => _audioPlayer.setLoopMode(LoopMode.off));
2023-05-18 09:17:05 -04:00
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_audioPlayer.dispose();
super.dispose();
}
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-25 09:50:38 -04:00
super.build(context);
if (!Platform.isAndroid) {
return _createErrorWidget("Sorry, audio-messages are not\n supported on this platform.");
}
2023-05-20 15:09:22 -04:00
return FutureBuilder(
future: _audioFileFuture,
builder: (context, snapshot) {
2023-05-21 08:23:53 -04:00
if (snapshot.hasError) {
return SizedBox(
width: 300,
child: Row(
children: [
const Icon(Icons.volume_off),
const SizedBox(width: 8,),
Expanded(
child: Text(
"Failed to load voice message: ${snapshot.error}",
maxLines: 4,
overflow: TextOverflow.ellipsis,
softWrap: true,
),
),
],
),
);
}
return IntrinsicWidth(
2023-05-20 15:09:22 -04:00
child: StreamBuilder<PlayerState>(
stream: _audioPlayer.playerStateStream,
builder: (context, snapshot) {
if (snapshot.hasData) {
final playerState = snapshot.data as PlayerState;
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
2023-05-20 15:09:22 -04:00
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2023-05-21 08:23:53 -04:00
snapshot.hasData ? IconButton(
2023-05-20 15:09:22 -04:00
onPressed: () {
switch (playerState.processingState) {
case ProcessingState.idle:
case ProcessingState.loading:
case ProcessingState.buffering:
break;
case ProcessingState.ready:
if (playerState.playing) {
_audioPlayer.pause();
} else {
_audioPlayer.play();
}
break;
case ProcessingState.completed:
_audioPlayer.seek(Duration.zero);
_audioPlayer.play();
break;
}
},
color: widget.foregroundColor,
2023-05-21 08:23:53 -04:00
icon: SizedBox.square(
dimension: 24,
2023-05-20 15:09:22 -04:00
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)),
),
2023-05-21 08:23:53 -04:00
) : const SizedBox.square(dimension: 24, child: CircularProgressIndicator(),),
2023-05-20 15:09:22 -04:00
StreamBuilder(
stream: _audioPlayer.positionStream,
builder: (context, snapshot) {
_sliderValue = _audioPlayer.duration == null ? 0 : (_audioPlayer.position.inMilliseconds /
(_audioPlayer.duration!.inMilliseconds)).clamp(0, 1);
return StatefulBuilder( // Not sure if this makes sense here...
builder: (context, setState) {
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-20 15:09:22 -04:00
)
],
),
2023-05-20 15:09:22 -04:00
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const SizedBox(width: 4,),
StreamBuilder(
stream: _audioPlayer.positionStream,
builder: (context, snapshot) {
return Text("${snapshot.data?.format() ?? "??"}/${_audioPlayer.duration?.format() ??
"??"}",
style: Theme
.of(context)
.textTheme
.bodySmall
?.copyWith(color: widget.foregroundColor?.withAlpha(150)),
);
}
),
const Spacer(),
MessageStateIndicator(message: widget.message, foregroundColor: widget.foregroundColor,),
],
)
],
2023-05-20 15:09:22 -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
);
}
2023-05-25 09:50:38 -04:00
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
2023-05-02 15:34:36 -04:00
}