com.google.android.exoplayer2.util.Util.constrainValue()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(275)

本文整理了Java中com.google.android.exoplayer2.util.Util.constrainValue()方法的一些代码示例,展示了Util.constrainValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util.constrainValue()方法的具体详情如下:
包路径:com.google.android.exoplayer2.util.Util
类名称:Util
方法名:constrainValue

Util.constrainValue介绍

[英]Constrains a value to the specified bounds.
[中]将值约束到指定的边界。

代码示例

代码示例来源:origin: google/ExoPlayer

  1. /** Returns whether it's possible to apply the specified edit using gapless playback info. */
  2. private static boolean canApplyEditWithGaplessInfo(
  3. long[] timestamps, long duration, long editStartTime, long editEndTime) {
  4. int lastIndex = timestamps.length - 1;
  5. int latestDelayIndex = Util.constrainValue(MAX_GAPLESS_TRIM_SIZE_SAMPLES, 0, lastIndex);
  6. int earliestPaddingIndex =
  7. Util.constrainValue(timestamps.length - MAX_GAPLESS_TRIM_SIZE_SAMPLES, 0, lastIndex);
  8. return timestamps[0] <= editStartTime
  9. && editStartTime < timestamps[latestDelayIndex]
  10. && timestamps[earliestPaddingIndex] < editEndTime
  11. && editEndTime <= duration;
  12. }

代码示例来源:origin: google/ExoPlayer

  1. /**
  2. * Returns the sample index for the sample at given position.
  3. *
  4. * @param timeUs Time position in microseconds in the FLAC stream.
  5. * @return The sample index for the sample at given position.
  6. */
  7. public long getSampleIndex(long timeUs) {
  8. long sampleIndex = (timeUs * sampleRate) / C.MICROS_PER_SECOND;
  9. return Util.constrainValue(sampleIndex, 0, totalSamples - 1);
  10. }

代码示例来源:origin: google/ExoPlayer

  1. private long getFramePositionForTimeUs(long timeUs) {
  2. long positionOffset = (timeUs * bitrate) / (C.MICROS_PER_SECOND * C.BITS_PER_BYTE);
  3. // Constrain to nearest preceding frame offset.
  4. positionOffset = (positionOffset / frameSize) * frameSize;
  5. positionOffset =
  6. Util.constrainValue(positionOffset, /* min= */ 0, /* max= */ dataSize - frameSize);
  7. return firstFrameBytePosition + positionOffset;
  8. }
  9. }

代码示例来源:origin: google/ExoPlayer

  1. private void positionScrubber(float xPosition) {
  2. scrubberBar.right = Util.constrainValue((int) xPosition, progressBar.left, progressBar.right);
  3. }

代码示例来源:origin: google/ExoPlayer

  1. /**
  2. * Ensures {@code peekBuffer} is large enough to store at least {@code length} bytes from the
  3. * current peek position.
  4. */
  5. private void ensureSpaceForPeek(int length) {
  6. int requiredLength = peekBufferPosition + length;
  7. if (requiredLength > peekBuffer.length) {
  8. int newPeekCapacity = Util.constrainValue(peekBuffer.length * 2,
  9. requiredLength + PEEK_MIN_FREE_SPACE_AFTER_RESIZE, requiredLength + PEEK_MAX_FREE_SPACE);
  10. peekBuffer = Arrays.copyOf(peekBuffer, newPeekCapacity);
  11. }
  12. }

代码示例来源:origin: google/ExoPlayer

  1. private SeekParameters clipSeekParameters(long positionUs, SeekParameters seekParameters) {
  2. long toleranceBeforeUs =
  3. Util.constrainValue(
  4. seekParameters.toleranceBeforeUs, /* min= */ 0, /* max= */ positionUs - startUs);
  5. long toleranceAfterUs =
  6. Util.constrainValue(
  7. seekParameters.toleranceAfterUs,
  8. /* min= */ 0,
  9. /* max= */ endUs == C.TIME_END_OF_SOURCE ? Long.MAX_VALUE : endUs - positionUs);
  10. if (toleranceBeforeUs == seekParameters.toleranceBeforeUs
  11. && toleranceAfterUs == seekParameters.toleranceAfterUs) {
  12. return seekParameters;
  13. } else {
  14. return new SeekParameters(toleranceBeforeUs, toleranceAfterUs);
  15. }
  16. }

代码示例来源:origin: google/ExoPlayer

  1. /**
  2. * Sets the playback speed. Calling this method will discard any data buffered within the
  3. * processor, and may update the value returned by {@link #isActive()}.
  4. *
  5. * @param speed The requested new playback speed.
  6. * @return The actual new playback speed.
  7. */
  8. public float setSpeed(float speed) {
  9. speed = Util.constrainValue(speed, MINIMUM_SPEED, MAXIMUM_SPEED);
  10. if (this.speed != speed) {
  11. this.speed = speed;
  12. sonic = null;
  13. }
  14. flush();
  15. return speed;
  16. }

代码示例来源:origin: google/ExoPlayer

  1. /**
  2. * Sets the playback pitch. Calling this method will discard any data buffered within the
  3. * processor, and may update the value returned by {@link #isActive()}.
  4. *
  5. * @param pitch The requested new pitch.
  6. * @return The actual new pitch.
  7. */
  8. public float setPitch(float pitch) {
  9. pitch = Util.constrainValue(pitch, MINIMUM_PITCH, MAXIMUM_PITCH);
  10. if (this.pitch != pitch) {
  11. this.pitch = pitch;
  12. sonic = null;
  13. }
  14. flush();
  15. return pitch;
  16. }

代码示例来源:origin: google/ExoPlayer

  1. | (initializationBytes[11] & 0xFF);
  2. defaultVerticalPlacement = (float) requestedVerticalPlacement / calculatedVideoTrackHeight;
  3. defaultVerticalPlacement = Util.constrainValue(defaultVerticalPlacement, 0.0f, 0.95f);
  4. } else {
  5. defaultVerticalPlacement = DEFAULT_VERTICAL_PLACEMENT;

代码示例来源:origin: google/ExoPlayer

  1. private long getSegmentNum(
  2. RepresentationHolder representationHolder,
  3. @Nullable MediaChunk previousChunk,
  4. long loadPositionUs,
  5. long firstAvailableSegmentNum,
  6. long lastAvailableSegmentNum) {
  7. return previousChunk != null
  8. ? previousChunk.getNextChunkIndex()
  9. : Util.constrainValue(
  10. representationHolder.getSegmentNum(loadPositionUs),
  11. firstAvailableSegmentNum,
  12. lastAvailableSegmentNum);
  13. }

代码示例来源:origin: google/ExoPlayer

  1. @Override
  2. public final int getBufferedPercentage() {
  3. long position = getBufferedPosition();
  4. long duration = getDuration();
  5. return position == C.TIME_UNSET || duration == C.TIME_UNSET
  6. ? 0
  7. : duration == 0 ? 100 : Util.constrainValue((int) ((position * 100) / duration), 0, 100);
  8. }

代码示例来源:origin: google/ExoPlayer

  1. private void drawPlayhead(Canvas canvas) {
  2. if (duration <= 0) {
  3. return;
  4. }
  5. int playheadX = Util.constrainValue(scrubberBar.right, scrubberBar.left, progressBar.right);
  6. int playheadY = scrubberBar.centerY();
  7. if (scrubberDrawable == null) {
  8. int scrubberSize = (scrubbing || isFocused()) ? scrubberDraggedSize
  9. : (isEnabled() ? scrubberEnabledSize : scrubberDisabledSize);
  10. int playheadRadius = scrubberSize / 2;
  11. canvas.drawCircle(playheadX, playheadY, playheadRadius, scrubberPaint);
  12. } else {
  13. int scrubberDrawableWidth = scrubberDrawable.getIntrinsicWidth();
  14. int scrubberDrawableHeight = scrubberDrawable.getIntrinsicHeight();
  15. scrubberDrawable.setBounds(
  16. playheadX - scrubberDrawableWidth / 2,
  17. playheadY - scrubberDrawableHeight / 2,
  18. playheadX + scrubberDrawableWidth / 2,
  19. playheadY + scrubberDrawableHeight / 2);
  20. scrubberDrawable.draw(canvas);
  21. }
  22. }

代码示例来源:origin: google/ExoPlayer

  1. private void parsePaletteSection(ParsableByteArray buffer, int sectionLength) {
  2. if ((sectionLength % 5) != 2) {
  3. // Section must be two bytes followed by a whole number of (index, y, cb, cr, a) entries.
  4. return;
  5. }
  6. buffer.skipBytes(2);
  7. Arrays.fill(colors, 0);
  8. int entryCount = sectionLength / 5;
  9. for (int i = 0; i < entryCount; i++) {
  10. int index = buffer.readUnsignedByte();
  11. int y = buffer.readUnsignedByte();
  12. int cr = buffer.readUnsignedByte();
  13. int cb = buffer.readUnsignedByte();
  14. int a = buffer.readUnsignedByte();
  15. int r = (int) (y + (1.40200 * (cr - 128)));
  16. int g = (int) (y - (0.34414 * (cb - 128)) - (0.71414 * (cr - 128)));
  17. int b = (int) (y + (1.77200 * (cb - 128)));
  18. colors[index] =
  19. (a << 24)
  20. | (Util.constrainValue(r, 0, 255) << 16)
  21. | (Util.constrainValue(g, 0, 255) << 8)
  22. | Util.constrainValue(b, 0, 255);
  23. }
  24. colorsSet = true;
  25. }

代码示例来源:origin: google/ExoPlayer

  1. @Override
  2. public void setVolume(float audioVolume) {
  3. verifyApplicationThread();
  4. audioVolume = Util.constrainValue(audioVolume, /* min= */ 0, /* max= */ 1);
  5. if (this.audioVolume == audioVolume) {
  6. return;
  7. }
  8. this.audioVolume = audioVolume;
  9. sendVolumeToRenderers();
  10. for (AudioListener audioListener : audioListeners) {
  11. audioListener.onVolumeChanged(audioVolume);
  12. }
  13. }

代码示例来源:origin: google/ExoPlayer

  1. private int getDefaultBufferSize() {
  2. if (isInputPcm) {
  3. int minBufferSize =
  4. AudioTrack.getMinBufferSize(outputSampleRate, outputChannelConfig, outputEncoding);
  5. Assertions.checkState(minBufferSize != ERROR_BAD_VALUE);
  6. int multipliedBufferSize = minBufferSize * BUFFER_MULTIPLICATION_FACTOR;
  7. int minAppBufferSize = (int) durationUsToFrames(MIN_BUFFER_DURATION_US) * outputPcmFrameSize;
  8. int maxAppBufferSize = (int) Math.max(minBufferSize,
  9. durationUsToFrames(MAX_BUFFER_DURATION_US) * outputPcmFrameSize);
  10. return Util.constrainValue(multipliedBufferSize, minAppBufferSize, maxAppBufferSize);
  11. } else {
  12. int rate = getMaximumEncodedRateBytesPerSecond(outputEncoding);
  13. if (outputEncoding == C.ENCODING_AC3) {
  14. rate *= AC3_BUFFER_MULTIPLICATION_FACTOR;
  15. }
  16. return (int) (PASSTHROUGH_BUFFER_DURATION_US * rate / C.MICROS_PER_SECOND);
  17. }
  18. }

代码示例来源:origin: TeamNewPipe/NewPipe

  1. private void publishFloatingQueueWindow() {
  2. if (callback.getQueueSize() == 0) {
  3. mediaSession.setQueue(Collections.emptyList());
  4. activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID;
  5. return;
  6. }
  7. // Yes this is almost a copypasta, got a problem with that? =\
  8. int windowCount = callback.getQueueSize();
  9. int currentWindowIndex = callback.getCurrentPlayingIndex();
  10. int queueSize = Math.min(maxQueueSize, windowCount);
  11. int startIndex = Util.constrainValue(currentWindowIndex - ((queueSize - 1) / 2), 0,
  12. windowCount - queueSize);
  13. List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();
  14. for (int i = startIndex; i < startIndex + queueSize; i++) {
  15. queue.add(new MediaSessionCompat.QueueItem(callback.getQueueMetadata(i), i));
  16. }
  17. mediaSession.setQueue(queue);
  18. activeQueueItemId = currentWindowIndex;
  19. }

代码示例来源:origin: google/ExoPlayer

  1. private void publishFloatingQueueWindow(Player player) {
  2. if (player.getCurrentTimeline().isEmpty()) {
  3. mediaSession.setQueue(Collections.emptyList());
  4. activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID;
  5. return;
  6. }
  7. int windowCount = player.getCurrentTimeline().getWindowCount();
  8. int currentWindowIndex = player.getCurrentWindowIndex();
  9. int queueSize = Math.min(maxQueueSize, windowCount);
  10. int startIndex = Util.constrainValue(currentWindowIndex - ((queueSize - 1) / 2), 0,
  11. windowCount - queueSize);
  12. List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();
  13. for (int i = startIndex; i < startIndex + queueSize; i++) {
  14. queue.add(new MediaSessionCompat.QueueItem(getMediaDescription(player, i), i));
  15. }
  16. mediaSession.setQueue(queue);
  17. activeQueueItemId = currentWindowIndex;
  18. }

代码示例来源:origin: google/ExoPlayer

  1. /**
  2. * Incrementally scrubs the position by {@code positionChange}.
  3. *
  4. * @param positionChange The change in the scrubber position, in milliseconds. May be negative.
  5. * @return Returns whether the scrubber position changed.
  6. */
  7. private boolean scrubIncrementally(long positionChange) {
  8. if (duration <= 0) {
  9. return false;
  10. }
  11. long scrubberPosition = getScrubberPosition();
  12. scrubPosition = Util.constrainValue(scrubberPosition + positionChange, 0, duration);
  13. if (scrubPosition == scrubberPosition) {
  14. return false;
  15. }
  16. if (!scrubbing) {
  17. startScrubbing();
  18. }
  19. for (OnScrubListener listener : listeners) {
  20. listener.onScrubMove(this, scrubPosition);
  21. }
  22. update();
  23. return true;
  24. }

代码示例来源:origin: google/ExoPlayer

  1. @Override
  2. public SeekPoints getSeekPoints(long timeUs) {
  3. timeUs = Util.constrainValue(timeUs, 0, durationUs);
  4. Pair<Long, Long> timeMsAndPosition =
  5. linearlyInterpolate(C.usToMs(timeUs), referenceTimesMs, referencePositions);
  6. timeUs = C.msToUs(timeMsAndPosition.first);
  7. long position = timeMsAndPosition.second;
  8. return new SeekPoints(new SeekPoint(timeUs, position));
  9. }

代码示例来源:origin: google/ExoPlayer

  1. @Override
  2. public SeekPoints getSeekPoints(long timeUs) {
  3. long positionOffset = (timeUs * averageBytesPerSecond) / C.MICROS_PER_SECOND;
  4. // Constrain to nearest preceding frame offset.
  5. positionOffset = (positionOffset / blockAlignment) * blockAlignment;
  6. positionOffset = Util.constrainValue(positionOffset, 0, dataSize - blockAlignment);
  7. long seekPosition = dataStartPosition + positionOffset;
  8. long seekTimeUs = getTimeUs(seekPosition);
  9. SeekPoint seekPoint = new SeekPoint(seekTimeUs, seekPosition);
  10. if (seekTimeUs >= timeUs || positionOffset == dataSize - blockAlignment) {
  11. return new SeekPoints(seekPoint);
  12. } else {
  13. long secondSeekPosition = seekPosition + blockAlignment;
  14. long secondSeekTimeUs = getTimeUs(secondSeekPosition);
  15. SeekPoint secondSeekPoint = new SeekPoint(secondSeekTimeUs, secondSeekPosition);
  16. return new SeekPoints(seekPoint, secondSeekPoint);
  17. }
  18. }

相关文章