Playists, looping, shuffling for Android and web

This commit is contained in:
Ryan Heise 2020-07-09 13:27:53 +10:00
parent 6f776eee87
commit c0c5d0c2bf
8 changed files with 1795 additions and 379 deletions

View file

@ -14,19 +14,57 @@ class _MyAppState extends State<MyApp> {
final _volumeSubject = BehaviorSubject.seeded(1.0);
final _speedSubject = BehaviorSubject.seeded(1.0);
AudioPlayer _player;
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)",
),
),
),
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();
@override
void initState() {
super.initState();
AudioPlayer.setIosCategory(IosCategory.playback);
_player = AudioPlayer();
_player
.setUrl(
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3")
.catchError((error) {
_loadAudio();
}
_loadAudio() async {
try {
await _player.load(_playlist);
} catch (e) {
// catch audio error ex: 404 url, wrong url ...
print(error);
});
print("$e");
}
}
@override
@ -47,8 +85,21 @@ class _MyAppState extends State<MyApp> {
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Science Friday"),
Text("Science Friday and WNYC Studios"),
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 ?? ''),
],
);
},
),
StreamBuilder<FullAudioPlaybackState>(
stream: _player.fullPlaybackStateStream,
builder: (context, snapshot) {
@ -141,6 +192,77 @@ class _MyAppState extends State<MyApp> {
},
),
),
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);
},
),
),
);
},
),
),
],
),
),
@ -192,3 +314,10 @@ class _SeekBarState extends State<SeekBar> {
);
}
}
class AudioMetadata {
final String album;
final String title;
AudioMetadata({this.album, this.title});
}

View file

@ -1,20 +1,6 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.13"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.0"
async:
dependency: transitive
description:
@ -36,6 +22,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
collection:
dependency: transitive
description:
@ -64,6 +57,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
file:
dependency: transitive
description:
@ -86,13 +86,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.12"
intl:
dependency: transitive
description:
@ -106,7 +99,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.2.1"
version: "0.2.2"
matcher:
dependency: transitive
description:
@ -127,7 +120,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
version: "1.7.0"
path_provider:
dependency: transitive
description:
@ -156,13 +149,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
platform:
dependency: transitive
description:
@ -184,13 +170,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.13"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
rxdart:
dependency: "direct main"
description:
@ -244,7 +223,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.15"
version: "0.2.16"
typed_data:
dependency: transitive
description:
@ -252,6 +231,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
uuid:
dependency: transitive
description:
name: uuid
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
vector_math:
dependency: transitive
description:
@ -266,13 +252,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.6.1"
sdks:
dart: ">=2.6.0 <3.0.0"
dart: ">=2.7.0 <3.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"