-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathplayer.dart
153 lines (137 loc) · 3.63 KB
/
player.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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:provider/provider.dart';
import 'package:dashcast/notifiers.dart';
class PlayerPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
Provider.of<Podcast>(context).selectedItem.title,
),
),
body: SafeArea(child: Player()),
);
}
}
class Player extends StatelessWidget {
@override
Widget build(BuildContext context) {
final podcast = Provider.of<Podcast>(context);
return Column(
children: [
Flexible(
flex: 8,
child: SingleChildScrollView(
child: Column(children: [
Image.network(podcast.feed.image.url),
Padding(
padding: const EdgeInsets.all(10),
child: Text(
podcast.selectedItem.description.trim(),
),
),
]),
)),
Flexible(
flex: 2,
child: Material(
elevation: 12,
child: AudioControls(),
),
),
],
);
}
}
class AudioControls extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
PlaybackButtons(),
],
);
}
}
class PlaybackButtons extends StatefulWidget {
@override
_PlaybackButtonState createState() => _PlaybackButtonState();
}
class _PlaybackButtonState extends State<PlaybackButtons> {
bool _isPlaying = false;
FlutterSound _sound;
double _playPosition;
StreamSubscription<PlayStatus> _playerSubscription;
@override
void initState() {
super.initState();
_sound = FlutterSound();
_playPosition = 0;
}
@override
void dispose() {
_cleanup();
super.dispose();
}
void _cleanup() async {
if (_sound.audioState == t_AUDIO_STATE.IS_PLAYING)
await _sound.stopPlayer();
// TODO somehow this gets called from episode list and breaks everything.
_playerSubscription.cancel();
}
void _stop() async {
await _sound.stopPlayer();
setState(() => _isPlaying = false);
}
void _play(String url) async {
await _sound.startPlayer(url);
_playerSubscription = _sound.onPlayerStateChanged.listen((e) {
if (e != null) {
// print(e.currentPosition);
setState(() => _playPosition = (e.currentPosition / e.duration));
}
});
setState(() => _isPlaying = true);
}
void _fastForward() {}
void _rewind() {}
@override
Widget build(BuildContext context) {
final podcast = Provider.of<Podcast>(context);
final item = podcast.selectedItem;
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Slider(
value: _playPosition,
onChanged: null,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(icon: Icon(Icons.fast_rewind), onPressed: null),
IconButton(
icon: _isPlaying ? Icon(Icons.stop) : Icon(Icons.play_arrow),
onPressed: () {
if (_isPlaying) {
_stop();
} else {
var url = item.downloadLocation ?? item.guid;
print('Playing url: $url');
_play(url);
}
},
),
IconButton(
icon: Icon(Icons.fast_forward),
onPressed: null,
),
],
),
],
);
}
}