Advertisement
theaayushbhattarai

Untitled

Nov 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.60 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:audioplayers/audio_cache.dart';
  3. import 'package:audioplayers/audioplayers.dart';
  4.  
  5. typedef void OnError(Exception exception);
  6.  
  7. class MusicPlay extends StatefulWidget {
  8.   final music_play_name;
  9.   final music_play_image;
  10.   final music_play_background;
  11.   final music_play_audio;
  12.   MusicPlay({
  13.     this.music_play_name,
  14.     this.music_play_image,
  15.     this.music_play_background,
  16.     this.music_play_audio,
  17.   });
  18.   @override
  19.   _MusicPlayState createState() => _MusicPlayState();
  20. }
  21.  
  22. class _MusicPlayState extends State<MusicPlay> {
  23.   AudioPlayer player;
  24.   double _currentPosition = 0.0;
  25.   double _totalDuration = 0.0;
  26.   static AudioCache cache;
  27.   bool _isPlaying = false;
  28.   bool _repeat = false;
  29.  
  30.   @override
  31.   void initState() {
  32.     super.initState();
  33.     cache = AudioCache(prefix: 'music/', fixedPlayer: AudioPlayer());
  34.   }
  35.  
  36.   String localFilePath;
  37.  
  38.   Future<void> play() async {
  39.     _isPlaying = true;
  40.     if (player == null) {
  41.       player = await cache.play('music.mp3')
  42.         ..onAudioPositionChanged.listen((Duration p) {
  43.           setState(() {
  44.             _currentPosition = p.inMilliseconds.toDouble();
  45.           });
  46.         })
  47.         ..onDurationChanged.listen((Duration d) {
  48.           setState(() {
  49.             _totalDuration = d.inMilliseconds.toDouble();
  50.           });
  51.         })
  52.         ..onPlayerCompletion.listen((_) {
  53.           setState(() {
  54.             _currentPosition = 0;
  55.             _isPlaying = false;
  56.           });
  57.         });
  58.     } else {
  59.       player.resume();
  60.     }
  61.     setState(() {});
  62.     print("Hello I am music and I am playing Thanks to play me");
  63.   }
  64.  
  65.   void pause() {
  66.     _isPlaying = false;
  67.     player.pause();
  68.     setState(() {});
  69.     print("shut up Why I am stop");
  70.   }
  71.  
  72.   void seekToSecond(int second) {
  73.     player.seek(Duration(seconds: second));
  74.   }
  75.  
  76.   @override
  77.   Widget build(BuildContext context) {
  78.     return WillPopScope(
  79.       onWillPop: () async {
  80.         await player.stop();
  81.         return true;
  82.       },
  83.       child: Scaffold(
  84.         appBar: AppBar(
  85.           title: Text(widget.music_play_name),
  86.           elevation: 0.0,
  87.         ),
  88.         body: Column(
  89.           children: <Widget>[
  90.             Flexible(
  91.               flex: 9,
  92.               child: Center(
  93.                   child: new Container(
  94.                 color: Colors.blue,
  95.                 child: Image.asset(
  96.                   widget.music_play_image,
  97.                   fit: BoxFit.cover,
  98.                 ),
  99.               )),
  100.             ),
  101.             Flexible(
  102.               flex: 3,
  103.               child: Column(
  104.                 children: <Widget>[
  105.                   Slider(
  106.                     value: _currentPosition,
  107.                     min: 0.0,
  108.                     max: _totalDuration,
  109.                     onChanged: (v) {
  110.                       seekToSecond(v.toInt());
  111.                       _currentPosition = v;
  112.                       v = v;
  113.                     },
  114.                   ),
  115.                   Column(
  116.                     crossAxisAlignment: CrossAxisAlignment.end,
  117.                     children: <Widget>[
  118.                       Text("Current Duration ${_currentPosition.floor()}")
  119.                     ],
  120.                   ),
  121.                   Center(
  122.                     child: Row(
  123.                       mainAxisAlignment: MainAxisAlignment.spaceAround,
  124.                       children: <Widget>[
  125.                         IconButton(
  126.                           icon: Icon(Icons.shuffle),
  127.                           onPressed: null,
  128.                           color: Colors.blue,
  129.                         ),
  130.                         IconButton(
  131.                           icon: _isPlaying
  132.                               ? Icon(Icons.pause_circle_filled)
  133.                               : Icon(Icons.play_circle_filled),
  134.                           onPressed: () {
  135.                             if (_isPlaying == true) {
  136.                               pause();
  137.                             } else {
  138.                               play();
  139.                             }
  140.                           },
  141.                           color: Colors.lightBlue,
  142.                           iconSize: 60.0,
  143.                         ),
  144.                         IconButton(
  145.                           icon: Icon(Icons.repeat),
  146.                           color: Colors.blue,
  147.                           onPressed: () {},
  148.                         )
  149.                       ],
  150.                     ),
  151.                   ),
  152.                 ],
  153.               ),
  154.             )
  155.           ],
  156.         ),
  157.       ),
  158.     );
  159.   }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement