-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_music.dart
170 lines (158 loc) · 4.74 KB
/
play_music.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class PlayMusic extends StatefulWidget {
const PlayMusic({Key? key, required String title}) : super(key: key);
@override
State<PlayMusic> createState() => _PlayMusicState();
}
class _PlayMusicState extends State<PlayMusic> {
late int sec = 10;
late AudioPlayer player;
late AudioCache cache;
bool isPlay = false;
Duration currentPostion = Duration();
Duration musicLength = Duration();
int index=0;
late Color full=Colors.indigo;
List<String> mylist= ['a.mp3','b.mp3','c.mp3', 'd.mp3', 'e.mp3'];
@override
void initState() {
super.initState();
player = AudioPlayer();
cache = AudioCache(fixedPlayer: player);
cache.load("ayub.m4a");
index=0;
setUp();
}
setUp() {
player.onAudioPositionChanged.listen((c) {
setState(() {
currentPostion = c;
});
player.onDurationChanged.listen((l) {
musicLength = l;
});
});
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: Column(
children: [
Container(
alignment: Alignment.center,
height: size.height / 2,
width: double.infinity,
decoration: const BoxDecoration(color: Colors.indigo),
child: const Text(
"تشغيل الموسيقى",
style: TextStyle(fontSize: 22, color: Colors.white),
),
),
Container(
padding: const EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("${currentPostion.inSeconds}"),
Container(
margin: const EdgeInsets.all(2),
width: size.width * .7,
child: Slider(
value: currentPostion.inSeconds.toDouble(),
max: musicLength.inSeconds.toDouble(),
onChanged: (val) {
seekTo(val.toInt());
})),
Text("${musicLength.inSeconds}",style: TextStyle(color: full),),
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: size.width / 7,
),
IconButton(
onPressed: () {
if (currentPostion.inSeconds == 0 ||
currentPostion.inSeconds < sec) {
seekTo(0);
// isPlay = false;
}
else if (currentPostion.inSeconds > 10)
{
seekTo(currentPostion.inSeconds - sec);
}
},
icon: const Icon(FontAwesomeIcons.backward),
iconSize: 20,
color: Colors.indigo,
),
SizedBox(
width: size.width / 7,
),
IconButton(
onPressed: () {
if (isPlay) {
setState(() {
isPlay = false;
stopMusic();
});
} else {
setState(() {
isPlay = true;
playMusic();
});
//playMusic();
}
},
icon: isPlay
? const Icon(FontAwesomeIcons.pause)
: const Icon(FontAwesomeIcons.play),
iconSize: 20,
color: Colors.indigo,
),
SizedBox(
width: size.width / 7,
),
IconButton(
onPressed: () {
if (currentPostion < musicLength - Duration(seconds: 10)) {
seekTo(currentPostion.inSeconds + sec);
}
else {
seekTo(musicLength.inSeconds);
setState(() {
isPlay=false;
});
player.stop();
}
},
icon: const Icon(FontAwesomeIcons.forward),
iconSize: 20,
color: Colors.indigo,
),
SizedBox(
width: size.width / 7,
),
],
),
],
));
}
void stopMusic() {
player.pause();
}
void playMusic() {
cache.play("ayub.m4a");
}
void seekTo(int i) {
player.seek(
Duration(seconds: i),
);
}
}