Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:audioplayers/audio_cache.dart';
- import 'package:audioplayers/audioplayers.dart';
- typedef void OnError(Exception exception);
- class MusicPlay extends StatefulWidget {
- final music_play_name;
- final music_play_image;
- final music_play_background;
- final music_play_audio;
- MusicPlay({
- this.music_play_name,
- this.music_play_image,
- this.music_play_background,
- this.music_play_audio,
- });
- @override
- _MusicPlayState createState() => _MusicPlayState();
- }
- class _MusicPlayState extends State<MusicPlay> {
- AudioPlayer player;
- double _currentPosition = 0.0;
- double _totalDuration = 0.0;
- static AudioCache cache;
- bool _isPlaying = false;
- bool _repeat = false;
- @override
- void initState() {
- super.initState();
- cache = AudioCache(prefix: 'music/', fixedPlayer: AudioPlayer());
- }
- String localFilePath;
- Future<void> play() async {
- _isPlaying = true;
- if (player == null) {
- player = await cache.play('music.mp3')
- ..onAudioPositionChanged.listen((Duration p) {
- setState(() {
- _currentPosition = p.inMilliseconds.toDouble();
- });
- })
- ..onDurationChanged.listen((Duration d) {
- setState(() {
- _totalDuration = d.inMilliseconds.toDouble();
- });
- })
- ..onPlayerCompletion.listen((_) {
- setState(() {
- _currentPosition = 0;
- _isPlaying = false;
- });
- });
- } else {
- player.resume();
- }
- setState(() {});
- print("Hello I am music and I am playing Thanks to play me");
- }
- void pause() {
- _isPlaying = false;
- player.pause();
- setState(() {});
- print("shut up Why I am stop");
- }
- void seekToSecond(int second) {
- player.seek(Duration(seconds: second));
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- onWillPop: () async {
- await player.stop();
- return true;
- },
- child: Scaffold(
- appBar: AppBar(
- title: Text(widget.music_play_name),
- elevation: 0.0,
- ),
- body: Column(
- children: <Widget>[
- Flexible(
- flex: 9,
- child: Center(
- child: new Container(
- color: Colors.blue,
- child: Image.asset(
- widget.music_play_image,
- fit: BoxFit.cover,
- ),
- )),
- ),
- Flexible(
- flex: 3,
- child: Column(
- children: <Widget>[
- Slider(
- value: _currentPosition,
- min: 0.0,
- max: _totalDuration,
- onChanged: (v) {
- seekToSecond(v.toInt());
- _currentPosition = v;
- v = v;
- },
- ),
- Column(
- crossAxisAlignment: CrossAxisAlignment.end,
- children: <Widget>[
- Text("Current Duration ${_currentPosition.floor()}")
- ],
- ),
- Center(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- children: <Widget>[
- IconButton(
- icon: Icon(Icons.shuffle),
- onPressed: null,
- color: Colors.blue,
- ),
- IconButton(
- icon: _isPlaying
- ? Icon(Icons.pause_circle_filled)
- : Icon(Icons.play_circle_filled),
- onPressed: () {
- if (_isPlaying == true) {
- pause();
- } else {
- play();
- }
- },
- color: Colors.lightBlue,
- iconSize: 60.0,
- ),
- IconButton(
- icon: Icon(Icons.repeat),
- color: Colors.blue,
- onPressed: () {},
- )
- ],
- ),
- ),
- ],
- ),
- )
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement