Skip to content

Commit

Permalink
fix: Set playback rate only when playing (#1658)
Browse files Browse the repository at this point in the history
Fixes #468
  • Loading branch information
Gustl22 authored Oct 1, 2023
1 parent 9086e75 commit d73c7d5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
12 changes: 12 additions & 0 deletions packages/audioplayers/example/integration_test/platform_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ void main() async {
}
}

testWidgets('Avoid resume on setting playbackRate (#468)', (tester) async {
await tester.prepareSource(
playerId: playerId,
platform: platform,
testData: mp3Url1TestData,
);
await platform.setPlaybackRate(playerId, 2.0);
await tester.pumpAndSettle(const Duration(seconds: 2));
expect(await platform.getCurrentPosition(playerId), 0);
await tester.pumpLinux();
});

for (final td in audioTestDataList) {
if (features.hasSeek && !td.isLiveStream) {
testWidgets('#seek with millisecond precision ${td.source}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class MediaPlayerPlayer(
override fun setRate(rate: Float) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mediaPlayer.playbackParams = mediaPlayer.playbackParams.setSpeed(rate)
} else if (rate != 1.0f) {
} else if (rate == 1.0f) {
mediaPlayer.start()
} else {
error("Changing the playback rate is only available for Android M/23+ or using LOW_LATENCY mode.")
}
}
Expand All @@ -58,7 +60,8 @@ class MediaPlayerPlayer(
}

override fun start() {
mediaPlayer.start()
// Setting playback rate instead of mediaPlayer.start().
setRate(wrappedPlayer.rate)
}

override fun pause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class WrappedPlayer internal constructor(
set(value) {
if (field != value) {
field = value
player?.setRate(value)
if (playing) {
player?.setRate(value)
}
}
}

Expand Down Expand Up @@ -364,7 +366,6 @@ class WrappedPlayer internal constructor(
}

private fun Player.configAndPrepare() {
setRate(rate)
setVolumeAndBalance(volume, balance)
setLooping(isLooping)
prepare()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ class WrappedMediaPlayer {

func setPlaybackRate(playbackRate: Double) {
self.playbackRate = playbackRate
player.rate = Float(playbackRate)
if isPlaying {
// Setting the rate causes the player to resume playing. So setting it only, when already playing.
player.rate = Float(playbackRate)
}
}

func seek(time: CMTime, completer: Completer? = nil) {
Expand Down

0 comments on commit d73c7d5

Please sign in to comment.