diff --git a/library/common/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java b/library/common/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java index ff4f262812..806bf11064 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/PlaybackParameters.java @@ -15,13 +15,18 @@ */ package com.google.android.exoplayer2; +import android.os.Bundle; import androidx.annotation.CheckResult; +import androidx.annotation.IntDef; import androidx.annotation.Nullable; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; /** Parameters that apply to playback, including speed setting. */ -public final class PlaybackParameters { +public final class PlaybackParameters implements Bundleable { /** The default playback parameters: real-time playback with no silence skipping. */ public static final PlaybackParameters DEFAULT = new PlaybackParameters(/* speed= */ 1f); @@ -106,4 +111,34 @@ public final class PlaybackParameters { public String toString() { return Util.formatInvariant("PlaybackParameters(speed=%.2f, pitch=%.2f)", speed, pitch); } + + // Bundleable implementation. + + @Documented + @Retention(RetentionPolicy.SOURCE) + @IntDef({FIELD_SPEED, FIELD_PITCH}) + private @interface FieldNumber {} + + private static final int FIELD_SPEED = 0; + private static final int FIELD_PITCH = 1; + + @Override + public Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putFloat(keyForField(FIELD_SPEED), speed); + bundle.putFloat(keyForField(FIELD_PITCH), pitch); + return bundle; + } + + /** Object that can restore {@link PlaybackParameters} from a {@link Bundle}. */ + public static final Creator CREATOR = + bundle -> { + float speed = bundle.getFloat(keyForField(FIELD_SPEED), /* defaultValue= */ 1f); + float pitch = bundle.getFloat(keyForField(FIELD_PITCH), /* defaultValue= */ 1f); + return new PlaybackParameters(speed, pitch); + }; + + private static String keyForField(@FieldNumber int field) { + return Integer.toString(field, Character.MAX_RADIX); + } } diff --git a/library/common/src/test/java/com/google/android/exoplayer2/PlaybackParametersTest.java b/library/common/src/test/java/com/google/android/exoplayer2/PlaybackParametersTest.java new file mode 100644 index 0000000000..8d255da0e7 --- /dev/null +++ b/library/common/src/test/java/com/google/android/exoplayer2/PlaybackParametersTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2; + +import static com.google.common.truth.Truth.assertThat; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Unit tests for {@link PlaybackParameters}. */ +@RunWith(AndroidJUnit4.class) +public class PlaybackParametersTest { + + @Test + public void roundtripViaBundle_ofPlaybackParameters_yieldsEqualInstance() { + PlaybackParameters playbackParameters = + new PlaybackParameters(/* speed= */ 2.9f, /* pitch= */ 1.2f); + + assertThat(PlaybackParameters.CREATOR.fromBundle(playbackParameters.toBundle())) + .isEqualTo(playbackParameters); + } +}