Implement Bundleable for PlaybackParameters
PiperOrigin-RevId: 360810865
This commit is contained in:
parent
01cb6ee3a3
commit
7fad836b3a
@ -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<PlaybackParameters> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user