2020-07-27 17:54:00 +00:00
|
|
|
import 'dart:math';
|
2019-11-25 14:50:21 +00:00
|
|
|
|
2020-07-27 17:54:00 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2019-11-28 06:55:32 +00:00
|
|
|
import 'package:just_audio/just_audio.dart';
|
2019-12-25 13:44:08 +00:00
|
|
|
import 'package:rxdart/rxdart.dart';
|
2019-11-25 14:50:21 +00:00
|
|
|
|
|
|
|
void main() => runApp(MyApp());
|
|
|
|
|
|
|
|
class MyApp extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_MyAppState createState() => _MyAppState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyAppState extends State<MyApp> {
|
2019-12-25 13:44:08 +00:00
|
|
|
final _volumeSubject = BehaviorSubject.seeded(1.0);
|
|
|
|
final _speedSubject = BehaviorSubject.seeded(1.0);
|
|
|
|
AudioPlayer _player;
|
2020-07-09 03:27:53 +00:00
|
|
|
ConcatenatingAudioSource _playlist = ConcatenatingAudioSource(audioSources: [
|
|
|
|
LoopingAudioSource(
|
|
|
|
count: 2,
|
|
|
|
audioSource: ClippingAudioSource(
|
|
|
|
start: Duration(seconds: 60),
|
|
|
|
end: Duration(seconds: 65),
|
|
|
|
audioSource: AudioSource.uri(Uri.parse(
|
|
|
|
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3")),
|
|
|
|
tag: AudioMetadata(
|
|
|
|
album: "Science Friday",
|
|
|
|
title: "A Salute To Head-Scratching Science (5 seconds)",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-07-27 17:54:00 +00:00
|
|
|
//LoopingAudioSource(
|
|
|
|
// count: 2,
|
|
|
|
// audioSource: AudioSource.uri(
|
|
|
|
// Uri.parse(
|
|
|
|
// "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
|
|
|
|
// tag: AudioMetadata(
|
|
|
|
// album: "Science Friday",
|
|
|
|
// title: "A Salute To Head-Scratching Science (full)",
|
|
|
|
// ),
|
|
|
|
// ),
|
|
|
|
//),
|
|
|
|
//ClippingAudioSource(
|
|
|
|
// start: Duration(seconds: 60),
|
|
|
|
// end: Duration(seconds: 65),
|
|
|
|
// audioSource: AudioSource.uri(Uri.parse(
|
|
|
|
// "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3")),
|
|
|
|
// tag: AudioMetadata(
|
|
|
|
// album: "Science Friday",
|
|
|
|
// title: "A Salute To Head-Scratching Science (5 seconds)",
|
|
|
|
// ),
|
|
|
|
//),
|
2020-07-09 03:27:53 +00:00
|
|
|
AudioSource.uri(
|
|
|
|
Uri.parse(
|
|
|
|
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
|
|
|
|
tag: AudioMetadata(
|
|
|
|
album: "Science Friday",
|
|
|
|
title: "A Salute To Head-Scratching Science (full)",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
AudioSource.uri(
|
|
|
|
Uri.parse("https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3"),
|
|
|
|
tag: AudioMetadata(
|
|
|
|
album: "Science Friday",
|
|
|
|
title: "From Cat Rheology To Operatic Incompetence",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
List<IndexedAudioSource> get _sequence => _playlist.sequence;
|
|
|
|
|
|
|
|
List<AudioMetadata> get _metadataSequence =>
|
|
|
|
_sequence.map((s) => s.tag as AudioMetadata).toList();
|
2019-11-25 14:50:21 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2020-05-20 14:38:26 +00:00
|
|
|
AudioPlayer.setIosCategory(IosCategory.playback);
|
2019-12-25 13:44:08 +00:00
|
|
|
_player = AudioPlayer();
|
2020-07-09 03:27:53 +00:00
|
|
|
_loadAudio();
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadAudio() async {
|
|
|
|
try {
|
|
|
|
await _player.load(_playlist);
|
|
|
|
} catch (e) {
|
2020-04-20 04:19:43 +00:00
|
|
|
// catch audio error ex: 404 url, wrong url ...
|
2020-07-09 03:27:53 +00:00
|
|
|
print("$e");
|
|
|
|
}
|
2019-11-25 14:50:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 05:16:54 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_player.dispose();
|
|
|
|
super.dispose();
|
2019-11-25 14:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
2019-11-28 05:16:54 +00:00
|
|
|
title: const Text('Audio Player Demo'),
|
2019-11-25 14:50:21 +00:00
|
|
|
),
|
|
|
|
body: Center(
|
2019-11-28 05:16:54 +00:00
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2020-07-09 03:27:53 +00:00
|
|
|
StreamBuilder<int>(
|
|
|
|
stream: _player.currentIndexStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final index = snapshot.data ?? 0;
|
|
|
|
final metadata = _metadataSequence[index];
|
|
|
|
return Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(metadata.album ?? '',
|
|
|
|
style: Theme.of(context).textTheme.headline6),
|
|
|
|
Text(metadata.title ?? ''),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2020-02-05 01:26:21 +00:00
|
|
|
StreamBuilder<FullAudioPlaybackState>(
|
|
|
|
stream: _player.fullPlaybackStateStream,
|
2019-11-28 05:16:54 +00:00
|
|
|
builder: (context, snapshot) {
|
2020-02-05 01:26:21 +00:00
|
|
|
final fullState = snapshot.data;
|
|
|
|
final state = fullState?.state;
|
|
|
|
final buffering = fullState?.buffering;
|
2019-11-28 05:16:54 +00:00
|
|
|
return Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2020-02-05 01:26:21 +00:00
|
|
|
if (state == AudioPlaybackState.connecting ||
|
|
|
|
buffering == true)
|
2019-11-28 05:16:54 +00:00
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.all(8.0),
|
|
|
|
width: 64.0,
|
|
|
|
height: 64.0,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
)
|
2020-02-05 01:26:21 +00:00
|
|
|
else if (state == AudioPlaybackState.playing)
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.pause),
|
|
|
|
iconSize: 64.0,
|
|
|
|
onPressed: _player.pause,
|
|
|
|
)
|
2019-11-28 05:16:54 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2019-12-25 13:44:08 +00:00
|
|
|
Text("Track position"),
|
2019-11-28 05:16:54 +00:00
|
|
|
StreamBuilder<Duration>(
|
|
|
|
stream: _player.durationStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final duration = snapshot.data ?? Duration.zero;
|
|
|
|
return StreamBuilder<Duration>(
|
|
|
|
stream: _player.getPositionStream(),
|
|
|
|
builder: (context, snapshot) {
|
2019-12-26 15:45:11 +00:00
|
|
|
var position = snapshot.data ?? Duration.zero;
|
|
|
|
if (position > duration) {
|
|
|
|
position = duration;
|
|
|
|
}
|
2019-11-28 05:16:54 +00:00
|
|
|
return SeekBar(
|
|
|
|
duration: duration,
|
|
|
|
position: position,
|
|
|
|
onChangeEnd: (newPosition) {
|
|
|
|
_player.seek(newPosition);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2019-12-25 13:44:08 +00:00
|
|
|
Text("Volume"),
|
|
|
|
StreamBuilder<double>(
|
|
|
|
stream: _volumeSubject.stream,
|
|
|
|
builder: (context, snapshot) => Slider(
|
|
|
|
divisions: 20,
|
|
|
|
min: 0.0,
|
|
|
|
max: 2.0,
|
|
|
|
value: snapshot.data ?? 1.0,
|
|
|
|
onChanged: (value) {
|
|
|
|
_volumeSubject.add(value);
|
|
|
|
_player.setVolume(value);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Text("Speed"),
|
|
|
|
StreamBuilder<double>(
|
|
|
|
stream: _speedSubject.stream,
|
|
|
|
builder: (context, snapshot) => Slider(
|
|
|
|
divisions: 10,
|
|
|
|
min: 0.5,
|
|
|
|
max: 1.5,
|
|
|
|
value: snapshot.data ?? 1.0,
|
|
|
|
onChanged: (value) {
|
|
|
|
_speedSubject.add(value);
|
|
|
|
_player.setSpeed(value);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2020-07-09 03:27:53 +00:00
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
StreamBuilder<LoopMode>(
|
|
|
|
stream: _player.loopModeStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final loopMode = snapshot.data ?? LoopMode.off;
|
|
|
|
const icons = [
|
|
|
|
Icon(Icons.repeat, color: Colors.grey),
|
|
|
|
Icon(Icons.repeat, color: Colors.orange),
|
|
|
|
Icon(Icons.repeat_one, color: Colors.orange),
|
|
|
|
];
|
|
|
|
const cycleModes = [
|
|
|
|
LoopMode.off,
|
|
|
|
LoopMode.all,
|
|
|
|
LoopMode.one,
|
|
|
|
];
|
|
|
|
final index = cycleModes.indexOf(loopMode);
|
|
|
|
return IconButton(
|
|
|
|
icon: icons[index],
|
|
|
|
onPressed: () {
|
|
|
|
_player.setLoopMode(cycleModes[
|
|
|
|
(cycleModes.indexOf(loopMode) + 1) %
|
|
|
|
cycleModes.length]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
"Playlist",
|
|
|
|
style: Theme.of(context).textTheme.headline6,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
StreamBuilder<bool>(
|
|
|
|
stream: _player.shuffleModeEnabledStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final shuffleModeEnabled = snapshot.data ?? false;
|
|
|
|
return IconButton(
|
|
|
|
icon: shuffleModeEnabled
|
|
|
|
? Icon(Icons.shuffle, color: Colors.orange)
|
|
|
|
: Icon(Icons.shuffle, color: Colors.grey),
|
|
|
|
onPressed: () {
|
|
|
|
_player.setShuffleModeEnabled(!shuffleModeEnabled);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: StreamBuilder<int>(
|
|
|
|
stream: _player.currentIndexStream,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
final currentIndex = snapshot.data ?? 0;
|
|
|
|
return ListView.builder(
|
|
|
|
itemCount: _metadataSequence.length,
|
|
|
|
itemBuilder: (context, index) => Material(
|
|
|
|
color:
|
|
|
|
index == currentIndex ? Colors.grey.shade300 : null,
|
|
|
|
child: ListTile(
|
|
|
|
title: Text(_metadataSequence[index].title),
|
|
|
|
onTap: () {
|
|
|
|
_player.seek(Duration.zero, index: index);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2019-11-28 05:16:54 +00:00
|
|
|
],
|
|
|
|
),
|
2019-11-25 14:50:21 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 05:16:54 +00:00
|
|
|
|
|
|
|
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(),
|
2020-07-27 17:54:00 +00:00
|
|
|
value: min(_dragValue ?? widget.position.inMilliseconds.toDouble(),
|
|
|
|
widget.duration.inMilliseconds.toDouble()),
|
2019-11-28 05:16:54 +00:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 03:27:53 +00:00
|
|
|
|
|
|
|
class AudioMetadata {
|
|
|
|
final String album;
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
AudioMetadata({this.album, this.title});
|
|
|
|
}
|