Add seekToNext/Previous, hasNext/Previous, polish example.

This commit is contained in:
Ryan Heise 2020-08-07 02:41:55 +10:00
parent a494dabcfb
commit 372b4665a5
5 changed files with 184 additions and 74 deletions

View File

@ -62,6 +62,8 @@ await player.load(
], ],
), ),
); );
player.seekToNext();
player.seekToPrevious();
// Jump to the beginning of track3.mp3. // Jump to the beginning of track3.mp3.
player.seek(Duration(milliseconds: 0), index: 2); player.seek(Duration(milliseconds: 0), index: 2);
``` ```

View File

@ -1,8 +1,8 @@
import 'dart:math'; import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:just_audio/just_audio.dart'; import 'package:just_audio/just_audio.dart';
import 'package:rxdart/rxdart.dart';
void main() => runApp(MyApp()); void main() => runApp(MyApp());
@ -12,8 +12,6 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
final _volumeSubject = BehaviorSubject.seeded(1.0);
final _speedSubject = BehaviorSubject.seeded(1.0);
AudioPlayer _player; AudioPlayer _player;
ConcatenatingAudioSource _playlist = ConcatenatingAudioSource(children: [ ConcatenatingAudioSource _playlist = ConcatenatingAudioSource(children: [
LoopingAudioSource( LoopingAudioSource(
@ -26,6 +24,8 @@ class _MyAppState extends State<MyApp> {
tag: AudioMetadata( tag: AudioMetadata(
album: "Science Friday", album: "Science Friday",
title: "A Salute To Head-Scratching Science (5 seconds)", title: "A Salute To Head-Scratching Science (5 seconds)",
artwork:
"https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
), ),
), ),
), ),
@ -35,6 +35,8 @@ class _MyAppState extends State<MyApp> {
tag: AudioMetadata( tag: AudioMetadata(
album: "Science Friday", album: "Science Friday",
title: "A Salute To Head-Scratching Science", title: "A Salute To Head-Scratching Science",
artwork:
"https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
), ),
), ),
AudioSource.uri( AudioSource.uri(
@ -42,6 +44,8 @@ class _MyAppState extends State<MyApp> {
tag: AudioMetadata( tag: AudioMetadata(
album: "Science Friday", album: "Science Friday",
title: "From Cat Rheology To Operatic Incompetence", title: "From Cat Rheology To Operatic Incompetence",
artwork:
"https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
), ),
), ),
]); ]);
@ -56,6 +60,9 @@ class _MyAppState extends State<MyApp> {
super.initState(); super.initState();
AudioPlayer.setIosCategory(IosCategory.playback); AudioPlayer.setIosCategory(IosCategory.playback);
_player = AudioPlayer(); _player = AudioPlayer();
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.black,
));
_loadAudio(); _loadAudio();
} }
@ -77,29 +84,36 @@ class _MyAppState extends State<MyApp> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold( home: Scaffold(
appBar: AppBar( body: SafeArea(
title: const Text('Audio Player Demo'),
),
body: Center(
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
StreamBuilder<int>( Expanded(
stream: _player.currentIndexStream, child: StreamBuilder<int>(
builder: (context, snapshot) { stream: _player.currentIndexStream,
final index = snapshot.data ?? 0; builder: (context, snapshot) {
final metadata = _metadataSequence[index]; final index = snapshot.data ?? 0;
return Column( final metadata = _metadataSequence[index];
crossAxisAlignment: CrossAxisAlignment.center, return Column(
children: [ crossAxisAlignment: CrossAxisAlignment.center,
Text(metadata.album ?? '', children: [
style: Theme.of(context).textTheme.headline6), Expanded(
Text(metadata.title ?? ''), child: Padding(
], padding: const EdgeInsets.all(8.0),
); child:
}, Center(child: Image.network(metadata.artwork)),
),
),
Text(metadata.album ?? '',
style: Theme.of(context).textTheme.headline6),
Text(metadata.title ?? ''),
],
);
},
),
), ),
StreamBuilder<PlayerState>( StreamBuilder<PlayerState>(
stream: _player.playerStateStream, stream: _player.playerStateStream,
@ -110,6 +124,25 @@ class _MyAppState extends State<MyApp> {
return Row( return Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
IconButton(
icon: Icon(Icons.volume_up),
onPressed: () {
_showSliderDialog(
context: context,
title: "Adjust volume",
divisions: 10,
min: 0.0,
max: 1.0,
stream: _player.volumeStream,
onChanged: _player.setVolume,
);
},
),
IconButton(
icon: Icon(Icons.skip_previous),
onPressed:
_player.hasPrevious ? _player.seekToPrevious : null,
),
if (processingState == ProcessingState.loading || if (processingState == ProcessingState.loading ||
processingState == ProcessingState.buffering) processingState == ProcessingState.buffering)
Container( Container(
@ -137,11 +170,32 @@ class _MyAppState extends State<MyApp> {
onPressed: () => onPressed: () =>
_player.seek(Duration.zero, index: 0), _player.seek(Duration.zero, index: 0),
), ),
IconButton(
icon: Icon(Icons.skip_next),
onPressed: _player.hasNext ? _player.seekToNext : null,
),
IconButton(
icon: StreamBuilder<double>(
stream: _player.speedStream,
builder: (context, snapshot) => Text(
"${snapshot.data?.toStringAsFixed(1)}x",
style: TextStyle(fontWeight: FontWeight.bold))),
onPressed: () {
_showSliderDialog(
context: context,
title: "Adjust speed",
divisions: 10,
min: 0.5,
max: 1.5,
stream: _player.speedStream,
onChanged: _player.setSpeed,
);
},
),
], ],
); );
}, },
), ),
Text("Track position"),
StreamBuilder<Duration>( StreamBuilder<Duration>(
stream: _player.durationStream, stream: _player.durationStream,
builder: (context, snapshot) { builder: (context, snapshot) {
@ -164,34 +218,7 @@ class _MyAppState extends State<MyApp> {
); );
}, },
), ),
Text("Volume"), SizedBox(height: 8.0),
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);
},
),
),
Row( Row(
children: [ children: [
StreamBuilder<LoopMode>( StreamBuilder<LoopMode>(
@ -242,7 +269,8 @@ class _MyAppState extends State<MyApp> {
), ),
], ],
), ),
Expanded( Container(
height: 240.0,
child: StreamBuilder<int>( child: StreamBuilder<int>(
stream: _player.currentIndexStream, stream: _player.currentIndexStream,
builder: (context, snapshot) { builder: (context, snapshot) {
@ -293,32 +321,89 @@ class _SeekBarState extends State<SeekBar> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Slider( return Stack(
min: 0.0, children: [
max: widget.duration.inMilliseconds.toDouble(), Slider(
value: min(_dragValue ?? widget.position.inMilliseconds.toDouble(), min: 0.0,
widget.duration.inMilliseconds.toDouble()), max: widget.duration.inMilliseconds.toDouble(),
onChanged: (value) { value: min(_dragValue ?? widget.position.inMilliseconds.toDouble(),
setState(() { widget.duration.inMilliseconds.toDouble()),
_dragValue = value; onChanged: (value) {
}); setState(() {
if (widget.onChanged != null) { _dragValue = value;
widget.onChanged(Duration(milliseconds: value.round())); });
} if (widget.onChanged != null) {
}, widget.onChanged(Duration(milliseconds: value.round()));
onChangeEnd: (value) { }
if (widget.onChangeEnd != null) { },
widget.onChangeEnd(Duration(milliseconds: value.round())); onChangeEnd: (value) {
} if (widget.onChangeEnd != null) {
_dragValue = null; widget.onChangeEnd(Duration(milliseconds: value.round()));
}, }
_dragValue = null;
},
),
Positioned(
right: 16.0,
bottom: 0.0,
child: Text(
RegExp(r'((^0*[1-9]\d*:)?\d{2}:\d{2})\.\d+$')
.firstMatch("$_remaining")
?.group(1) ??
'$_remaining',
style: Theme.of(context).textTheme.caption),
),
],
); );
} }
Duration get _remaining => widget.duration - widget.position;
}
_showSliderDialog({
BuildContext context,
String title,
int divisions,
double min,
double max,
String valueSuffix = '',
Stream<double> stream,
ValueChanged<double> onChanged,
}) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(title, textAlign: TextAlign.center),
content: StreamBuilder<double>(
stream: stream,
builder: (context, snapshot) => Container(
height: 100.0,
child: Column(
children: [
Text('${snapshot.data?.toStringAsFixed(1)}$valueSuffix',
style: TextStyle(
fontFamily: 'Fixed',
fontWeight: FontWeight.bold,
fontSize: 24.0)),
Slider(
divisions: divisions,
min: min,
max: max,
value: snapshot.data ?? 1.0,
onChanged: onChanged,
),
],
),
),
),
),
);
} }
class AudioMetadata { class AudioMetadata {
final String album; final String album;
final String title; final String title;
final String artwork;
AudioMetadata({this.album, this.title}); AudioMetadata({this.album, this.title, this.artwork});
} }

View File

@ -178,7 +178,7 @@ packages:
source: hosted source: hosted
version: "3.0.13" version: "3.0.13"
rxdart: rxdart:
dependency: "direct main" dependency: transitive
description: description:
name: rxdart name: rxdart
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"

View File

@ -7,7 +7,6 @@ dependencies:
sdk: flutter sdk: flutter
cupertino_icons: ^0.1.2 cupertino_icons: ^0.1.2
rxdart: ^0.24.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

View File

@ -222,6 +222,16 @@ class AudioPlayer {
/// A stream broadcasting the current item. /// A stream broadcasting the current item.
Stream<int> get currentIndexStream => _currentIndexSubject.stream; Stream<int> get currentIndexStream => _currentIndexSubject.stream;
/// Whether there is another item after the current index.
bool get hasNext =>
_audioSource != null &&
currentIndex != null &&
currentIndex + 1 < _audioSource.sequence.length;
/// Whether there is another item before the current index.
bool get hasPrevious =>
_audioSource != null && currentIndex != null && currentIndex > 0;
/// The current loop mode. /// The current loop mode.
LoopMode get loopMode => _loopModeSubject.value; LoopMode get loopMode => _loopModeSubject.value;
@ -268,7 +278,7 @@ class AudioPlayer {
_positionSubject.addStream(createPositionStream( _positionSubject.addStream(createPositionStream(
steps: 800, steps: 800,
minPeriod: Duration(milliseconds: 16), minPeriod: Duration(milliseconds: 16),
maxPeriod: Duration(milliseconds: 11200))); maxPeriod: Duration(milliseconds: 200)));
} }
return _positionSubject.stream; return _positionSubject.stream;
} }
@ -515,6 +525,20 @@ class AudioPlayer {
} }
} }
/// Seek to the next item.
Future<void> seekToNext() async {
if (hasNext) {
await seek(Duration.zero, index: currentIndex + 1);
}
}
/// Seek to the previous item.
Future<void> seekToPrevious() async {
if (hasPrevious) {
await seek(Duration.zero, index: currentIndex - 1);
}
}
/// Release all resources associated with this player. You must invoke this /// Release all resources associated with this player. You must invoke this
/// after you are done with the player. /// after you are done with the player.
Future<void> dispose() async { Future<void> dispose() async {