VLCMetadata.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // VLCMediaPlayer + Metadata.m
  3. // VLC
  4. //
  5. // Created by Carola Nitz on 9/27/17.
  6. // Copyright © 2017 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCMetadata.h"
  9. #import <MediaPlayer/MediaPlayer.h>
  10. #import "VLCPlaybackController.h"
  11. #if TARGET_OS_IOS
  12. #import "VLC-Swift.h"
  13. #import "VLCThumbnailsCache.h"
  14. #endif
  15. @implementation VLCMetaData
  16. - (instancetype)init
  17. {
  18. self = [super init];
  19. if (self) {
  20. }
  21. return self;
  22. }
  23. - (void)updateMetadataFromMediaPlayer:(VLCMediaPlayer *)mediaPlayer;
  24. {
  25. #if TARGET_OS_IOS
  26. MLFile *item;
  27. self.trackNumber = nil;
  28. self.title = @"";
  29. self.artist = @"";
  30. self.albumName = @"";
  31. self.artworkImage = nil;
  32. self.isAudioOnly = NO;
  33. if ([VLCPlaybackController sharedInstance].mediaList) {
  34. NSArray *matches = [MLFile fileForURL:mediaPlayer.media.url];
  35. item = matches.firstObject;
  36. }
  37. if (item) {
  38. if (item.isAlbumTrack) {
  39. self.title = item.albumTrack.title;
  40. self.artist = item.albumTrack.artist;
  41. self.albumName = item.albumTrack.album.name;
  42. } else
  43. self.title = item.title;
  44. /* MLKit knows better than us if this thing is audio only or not */
  45. self.isAudioOnly = [item isSupportedAudioFile];
  46. } else {
  47. #endif
  48. NSDictionary * metaDict = mediaPlayer.media.metaDictionary;
  49. if (metaDict) {
  50. self.title = metaDict[VLCMetaInformationNowPlaying] ? metaDict[VLCMetaInformationNowPlaying] : metaDict[VLCMetaInformationTitle];
  51. self.artist = metaDict[VLCMetaInformationArtist];
  52. self.albumName = metaDict[VLCMetaInformationAlbum];
  53. self.trackNumber = metaDict[VLCMetaInformationTrackNumber];
  54. }
  55. #if TARGET_OS_IOS
  56. }
  57. #endif
  58. if (!self.isAudioOnly) {
  59. /* either what we are playing is not a file known to MLKit or
  60. * MLKit fails to acknowledge that it is audio-only.
  61. * Either way, do a more expensive check to see if it is really audio-only */
  62. NSArray *tracks = mediaPlayer.media.tracksInformation;
  63. NSUInteger trackCount = tracks.count;
  64. self.isAudioOnly = YES;
  65. for (NSUInteger x = 0 ; x < trackCount; x++) {
  66. if ([[tracks[x] objectForKey:VLCMediaTracksInformationType] isEqualToString:VLCMediaTracksInformationTypeVideo]) {
  67. self.isAudioOnly = NO;
  68. break;
  69. }
  70. }
  71. }
  72. if (self.isAudioOnly) {
  73. #if TARGET_OS_IOS
  74. self.artworkImage = [VLCThumbnailsCache thumbnailForManagedObject:item];
  75. if (self.artworkImage) {
  76. if (self.artist)
  77. self.title = [self.title stringByAppendingFormat:@" — %@", self.artist];
  78. if (self.albumName)
  79. self.title = [self.title stringByAppendingFormat:@" — %@", self.albumName];
  80. }
  81. #endif
  82. if (self.title.length < 1)
  83. self.title = [[mediaPlayer.media url] lastPathComponent];
  84. }
  85. self.playbackDuration = @(mediaPlayer.media.length.intValue / 1000.);
  86. self.playbackRate = @(mediaPlayer.rate);
  87. self.elapsedPlaybackTime = @(mediaPlayer.time.value.floatValue / 1000.);
  88. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackMetadataDidChange object:self];
  89. #if TARGET_OS_IOS
  90. if ([VLCKeychainCoordinator passcodeLockEnabled]) return;
  91. #endif
  92. [self populateInfoCenterFromMetadata];
  93. }
  94. - (void)populateInfoCenterFromMetadata
  95. {
  96. NSMutableDictionary *currentlyPlayingTrackInfo = [NSMutableDictionary dictionary];
  97. currentlyPlayingTrackInfo[MPMediaItemPropertyPlaybackDuration] = self.playbackDuration;
  98. currentlyPlayingTrackInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = self.elapsedPlaybackTime;
  99. currentlyPlayingTrackInfo[MPNowPlayingInfoPropertyPlaybackRate] = self.playbackRate;
  100. currentlyPlayingTrackInfo[MPMediaItemPropertyTitle] = self.title;
  101. currentlyPlayingTrackInfo[MPMediaItemPropertyArtist] = self.artist;
  102. currentlyPlayingTrackInfo[MPMediaItemPropertyAlbumTitle] = self.albumName;
  103. if ([self.trackNumber intValue] > 0)
  104. currentlyPlayingTrackInfo[MPMediaItemPropertyAlbumTrackNumber] = self.trackNumber;
  105. #if TARGET_OS_IOS
  106. if (self.artworkImage) {
  107. MPMediaItemArtwork *mpartwork = [[MPMediaItemArtwork alloc] initWithImage:self.artworkImage];
  108. currentlyPlayingTrackInfo[MPMediaItemPropertyArtwork] = mpartwork;
  109. }
  110. #endif
  111. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
  112. }
  113. @end