208 lines
8.7 KiB
Dart
208 lines
8.7 KiB
Dart
// ignore_for_file: implementation_imports, library_private_types_in_public_api
|
|
|
|
import 'dart:math';
|
|
|
|
import 'package:audio_video_progress_bar/audio_video_progress_bar.dart';
|
|
import 'package:didvan/config/design_config.dart';
|
|
import 'package:didvan/config/theme_data.dart';
|
|
import 'package:didvan/constants/app_icons.dart';
|
|
import 'package:didvan/services/media/media.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
import 'package:didvan/utils/date_time.dart';
|
|
import 'package:didvan/views/ai/widgets/message_bar_btn.dart';
|
|
import 'package:didvan/views/direct/widgets/message.dart';
|
|
import 'package:didvan/views/widgets/audio/audio_slider.dart';
|
|
import 'package:didvan/views/widgets/didvan/text.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
|
|
class AudioWave extends StatefulWidget {
|
|
final String file;
|
|
final double loadingPaddingSize;
|
|
const AudioWave({Key? key, required this.file, this.loadingPaddingSize = 0})
|
|
: super(key: key);
|
|
|
|
@override
|
|
_AudioWaveState createState() => _AudioWaveState();
|
|
}
|
|
|
|
class _AudioWaveState extends State<AudioWave> {
|
|
bool loading = false;
|
|
Duration? totalDuration;
|
|
|
|
final int id =
|
|
DateTime.now().millisecondsSinceEpoch ~/ 1000 * Random().nextInt(1000000);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
await MediaService.getDuration(src: widget.file).then((duration) {
|
|
setState(() {
|
|
totalDuration = duration;
|
|
print(totalDuration!.inSeconds);
|
|
});
|
|
});
|
|
});
|
|
// listeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
MediaService.resetAudioPlayer();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SizedBox(
|
|
height: 46,
|
|
child: Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
StreamBuilder<PlayerState>(
|
|
stream: MediaService.audioPlayer.playerStateStream,
|
|
builder: (context, snapshot) {
|
|
if (!snapshot.hasData) {
|
|
return const SizedBox();
|
|
}
|
|
// if (snapshot.data!.processingState ==
|
|
// ProcessingState.completed) {
|
|
// MediaService.audioPlayer.pause();
|
|
// MediaService.audioPlayer.seek(Duration.zero);
|
|
// }
|
|
print(snapshot.data);
|
|
return MessageBarBtn(
|
|
enable: true,
|
|
icon: snapshot.data!.playing &&
|
|
snapshot.data!.processingState !=
|
|
ProcessingState.completed &&
|
|
MediaService.audioPlayerTag == 'message-$id'
|
|
? DidvanIcons.pause_solid
|
|
: DidvanIcons.play_solid,
|
|
click: () async {
|
|
await MediaService.handleAudioPlayback(
|
|
audioSource: widget.file,
|
|
id: id,
|
|
isNetworkAudio: widget.file.startsWith('/uploads'),
|
|
isVoiceMessage: true,
|
|
isBlob: widget.file.startsWith('blob:'));
|
|
MediaService.audioPlayerTag = 'message-$id';
|
|
},
|
|
);
|
|
}),
|
|
StreamBuilder<Duration>(
|
|
stream: MediaService.audioPlayer.positionStream,
|
|
builder: (context, snapshot) {
|
|
if (!snapshot.hasData) {
|
|
return const SizedBox();
|
|
}
|
|
print("Position: ${snapshot.data}");
|
|
return Expanded(
|
|
child: totalDuration == null
|
|
? Padding(
|
|
padding: EdgeInsets.symmetric(
|
|
vertical: widget.loadingPaddingSize),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: List.generate(
|
|
5,
|
|
(index) => SpinKitWave(
|
|
color: Theme.of(context)
|
|
.colorScheme
|
|
.primary
|
|
.withOpacity(0.4),
|
|
size: 32,
|
|
itemCount: 10,
|
|
))),
|
|
)
|
|
: Row(
|
|
children: [
|
|
Expanded(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 12.0),
|
|
child: Expanded(
|
|
child: ProgressBar(
|
|
thumbColor:
|
|
Theme.of(context).colorScheme.title,
|
|
progressBarColor: DesignConfig.isDark
|
|
? Theme.of(context).colorScheme.title
|
|
: Theme.of(context)
|
|
.colorScheme
|
|
.primary,
|
|
baseBarColor:
|
|
Theme.of(context).colorScheme.border,
|
|
bufferedBarColor:
|
|
Theme.of(context).colorScheme.splash,
|
|
total: totalDuration!,
|
|
progress: MediaService.audioPlayerTag ==
|
|
'message-$id'
|
|
? snapshot.data ?? Duration.zero
|
|
: Duration.zero,
|
|
thumbRadius: 6,
|
|
barHeight: 3,
|
|
// timeLabelTextStyle: TextStyle(
|
|
// fontSize: showTimer ? null : 0,
|
|
// height: showTimer ? 3 : 0,
|
|
// color:
|
|
// Theme.of(context).colorScheme.text,
|
|
// fontFamily:
|
|
// DesignConfig.fontFamily.replaceAll(
|
|
// '-FA',
|
|
// '',
|
|
// ),
|
|
// ),
|
|
onSeek: (value) {
|
|
if (MediaService.audioPlayerTag ==
|
|
'message-$id') {
|
|
MediaService.audioPlayer.seek(
|
|
Duration(
|
|
milliseconds:
|
|
value.inMilliseconds));
|
|
}
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
DidvanText(DateTimeUtils.normalizeTimeDuration(
|
|
snapshot.data ?? Duration.zero)),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
)
|
|
],
|
|
),
|
|
));
|
|
}
|
|
|
|
Row noise({required final List<double> values, final Color? color}) {
|
|
return Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: values
|
|
.map(
|
|
(e) => Container(
|
|
margin: const EdgeInsets.symmetric(horizontal: 1),
|
|
width: 2,
|
|
height: e,
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(1000),
|
|
color: color ?? Theme.of(context).colorScheme.primary,
|
|
),
|
|
),
|
|
)
|
|
.toList(),
|
|
);
|
|
}
|
|
}
|