Advertisement
Guest User

Untitled

a guest
Apr 8th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:io';
  3.  
  4. import 'package:coupled/dal/storage.dart';
  5. import 'package:coupled/generated/l10n.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/animation.dart';
  8. import '../widgets/audioPlayerCard.dart';
  9. import 'package:flutter_sound/flutter_sound.dart';
  10.  
  11. class AudioPlayerPage extends StatefulWidget {
  12. AudioPlayerPage(this.title, this.audioUrl, this.audioDuration,
  13. {Key? key,
  14. this.titleEnabled = false,
  15. required this.album,
  16. required this.imageId,
  17. required this.groupId})
  18. : super(key: key);
  19.  
  20. final String title;
  21. final String audioUrl;
  22. final double audioDuration;
  23.  
  24. final bool titleEnabled;
  25.  
  26. final String album;
  27. final String imageId;
  28. final String groupId;
  29.  
  30. @override
  31. AudioPlayerPageState createState() => new AudioPlayerPageState();
  32. }
  33.  
  34. class AudioPlayerPageState extends State<AudioPlayerPage>
  35. with TickerProviderStateMixin {
  36. final String playerId = "1"; //Id doesn't matter, it's the only player
  37. AudioPlayerCardController? cardController;
  38. StreamSubscription? _audioPlayerDurationSubscription;
  39. late AppLocalization locale;
  40. bool _hasSetTitle = false;
  41. String _title = "";
  42.  
  43. FlutterSoundPlayer player = FlutterSoundPlayer();
  44.  
  45. @override
  46. void initState() {
  47. super.initState();
  48. player.openAudioSession().then((value) {
  49. setState(() {});
  50. });
  51. cardController = AudioPlayerCardController(playerId);
  52. _title = widget.title;
  53. }
  54.  
  55. @override
  56. Widget build(BuildContext context) {
  57. locale = AppLocalization.of(context);
  58. List<Widget> actions = [];
  59. if (widget.titleEnabled) {
  60. actions = <Widget>[
  61. IconButton(
  62. tooltip: locale.audioToolTip,
  63. icon: Icon(Icons.text_fields),
  64. onPressed: onTitleTapped)
  65. ];
  66. }
  67.  
  68. Widget audioCard = AudioPlayerCard(
  69. playerId,
  70. cardController!,
  71. widget.audioDuration,
  72. onPlayPressed,
  73. onPausedPressed,
  74. onSeek,
  75. onResumePressed);
  76.  
  77. return WillPopScope(
  78. onWillPop: willPop,
  79. child: Scaffold(
  80. appBar: AppBar(
  81. title: Text(_title),
  82. actions: actions,
  83. ),
  84. body: Material(
  85. child: Center(child: audioCard),
  86. )),
  87. );
  88. }
  89.  
  90. @override
  91. void dispose() {
  92. cardController!.dispose();
  93. if (_audioPlayerDurationSubscription != null) {
  94. _audioPlayerDurationSubscription!.cancel();
  95. _audioPlayerDurationSubscription = null;
  96. }
  97. if (player.isPlaying) {
  98. player.stopPlayer();
  99. }
  100. player.stopPlayer();
  101. player.closeAudioSession();
  102.  
  103. super.dispose();
  104. }
  105.  
  106. void onPlayPressed(String id) async {
  107. await player.startPlayer(
  108. fromURI: widget.audioUrl,
  109. whenFinished: () {
  110. setState(() {});
  111. });
  112. cardController!.stopLoading();
  113. cardController!.play();
  114.  
  115. _audioPlayerDurationSubscription = player.onProgress?.listen((playStatus) {
  116. double progress = playStatus.duration.inMilliseconds.toDouble();
  117. progress = playStatus.position.inMilliseconds /
  118. playStatus.duration.inMilliseconds;
  119. cardController!
  120. .updateProgress(playStatus.position.inSeconds.roundToDouble());
  121. if (progress >= 100) {
  122. cardController!.stop();
  123. }
  124. });
  125. }
  126.  
  127. void onPausedPressed(String id) async {
  128. await player.pausePlayer();
  129. }
  130.  
  131. void onResumePressed(String id) async {
  132. await player.resumePlayer();
  133. }
  134.  
  135. void onSeek(String id, double position) async {
  136. await player.seekToPlayer(Duration(milliseconds: position.floor()));
  137. }
  138.  
  139. void onTitleTapped() async {
  140. await showDialog(
  141. context: context,
  142. barrierDismissible: true,
  143. builder: (BuildContext dialogContext) {
  144. String nameError = "";
  145. TextEditingController newFolderController =
  146. TextEditingController(text: _title);
  147. return StatefulBuilder(builder: (context, setDialogState) {
  148. return AlertDialog(
  149. content: TextField(
  150. textCapitalization: TextCapitalization.words,
  151. controller: newFolderController,
  152. decoration: InputDecoration(
  153. labelText: locale.audioLabelTitle,
  154. hasFloatingPlaceholder: true,
  155. errorText: nameError),
  156. ),
  157. actions: <Widget>[
  158. FlatButton(
  159. child: Text(locale.audioDialogCancel),
  160. onPressed: () {
  161. Navigator.of(context).pop();
  162. },
  163. ),
  164. FlatButton(
  165. child: Text(locale.audioDialogOk.toUpperCase()),
  166. onPressed: () async {
  167. String name = newFolderController.text;
  168. if (name.trim() == "") {
  169. setDialogState(() {
  170. nameError = locale.audioNameError;
  171. });
  172. } else {
  173. Navigator.of(context).pop();
  174. setState(() {
  175. _title = name;
  176. _hasSetTitle = true;
  177. });
  178. Storage storage = new Storage(widget.groupId);
  179. await storage.setAlbumContentTitle(
  180. widget.album, widget.imageId, name);
  181. }
  182. },
  183. ),
  184. ]);
  185. });
  186. });
  187. }
  188.  
  189. Future<bool> willPop() async {
  190. if (_hasSetTitle) {
  191. Navigator.pop(context, {locale.audioPopScopeText.toLowerCase(): _title});
  192. } else {
  193. Navigator.pop(context);
  194. }
  195. return false;
  196. }
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement