Add tag and Factory to SilenceMediaSource

This commit is contained in:
Clement Jean 2020-04-16 22:28:48 +08:00
parent 7214ad2d6f
commit 3c655981d4

View file

@ -33,6 +33,46 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
/** Media source with a single period consisting of silent raw audio of a given duration. */
public final class SilenceMediaSource extends BaseMediaSource {
/** Factory for {@link SilenceMediaSource}s. */
public static final class Factory {
private long durationUs;
@Nullable private Object tag;
/**
* Sets the duration of the silent audio.
*
* @param durationUs The duration of silent audio to output, in microseconds.
* @return This factory, for convenience.
*/
public Factory setDuration(long durationUs) {
this.durationUs = durationUs;
return this;
}
/**
* Sets a tag for the media source which will be published in the {@link
* com.google.android.exoplayer2.Timeline} of the source as {@link
* com.google.android.exoplayer2.Timeline.Window#tag}.
*
* @param tag A tag for the media source.
* @return This factory, for convenience.
*/
public Factory setTag(@Nullable Object tag) {
this.tag = tag;
return this;
}
/**
* Returns a new {@link SilenceMediaSource}.
*
* @return The new {@link SilenceMediaSource}.
*/
public SilenceMediaSource createMediaSource() {
return new SilenceMediaSource(durationUs, tag);
}
}
private static final int SAMPLE_RATE_HZ = 44100;
@C.PcmEncoding private static final int PCM_ENCODING = C.ENCODING_PCM_16BIT;
private static final int CHANNEL_COUNT = 2;
@ -47,6 +87,7 @@ public final class SilenceMediaSource extends BaseMediaSource {
new byte[Util.getPcmFrameSize(PCM_ENCODING, CHANNEL_COUNT) * 1024];
private final long durationUs;
@Nullable private final Object tag;
/**
* Creates a new media source providing silent audio of the given duration.
@ -56,13 +97,20 @@ public final class SilenceMediaSource extends BaseMediaSource {
public SilenceMediaSource(long durationUs) {
Assertions.checkArgument(durationUs >= 0);
this.durationUs = durationUs;
this.tag = null;
}
private SilenceMediaSource(long durationUs, @Nullable Object tag) {
Assertions.checkArgument(durationUs >= 0);
this.durationUs = durationUs;
this.tag = tag;
}
@Override
protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) {
refreshSourceInfo(
new SinglePeriodTimeline(
durationUs, /* isSeekable= */ true, /* isDynamic= */ false, /* isLive= */ false));
durationUs, /* isSeekable= */ true, /* isDynamic= */ false, /* isLive= */ false, null, tag));
}
@Override