Skip to content

Commit

Permalink
[audioplayers] Update audioplayers to 6.1.0 (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSUYA authored Nov 11, 2024
1 parent f0b4723 commit 6f1d108
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 125 deletions.
7 changes: 7 additions & 0 deletions packages/audioplayers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.1.0

* Update audioplayers to 6.1.0.
* Update audioplayers_platform_interface to 7.0.0.
* Changed to create a player when AudioPlayer created.
* Remove 'audio.onCurrentPosition' method event.

## 3.0.2

* Update minimum Flutter and Dart version to 3.13 and 3.1.
Expand Down
7 changes: 0 additions & 7 deletions packages/audioplayers/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
include: package:flame_lint/analysis_options.yaml

# The 'invariant_booleans' is a deprecated lint rule.
# Remove the following lines when flame_lint package removes the rule
# from its `analysis_options.yaml`.
linter:
rules:
- invariant_booleans: false
5 changes: 5 additions & 0 deletions packages/audioplayers/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include: package:flame_lint/analysis_options.yaml

linter:
rules:
do_not_use_environment: false
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void main() {
await player.play(AssetSource(_kAssetAudio));
await started.future;
await Future<void>.delayed(_kPlayDuration);
expect(count, greaterThanOrEqualTo(5));
expect(count, greaterThanOrEqualTo(2));

await player.dispose();
});
Expand Down
13 changes: 2 additions & 11 deletions packages/audioplayers/example/lib/components/dlg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,10 @@ class Dlg extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
elevation: 0,
backgroundColor: Colors.white,
child: Container(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: contentBox(context),
child: child,
),
);
}

Widget contentBox(BuildContext context) {
return child;
}
}
10 changes: 3 additions & 7 deletions packages/audioplayers/example/lib/components/player_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';

// This code is also used in the example.md. Please keep it up to date.
class PlayerWidget extends StatefulWidget {
final AudioPlayer player;

Expand Down Expand Up @@ -106,12 +107,12 @@ class _PlayerWidgetState extends State<PlayerWidget> {
],
),
Slider(
onChanged: (v) {
onChanged: (value) {
final duration = _duration;
if (duration == null) {
return;
}
final position = v * duration.inMilliseconds;
final position = value * duration.inMilliseconds;
player.seek(Duration(milliseconds: position.round()));
},
value: (_position != null &&
Expand All @@ -129,7 +130,6 @@ class _PlayerWidgetState extends State<PlayerWidget> {
: '',
style: const TextStyle(fontSize: 16.0),
),
Text('State: ${_playerState ?? '-'}'),
],
);
}
Expand Down Expand Up @@ -159,10 +159,6 @@ class _PlayerWidgetState extends State<PlayerWidget> {
}

Future<void> _play() async {
final position = _position;
if (position != null && position.inMilliseconds > 0) {
await player.seek(position);
}
await player.resume();
setState(() => _playerState = PlayerState.playing);
}
Expand Down
1 change: 1 addition & 0 deletions packages/audioplayers/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const defaultPlayerCount = 4;

typedef OnError = void Function(Exception exception);

/// The app is deployed at: https://bluefireteam.github.io/audioplayers/
void main() {
runApp(const MaterialApp(home: _ExampleApp()));
}
Expand Down
62 changes: 38 additions & 24 deletions packages/audioplayers/example/lib/tabs/audio_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:audioplayers_tizen_example/components/cbx.dart';
import 'package:audioplayers_tizen_example/components/drop_down.dart';
import 'package:audioplayers_tizen_example/components/tab_content.dart';
import 'package:audioplayers_tizen_example/components/tabs.dart';
import 'package:audioplayers_tizen_example/utils.dart';
import 'package:flutter/material.dart';

class AudioContextTab extends StatefulWidget {
Expand All @@ -27,7 +28,7 @@ class AudioContextTabState extends State<AudioContextTab>
AudioContextConfig audioContextConfig = AudioContextConfig();

/// Set config for each platform individually
AudioContext audioContext = const AudioContext();
AudioContext audioContext = AudioContext();

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -82,10 +83,15 @@ class AudioContextTabState extends State<AudioContextTab>
}

void updateConfig(AudioContextConfig newConfig) {
setState(() {
audioContextConfig = newConfig;
audioContext = audioContextConfig.build();
});
try {
final context = newConfig.build();
setState(() {
audioContextConfig = newConfig;
audioContext = context;
});
} on AssertionError catch (e) {
toast(e.message.toString());
}
}

void updateAudioContextAndroid(AudioContextAndroid contextAndroid) {
Expand All @@ -94,10 +100,15 @@ class AudioContextTabState extends State<AudioContextTab>
});
}

void updateAudioContextIOS(AudioContextIOS contextIOS) {
setState(() {
audioContext = audioContext.copy(iOS: contextIOS);
});
void updateAudioContextIOS(AudioContextIOS Function() buildContextIOS) {
try {
final context = buildContextIOS();
setState(() {
audioContext = audioContext.copy(iOS: context);
});
} on AssertionError catch (e) {
toast(e.message.toString());
}
}

Widget _genericTab() {
Expand All @@ -112,11 +123,13 @@ class AudioContextTabState extends State<AudioContextTab>
audioContextConfig.copy(route: v),
),
),
Cbx(
'Duck Audio',
value: audioContextConfig.duckAudio,
({value}) => updateConfig(
audioContextConfig.copy(duckAudio: value),
LabeledDropDown<AudioContextConfigFocus>(
label: 'Audio Focus',
key: const Key('audioFocus'),
options: {for (final e in AudioContextConfigFocus.values) e: e.name},
selected: audioContextConfig.focus,
onChange: (v) => updateConfig(
audioContextConfig.copy(focus: v),
),
),
Cbx(
Expand Down Expand Up @@ -194,19 +207,20 @@ class AudioContextTabState extends State<AudioContextTab>
Widget _iosTab() {
final iosOptions = AVAudioSessionOptions.values.map(
(option) {
final options = audioContext.iOS.options;
final options = {...audioContext.iOS.options};
return Cbx(
option.name,
value: options.contains(option),
({value}) {
if (value ?? false) {
options.add(option);
} else {
options.remove(option);
}
updateAudioContextIOS(
audioContext.iOS.copy(options: options),
);
updateAudioContextIOS(() {
final iosContext = audioContext.iOS.copy(options: options);
if (value ?? false) {
options.add(option);
} else {
options.remove(option);
}
return iosContext;
});
},
);
},
Expand All @@ -219,7 +233,7 @@ class AudioContextTabState extends State<AudioContextTab>
options: {for (final e in AVAudioSessionCategory.values) e: e.name},
selected: audioContext.iOS.category,
onChange: (v) => updateAudioContextIOS(
audioContext.iOS.copy(category: v),
() => audioContext.iOS.copy(category: v),
),
),
...iosOptions,
Expand Down
13 changes: 6 additions & 7 deletions packages/audioplayers/example/lib/tabs/controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class _ControlsTabState extends State<ControlsTab>
txt: 'Custom',
onPressed: () async {
dialog(
SeekDialog(
_SeekDialog(
value: modalInputSeek,
setValue: (it) => setState(() => modalInputSeek = it),
seekDuration: () => _seekDuration(
Expand All @@ -184,18 +184,17 @@ class _ControlsTabState extends State<ControlsTab>
bool get wantKeepAlive => true;
}

class SeekDialog extends StatelessWidget {
class _SeekDialog extends StatelessWidget {
final VoidCallback seekDuration;
final VoidCallback seekPercent;
final void Function(String val) setValue;
final String value;

const SeekDialog({
const _SeekDialog({
required this.seekDuration,
required this.seekPercent,
required this.value,
required this.setValue,
super.key,
});

@override
Expand All @@ -209,7 +208,7 @@ class SeekDialog extends StatelessWidget {
onChange: setValue,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Btn(
txt: 'millis',
Expand All @@ -232,9 +231,9 @@ class SeekDialog extends StatelessWidget {
seekPercent();
},
),
Btn(
txt: 'Cancel',
TextButton(
onPressed: Navigator.of(context).pop,
child: const Text('Cancel'),
),
],
),
Expand Down
311 changes: 273 additions & 38 deletions packages/audioplayers/example/lib/tabs/sources.dart

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/audioplayers/example/lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension StateExt<T extends StatefulWidget> on State<T> {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message, key: textKey),
duration: const Duration(milliseconds: 250),
duration: Duration(milliseconds: message.length * 25),
),
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/audioplayers/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ environment:
flutter: ">=3.13.0"

dependencies:
audioplayers: ^5.1.0
audioplayers: ^6.1.0
audioplayers_tizen:
path: ../
collection: ^1.16.0
file_picker: ^5.0.1
flutter:
sdk: flutter
http: ^0.13.6
path_provider: ^2.0.1
http: ">=0.13.1 <2.0.0"
path_provider: ^2.0.12
path_provider_tizen:
path: ../../path_provider/
provider: ^6.0.5
Expand Down
2 changes: 1 addition & 1 deletion packages/audioplayers/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ flutter:
fileName: audioplayers_tizen_plugin.h

dependencies:
audioplayers_platform_interface: ^6.0.0
audioplayers_platform_interface: ^7.0.0
flutter:
sdk: flutter

Expand Down
25 changes: 16 additions & 9 deletions packages/audioplayers/tizen/src/audio_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

AudioPlayer::AudioPlayer(const std::string &player_id,
PreparedListener prepared_listener,
CurrentPositionListener current_position_listener,
DurationListener duration_listener,
SeekCompletedListener seek_completed_listener,
PlayCompletedListener play_completed_listener,
LogListener log_listener)
: player_id_(player_id),
prepared_listener_(prepared_listener),
current_position_listener_(current_position_listener),
duration_listener_(duration_listener),
seek_completed_listener_(seek_completed_listener),
play_completed_listener_(play_completed_listener),
log_listener_(log_listener) {}
log_listener_(log_listener) {
CreatePlayer();
}

AudioPlayer::~AudioPlayer() { Release(); }

Expand All @@ -32,10 +32,6 @@ void AudioPlayer::Play() {
return;
}

if (state == PLAYER_STATE_NONE) {
CreatePlayer();
}

switch (state) {
case PLAYER_STATE_NONE:
case PLAYER_STATE_IDLE: {
Expand Down Expand Up @@ -113,6 +109,7 @@ void AudioPlayer::Release() {
ecore_timer_del(timer_);
timer_ = nullptr;
}
released_ = true;
}

void AudioPlayer::Seek(int32_t position) {
Expand Down Expand Up @@ -224,6 +221,16 @@ int AudioPlayer::GetDuration() {
}

int AudioPlayer::GetCurrentPosition() {
// TODO(jsuya) : When stop() or pause() is called in AudioPlayer 6.1.0,
// PositionUpdater's stopAndUpdate() is called. At this time, getPosition() is
// called, but in ReleaseMode, the player is released after Stop(), so an
// eception is thrown. Since there are differences from the implementation in
// the frontend package, an exception is not thrown in this case.
if (!player_ && released_ && release_mode_ == ReleaseMode::kRelease) {
LOG_ERROR("The player has already been released.");
return 0;
}

int32_t position;
int ret = player_get_play_position(player_, &position);
if (ret != PLAYER_ERROR_NONE) {
Expand Down Expand Up @@ -263,6 +270,8 @@ void AudioPlayer::CreatePlayer() {
throw AudioPlayerError("player_set_error_cb failed",
get_error_message(ret));
}

released_ = false;
}

void AudioPlayer::PreparePlayer() {
Expand Down Expand Up @@ -434,8 +443,6 @@ Eina_Bool AudioPlayer::OnPositionUpdate(void *data) {
if (player->IsPlaying()) {
int32_t duration = player->GetDuration();
player->duration_listener_(player->player_id_, duration);
int32_t position = player->GetCurrentPosition();
player->current_position_listener_(player->player_id_, position);

return ECORE_CALLBACK_RENEW;
}
Expand Down
Loading

0 comments on commit 6f1d108

Please sign in to comment.