Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:io';
- import 'package:coupled/dal/storage.dart';
- import 'package:coupled/generated/l10n.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/animation.dart';
- import '../widgets/audioPlayerCard.dart';
- import 'package:flutter_sound/flutter_sound.dart';
- class AudioPlayerPage extends StatefulWidget {
- AudioPlayerPage(this.title, this.audioUrl, this.audioDuration,
- {Key? key,
- this.titleEnabled = false,
- required this.album,
- required this.imageId,
- required this.groupId})
- : super(key: key);
- final String title;
- final String audioUrl;
- final double audioDuration;
- final bool titleEnabled;
- final String album;
- final String imageId;
- final String groupId;
- @override
- AudioPlayerPageState createState() => new AudioPlayerPageState();
- }
- class AudioPlayerPageState extends State<AudioPlayerPage>
- with TickerProviderStateMixin {
- final String playerId = "1"; //Id doesn't matter, it's the only player
- AudioPlayerCardController? cardController;
- StreamSubscription? _audioPlayerDurationSubscription;
- late AppLocalization locale;
- bool _hasSetTitle = false;
- String _title = "";
- FlutterSoundPlayer player = FlutterSoundPlayer();
- @override
- void initState() {
- super.initState();
- player.openAudioSession().then((value) {
- setState(() {});
- });
- cardController = AudioPlayerCardController(playerId);
- _title = widget.title;
- }
- @override
- Widget build(BuildContext context) {
- locale = AppLocalization.of(context);
- List<Widget> actions = [];
- if (widget.titleEnabled) {
- actions = <Widget>[
- IconButton(
- tooltip: locale.audioToolTip,
- icon: Icon(Icons.text_fields),
- onPressed: onTitleTapped)
- ];
- }
- Widget audioCard = AudioPlayerCard(
- playerId,
- cardController!,
- widget.audioDuration,
- onPlayPressed,
- onPausedPressed,
- onSeek,
- onResumePressed);
- return WillPopScope(
- onWillPop: willPop,
- child: Scaffold(
- appBar: AppBar(
- title: Text(_title),
- actions: actions,
- ),
- body: Material(
- child: Center(child: audioCard),
- )),
- );
- }
- @override
- void dispose() {
- cardController!.dispose();
- if (_audioPlayerDurationSubscription != null) {
- _audioPlayerDurationSubscription!.cancel();
- _audioPlayerDurationSubscription = null;
- }
- if (player.isPlaying) {
- player.stopPlayer();
- }
- player.stopPlayer();
- player.closeAudioSession();
- super.dispose();
- }
- void onPlayPressed(String id) async {
- await player.startPlayer(
- fromURI: widget.audioUrl,
- whenFinished: () {
- setState(() {});
- });
- cardController!.stopLoading();
- cardController!.play();
- _audioPlayerDurationSubscription = player.onProgress?.listen((playStatus) {
- double progress = playStatus.duration.inMilliseconds.toDouble();
- progress = playStatus.position.inMilliseconds /
- playStatus.duration.inMilliseconds;
- cardController!
- .updateProgress(playStatus.position.inSeconds.roundToDouble());
- if (progress >= 100) {
- cardController!.stop();
- }
- });
- }
- void onPausedPressed(String id) async {
- await player.pausePlayer();
- }
- void onResumePressed(String id) async {
- await player.resumePlayer();
- }
- void onSeek(String id, double position) async {
- await player.seekToPlayer(Duration(milliseconds: position.floor()));
- }
- void onTitleTapped() async {
- await showDialog(
- context: context,
- barrierDismissible: true,
- builder: (BuildContext dialogContext) {
- String nameError = "";
- TextEditingController newFolderController =
- TextEditingController(text: _title);
- return StatefulBuilder(builder: (context, setDialogState) {
- return AlertDialog(
- content: TextField(
- textCapitalization: TextCapitalization.words,
- controller: newFolderController,
- decoration: InputDecoration(
- labelText: locale.audioLabelTitle,
- hasFloatingPlaceholder: true,
- errorText: nameError),
- ),
- actions: <Widget>[
- FlatButton(
- child: Text(locale.audioDialogCancel),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- FlatButton(
- child: Text(locale.audioDialogOk.toUpperCase()),
- onPressed: () async {
- String name = newFolderController.text;
- if (name.trim() == "") {
- setDialogState(() {
- nameError = locale.audioNameError;
- });
- } else {
- Navigator.of(context).pop();
- setState(() {
- _title = name;
- _hasSetTitle = true;
- });
- Storage storage = new Storage(widget.groupId);
- await storage.setAlbumContentTitle(
- widget.album, widget.imageId, name);
- }
- },
- ),
- ]);
- });
- });
- }
- Future<bool> willPop() async {
- if (_hasSetTitle) {
- Navigator.pop(context, {locale.audioPopScopeText.toLowerCase(): _title});
- } else {
- Navigator.pop(context);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement