VLCMetadata.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "VLCKeychainCoordinator.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. if ([VLCPlaybackController sharedInstance].mediaList) {
  28. NSArray *matches = [MLFile fileForURL:mediaPlayer.media.url];
  29. item = matches.firstObject;
  30. }
  31. if (item) {
  32. if (item.isAlbumTrack) {
  33. self.title = item.albumTrack.title;
  34. self.artist = item.albumTrack.artist;
  35. self.albumName = item.albumTrack.album.name;
  36. } else
  37. self.title = item.title;
  38. /* MLKit knows better than us if this thing is audio only or not */
  39. self.isAudioOnly = [item isSupportedAudioFile];
  40. } else {
  41. #endif
  42. NSDictionary * metaDict = mediaPlayer.media.metaDictionary;
  43. if (metaDict) {
  44. self.title = metaDict[VLCMetaInformationNowPlaying] ? metaDict[VLCMetaInformationNowPlaying] : metaDict[VLCMetaInformationTitle];
  45. self.artist = metaDict[VLCMetaInformationArtist];
  46. self.albumName = metaDict[VLCMetaInformationAlbum];
  47. self.trackNumber = metaDict[VLCMetaInformationTrackNumber];
  48. }
  49. #if TARGET_OS_IOS
  50. }
  51. #endif
  52. if (!self.isAudioOnly) {
  53. /* either what we are playing is not a file known to MLKit or
  54. * MLKit fails to acknowledge that it is audio-only.
  55. * Either way, do a more expensive check to see if it is really audio-only */
  56. NSArray *tracks = mediaPlayer.media.tracksInformation;
  57. NSUInteger trackCount = tracks.count;
  58. self.isAudioOnly = YES;
  59. for (NSUInteger x = 0 ; x < trackCount; x++) {
  60. if ([[tracks[x] objectForKey:VLCMediaTracksInformationType] isEqualToString:VLCMediaTracksInformationTypeVideo]) {
  61. self.isAudioOnly = NO;
  62. break;
  63. }
  64. }
  65. }
  66. if (self.isAudioOnly) {
  67. #if TARGET_OS_IOS
  68. self.artworkImage = [VLCThumbnailsCache thumbnailForManagedObject:item];
  69. if (self.artworkImage) {
  70. if (self.artist)
  71. self.title = [self.title stringByAppendingFormat:@" — %@", self.artist];
  72. if (self.albumName)
  73. self.title = [self.title stringByAppendingFormat:@" — %@", self.albumName];
  74. }
  75. #endif
  76. if (self.title.length < 1)
  77. self.title = [[mediaPlayer.media url] lastPathComponent];
  78. }
  79. self.playbackDuration = @(mediaPlayer.media.length.intValue / 1000.);
  80. self.playbackRate = @(mediaPlayer.rate);
  81. self.elapsedPlaybackTime = @(mediaPlayer.media.length.intValue / 1000.);
  82. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackMetadataDidChange object:self];
  83. #if TARGET_OS_IOS
  84. if ([[VLCKeychainCoordinator defaultCoordinator] passcodeLockEnabled]) return;
  85. #endif
  86. [self populateInfoCenterFromMetadata];
  87. }
  88. - (void)populateInfoCenterFromMetadata
  89. {
  90. NSMutableDictionary *currentlyPlayingTrackInfo = [NSMutableDictionary dictionary];
  91. currentlyPlayingTrackInfo[MPMediaItemPropertyPlaybackDuration] = self.playbackDuration;
  92. currentlyPlayingTrackInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = self.elapsedPlaybackTime;
  93. currentlyPlayingTrackInfo[MPNowPlayingInfoPropertyPlaybackRate] = self.playbackRate;
  94. currentlyPlayingTrackInfo[MPMediaItemPropertyTitle] = self.title;
  95. currentlyPlayingTrackInfo[MPMediaItemPropertyArtist] = self.artist;
  96. currentlyPlayingTrackInfo[MPMediaItemPropertyAlbumTitle] = self.albumName;
  97. if ([self.trackNumber intValue] > 0)
  98. currentlyPlayingTrackInfo[MPMediaItemPropertyAlbumTrackNumber] = self.trackNumber;
  99. #if TARGET_OS_IOS
  100. if (self.artworkImage) {
  101. MPMediaItemArtwork *mpartwork = [[MPMediaItemArtwork alloc] initWithImage:self.artworkImage];
  102. currentlyPlayingTrackInfo[MPMediaItemPropertyArtwork] = mpartwork;
  103. }
  104. #endif
  105. [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
  106. }
  107. @end