155 lines
4.6 KiB
Dart
155 lines
4.6 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/services.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
|
|
void main() => runApp(MyApp());
|
|
|
|
class MyApp extends StatefulWidget {
|
|
@override
|
|
_MyAppState createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
final AudioPlayer _player = AudioPlayer();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_player.setUrl(
|
|
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3");
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_player.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Audio Player Demo'),
|
|
),
|
|
body: Center(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text("Science Friday"),
|
|
Text("Science Friday and WNYC Studios"),
|
|
StreamBuilder<AudioPlaybackState>(
|
|
stream: _player.playbackStateStream,
|
|
builder: (context, snapshot) {
|
|
final state = snapshot.data;
|
|
return Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
if (state == AudioPlaybackState.playing)
|
|
IconButton(
|
|
icon: Icon(Icons.pause),
|
|
iconSize: 64.0,
|
|
onPressed: _player.pause,
|
|
)
|
|
else if (state == AudioPlaybackState.buffering ||
|
|
state == AudioPlaybackState.connecting)
|
|
Container(
|
|
margin: EdgeInsets.all(8.0),
|
|
width: 64.0,
|
|
height: 64.0,
|
|
child: CircularProgressIndicator(),
|
|
)
|
|
else
|
|
IconButton(
|
|
icon: Icon(Icons.play_arrow),
|
|
iconSize: 64.0,
|
|
onPressed: _player.play,
|
|
),
|
|
IconButton(
|
|
icon: Icon(Icons.stop),
|
|
iconSize: 64.0,
|
|
onPressed: state == AudioPlaybackState.stopped ||
|
|
state == AudioPlaybackState.none
|
|
? null
|
|
: _player.stop,
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
StreamBuilder<Duration>(
|
|
stream: _player.durationStream,
|
|
builder: (context, snapshot) {
|
|
final duration = snapshot.data ?? Duration.zero;
|
|
return StreamBuilder<Duration>(
|
|
stream: _player.getPositionStream(),
|
|
builder: (context, snapshot) {
|
|
final position = snapshot.data ?? Duration.zero;
|
|
return SeekBar(
|
|
duration: duration,
|
|
position: position,
|
|
onChangeEnd: (newPosition) {
|
|
_player.seek(newPosition);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SeekBar extends StatefulWidget {
|
|
final Duration duration;
|
|
final Duration position;
|
|
final ValueChanged<Duration> onChanged;
|
|
final ValueChanged<Duration> onChangeEnd;
|
|
|
|
SeekBar({
|
|
@required this.duration,
|
|
@required this.position,
|
|
this.onChanged,
|
|
this.onChangeEnd,
|
|
});
|
|
|
|
@override
|
|
_SeekBarState createState() => _SeekBarState();
|
|
}
|
|
|
|
class _SeekBarState extends State<SeekBar> {
|
|
double _dragValue;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Slider(
|
|
min: 0.0,
|
|
max: widget.duration.inMilliseconds.toDouble(),
|
|
value: _dragValue ?? widget.position.inMilliseconds.toDouble(),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_dragValue = value;
|
|
});
|
|
if (widget.onChanged != null) {
|
|
widget.onChanged(Duration(milliseconds: value.round()));
|
|
}
|
|
},
|
|
onChangeEnd: (value) {
|
|
_dragValue = null;
|
|
if (widget.onChangeEnd != null) {
|
|
widget.onChangeEnd(Duration(milliseconds: value.round()));
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|