메타데이터 가져오기

재생 중

미디어의 메타데이터는 재생 중에 여러 방법으로 가져올 수 있습니다. 가장 간단한 방법은 Player.Listener#onMediaMetadataChanged 이벤트를 수신 대기하는 것입니다. 이렇게 하면 사용할 수 있는 MediaMetadata 객체가 제공되며, 이 객체에는 titlealbumArtist과 같은 필드가 있습니다. 또는 Player#getMediaMetadata를 호출하면 동일한 객체가 반환됩니다.

Kotlin

override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) {
  mediaMetadata.title?.let(::handleTitle)
}

자바

@Override
public void onMediaMetadataChanged(MediaMetadata mediaMetadata) {
  if (mediaMetadata.title != null) {
    handleTitle(mediaMetadata.title);
  }
}

앱에서 특정 Metadata.Entry 객체에 액세스해야 하는 경우 Player.Listener#onMetadata (재생 중에 제공되는 동적 메타데이터용)를 수신해야 합니다. 또는 정적 메타데이터를 살펴봐야 하는 경우 TrackSelections#getFormat를 통해 액세스할 수 있습니다. Player#getMediaMetadata은 이 두 소스에서 모두 채워집니다.

재생 없이

재생이 필요하지 않은 경우 플레이어를 만들고 준비하지 않아도 되므로 MetadataRetriever를 사용하여 메타데이터를 추출하는 것이 더 효율적입니다.

Kotlin

try {
  MetadataRetriever.Builder(context, mediaItem).build().use { metadataRetriever ->
    val trackGroups = metadataRetriever.retrieveTrackGroups().await()
    val timeline = metadataRetriever.retrieveTimeline().await()
    val durationUs = metadataRetriever.retrieveDurationUs().await()
    handleMetadata(trackGroups, timeline, durationUs)
  }
} catch (e: IOException) {
  handleFailure(e)
}

자바

try (MetadataRetriever metadataRetriever =
    new MetadataRetriever.Builder(context, mediaItem).build()) {
  ListenableFuture<TrackGroupArray> trackGroupsFuture = metadataRetriever.retrieveTrackGroups();
  ListenableFuture<Timeline> timelineFuture = metadataRetriever.retrieveTimeline();
  ListenableFuture<Long> durationUsFuture = metadataRetriever.retrieveDurationUs();
  ListenableFuture<List<Object>> allFutures =
      Futures.allAsList(trackGroupsFuture, timelineFuture, durationUsFuture);
  Futures.addCallback(
      allFutures,
      new FutureCallback<List<Object>>() {
        @Override
        public void onSuccess(List<Object> result) {
          handleMetadata(
              Futures.getUnchecked(trackGroupsFuture),
              Futures.getUnchecked(timelineFuture),
              Futures.getUnchecked(durationUsFuture));
        }

        @Override
        public void onFailure(Throwable t) {
          handleFailure(t);
        }
      },
      executor);
}

모션 사진

파일의 이미지 및 동영상 부분의 오프셋과 길이를 비롯한 모션 사진 메타데이터를 추출할 수도 있습니다.

모션 사진의 경우 MetadataRetriever로 획득한 TrackGroupArray에는 MotionPhotoMetadata 메타데이터 항목을 둘러싸는 단일 Format이 있는 TrackGroup가 포함되어 있습니다.

Kotlin

0.until(trackGroups.length)
  .asSequence()
  .mapNotNull { trackGroups[it].getFormat(0).metadata }
  .filter { metadata -> metadata.length() == 1 }
  .map { metadata -> metadata[0] }
  .filterIsInstance<MotionPhotoMetadata>()
  .forEach(::handleMotionPhotoMetadata)

Java

for (int i = 0; i < trackGroups.length; i++) {
  TrackGroup trackGroup = trackGroups.get(i);
  Metadata metadata = trackGroup.getFormat(0).metadata;
  if (metadata != null && metadata.length() == 1) {
    Metadata.Entry metadataEntry = metadata.get(0);
    if (metadataEntry instanceof MotionPhotoMetadata) {
      MotionPhotoMetadata motionPhotoMetadata = (MotionPhotoMetadata) metadataEntry;
      handleMotionPhotoMetadata(motionPhotoMetadata);
    }
  }
}