Skip to content

Commit

Permalink
[audioplayers] Update audioplayers (flutter-tizen#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swanseo0 authored Jul 27, 2023
1 parent c23e61c commit f575fe4
Show file tree
Hide file tree
Showing 29 changed files with 1,497 additions and 493 deletions.
6 changes: 5 additions & 1 deletion packages/audioplayers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## NEXT
## 3.0.0

* Update audioplayers to 4.1.0.
* Update audioplayers_platform_interface to 5.0.1.
* Update example app.
* Update README.
* Increase the minimum Flutter version to 3.3.

## 2.0.0
Expand Down
8 changes: 4 additions & 4 deletions packages/audioplayers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ This package is not an _endorsed_ implementation of `audioplayers`. Therefore, y

```yaml
dependencies:
audioplayers: ^1.0.1
audioplayers_tizen: ^2.0.0
audioplayers: ^4.1.0
audioplayers_tizen: ^3.0.0

```

Expand Down Expand Up @@ -62,8 +62,8 @@ For detailed information on Tizen privileges, see [Tizen Docs: API Privileges](h
- [x] `AudioPlayer.getDuration`
- [x] `AudioPlayer.getCurrentPosition`
- [x] `AudioPlayer.dispose`
- [ ] `AudioPlayer.global.changeLogLevel` (not supported)
- [ ] `AudioPlayer.global.setGlobalAudioContext` (not supported)
- [ ] `AudioLogger.logLevel` (not supported)
- [ ] `AudioPlayer.global.setAudioContext` (not supported)

## Limitations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ void main() {
initialized.complete();
}
});
player.onSeekComplete.listen((event) => seek.complete());
player.onSeekComplete.listen((event) {
if (!seek.isCompleted) {
seek.complete();
}
});

await player.setSourceAsset(_kAssetAudio);
await initialized.future;
Expand Down
11 changes: 8 additions & 3 deletions packages/audioplayers/example/lib/components/btn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ class Btn extends StatelessWidget {
final String txt;
final VoidCallback onPressed;

const Btn({super.key, required this.txt, required this.onPressed});
const Btn({
required this.txt,
required this.onPressed,
super.key,
});

@override
Widget build(BuildContext context) {
return ButtonTheme(
minWidth: 48.0,
return Padding(
padding: const EdgeInsets.all(4),
child: ElevatedButton(
style: ElevatedButton.styleFrom(minimumSize: const Size(48, 36)),
onPressed: onPressed,
child: Text(txt),
),
Expand Down
13 changes: 6 additions & 7 deletions packages/audioplayers/example/lib/components/cbx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import 'package:flutter/material.dart';
class Cbx extends StatelessWidget {
final String label;
final bool value;
final void Function(bool) update;
final void Function({required bool? value}) update;

const Cbx(
this.label,
this.value,
this.update, {
required this.value,
super.key,
});

@override
Widget build(BuildContext context) {
return Row(
children: [
Text(label),
Checkbox(value: value, onChanged: (v) => update(v!)),
],
return CheckboxListTile(
title: Text(label),
value: value,
onChanged: (v) => update(value: v),
);
}
}
31 changes: 16 additions & 15 deletions packages/audioplayers/example/lib/components/dlg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@ import 'package:audioplayers_tizen_example/components/btn.dart';
import 'package:flutter/material.dart';

class SimpleDlg extends StatelessWidget {
final String message, action;
final String message;
final String action;

const SimpleDlg({
super.key,
required this.message,
required this.action,
super.key,
});

@override
Widget build(BuildContext context) {
return Dlg(
children: [
Text(message),
Btn(
txt: action,
onPressed: Navigator.of(context).pop,
),
],
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(message),
Btn(
txt: action,
onPressed: Navigator.of(context).pop,
),
],
),
);
}
}

class Dlg extends StatelessWidget {
final List<Widget> children;
final Widget child;

const Dlg({
required this.child,
super.key,
required this.children,
});

@override
Expand All @@ -48,9 +52,6 @@ class Dlg extends StatelessWidget {
}

Widget contentBox(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: children,
);
return child;
}
}
60 changes: 60 additions & 0 deletions packages/audioplayers/example/lib/components/drop_down.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:flutter/material.dart';

class LabeledDropDown<T> extends StatelessWidget {
final String label;
final Map<T, String> options;
final T selected;
final void Function(T?) onChange;

const LabeledDropDown({
required this.label,
required this.options,
required this.selected,
required this.onChange,
super.key,
});

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(label),
trailing: CustomDropDown<T>(
options: options,
selected: selected,
onChange: onChange,
),
);
}
}

class CustomDropDown<T> extends StatelessWidget {
final Map<T, String> options;
final T selected;
final void Function(T?) onChange;
final bool isExpanded;

const CustomDropDown({
required this.options,
required this.selected,
required this.onChange,
this.isExpanded = false,
super.key,
});

@override
Widget build(BuildContext context) {
return DropdownButton<T>(
isExpanded: isExpanded,
value: selected,
onChanged: onChange,
items: options.entries
.map<DropdownMenuItem<T>>(
(entry) => DropdownMenuItem<T>(
value: entry.key,
child: Text(entry.value),
),
)
.toList(),
);
}
}
26 changes: 26 additions & 0 deletions packages/audioplayers/example/lib/components/list_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';

class WrappedListTile extends StatelessWidget {
final List<Widget> children;
final Widget? leading;
final Widget? trailing;

const WrappedListTile({
required this.children,
this.leading,
this.trailing,
super.key,
});

@override
Widget build(BuildContext context) {
return ListTile(
title: Wrap(
alignment: WrapAlignment.end,
children: children,
),
leading: leading,
trailing: trailing,
);
}
}
3 changes: 2 additions & 1 deletion packages/audioplayers/example/lib/components/pad.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:flutter/material.dart';

class Pad extends StatelessWidget {
final double width, height;
final double width;
final double height;

const Pad({super.key, this.width = 0, this.height = 0});

Expand Down
43 changes: 33 additions & 10 deletions packages/audioplayers/example/lib/components/player_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class PlayerWidget extends StatefulWidget {
final AudioPlayer player;

const PlayerWidget({
super.key,
required this.player,
super.key,
});

@override
Expand All @@ -18,29 +18,52 @@ class PlayerWidget extends StatefulWidget {
}

class _PlayerWidgetState extends State<PlayerWidget> {
PlayerState? _audioPlayerState;
PlayerState? _playerState;
Duration? _duration;
Duration? _position;

PlayerState _playerState = PlayerState.stopped;
StreamSubscription? _durationSubscription;
StreamSubscription? _positionSubscription;
StreamSubscription? _playerCompleteSubscription;
StreamSubscription? _playerStateChangeSubscription;

bool get _isPlaying => _playerState == PlayerState.playing;

bool get _isPaused => _playerState == PlayerState.paused;

String get _durationText => _duration?.toString().split('.').first ?? '';

String get _positionText => _position?.toString().split('.').first ?? '';

AudioPlayer get player => widget.player;

@override
void initState() {
super.initState();
// Use initial values from player
_playerState = player.state;
player.getDuration().then(
(value) => setState(() {
_duration = value;
}),
);
player.getCurrentPosition().then(
(value) => setState(() {
_position = value;
}),
);
_initStreams();
}

@override
void setState(VoidCallback fn) {
// Subscriptions only can be closed asynchronously,
// therefore events can occur after widget has been disposed.
if (mounted) {
super.setState(fn);
}
}

@override
void dispose() {
_durationSubscription?.cancel();
Expand All @@ -52,6 +75,7 @@ class _PlayerWidgetState extends State<PlayerWidget> {

@override
Widget build(BuildContext context) {
final color = Theme.of(context).primaryColor;
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expand All @@ -63,21 +87,21 @@ class _PlayerWidgetState extends State<PlayerWidget> {
onPressed: _isPlaying ? null : _play,
iconSize: 48.0,
icon: const Icon(Icons.play_arrow),
color: Colors.cyan,
color: color,
),
IconButton(
key: const Key('pause_button'),
onPressed: _isPlaying ? _pause : null,
iconSize: 48.0,
icon: const Icon(Icons.pause),
color: Colors.cyan,
color: color,
),
IconButton(
key: const Key('stop_button'),
onPressed: _isPlaying || _isPaused ? _stop : null,
iconSize: 48.0,
icon: const Icon(Icons.stop),
color: Colors.cyan,
color: color,
),
],
),
Expand Down Expand Up @@ -105,7 +129,7 @@ class _PlayerWidgetState extends State<PlayerWidget> {
: '',
style: const TextStyle(fontSize: 16.0),
),
Text('State: $_audioPlayerState'),
Text('State: ${_playerState ?? '-'}'),
],
);
}
Expand All @@ -120,17 +144,16 @@ class _PlayerWidgetState extends State<PlayerWidget> {
);

_playerCompleteSubscription = player.onPlayerComplete.listen((event) {
player.stop();
setState(() {
_playerState = PlayerState.stopped;
_position = _duration;
_position = Duration.zero;
});
});

_playerStateChangeSubscription =
player.onPlayerStateChanged.listen((state) {
setState(() {
_audioPlayerState = state;
_playerState = state;
});
});
}
Expand Down
Loading

0 comments on commit f575fe4

Please sign in to comment.