VLCNowPlayingInterfaceController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*****************************************************************************
  2. * VLCNowPlayingInterfaceController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCNowPlayingInterfaceController.h"
  13. #import <MediaPlayer/MediaPlayer.h>
  14. #import <MobileVLCKit/VLCTime.h>
  15. #import <MediaLibraryKit/MediaLibraryKit.h>
  16. #import "VLCNotificationRelay.h"
  17. #import "VLCThumbnailsCache.h"
  18. #import "WKInterfaceObject+VLCProgress.h"
  19. @interface VLCNowPlayingInterfaceController ()
  20. {
  21. CGRect _screenBounds;
  22. CGFloat _screenScale;
  23. }
  24. @property (nonatomic, copy) NSString *titleString;
  25. @property (nonatomic, copy) NSNumber *playBackDurationNumber;
  26. @property (nonatomic) BOOL isPlaying;
  27. @property (nonatomic) NSTimer *updateTimer;
  28. @end
  29. @implementation VLCNowPlayingInterfaceController
  30. - (instancetype)init
  31. {
  32. self = [super init];
  33. if (self) {
  34. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  35. _isPlaying = YES;
  36. }
  37. return self;
  38. }
  39. - (void)awakeWithContext:(id)context {
  40. [super awakeWithContext:context];
  41. WKInterfaceDevice *currentDevice = [WKInterfaceDevice currentDevice];
  42. _screenBounds = currentDevice.screenBounds;
  43. _screenScale = currentDevice.screenScale;
  44. // Configure interface objects here.
  45. [self requestNowPlayingInfo];
  46. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:@"org.videolan.ios-app.nowPlayingInfoUpdate" toLocalName:@"nowPlayingInfoUpdate"];
  47. }
  48. - (void)willActivate {
  49. // This method is called when watch view controller is about to be visible to user
  50. [super willActivate];
  51. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  52. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNowPlayingInfo) name:@"nowPlayingInfoUpdate" object:nil];
  53. [self requestNowPlayingInfo];
  54. const NSTimeInterval updateInterval = 5;
  55. self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:updateInterval
  56. target:self
  57. selector:@selector(requestNowPlayingInfo)
  58. userInfo:nil
  59. repeats:YES];
  60. }
  61. - (void)didDeactivate {
  62. // This method is called when watch view controller is no longer visible
  63. [super didDeactivate];
  64. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"nowPlayingInfoUpdate" object:nil];
  65. [self.updateTimer invalidate];
  66. self.updateTimer = nil;
  67. }
  68. - (void)requestNowPlayingInfo {
  69. [WKInterfaceController openParentApplication:@{@"name": @"getNowPlayingInfo"} reply:^(NSDictionary *replyInfo, NSError *error) {
  70. MLFile *file = nil;
  71. NSString *uriString = replyInfo[@"URIRepresentation"];
  72. if (uriString) {
  73. NSURL *uriRepresentation = [NSURL URLWithString:uriString];
  74. file = [MLFile fileForURIRepresentation:uriRepresentation];
  75. }
  76. [self updateWithNowPlayingInfo:replyInfo[@"nowPlayingInfo"] andFile:file];
  77. }];
  78. }
  79. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo andFile:(MLFile*)file {
  80. self.titleString = file.title ?: nowPlayingInfo[MPMediaItemPropertyTitle];
  81. NSNumber *duration = file.duration;
  82. if (!duration) {
  83. duration = nowPlayingInfo[MPMediaItemPropertyPlaybackDuration];
  84. float durationFloat = duration.floatValue;
  85. duration = @(durationFloat*1000);
  86. }
  87. NSNumber *playbackTime = nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime];
  88. float playbackTimeFloat = playbackTime.floatValue; // seconds
  89. float durationFloat = duration.floatValue; // milliseconds
  90. durationFloat/=1000; // seconds
  91. [self.progressObject vlc_setProgressFromPlaybackTime:playbackTimeFloat duration:durationFloat hideForNoProgess:YES];
  92. self.playBackDurationNumber = duration;
  93. /* do not block */
  94. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  95. }
  96. - (void)loadThumbnailForFile:(MLFile *)file
  97. {
  98. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
  99. [self.playElementsGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
  100. }
  101. - (IBAction)playPausePressed {
  102. self.isPlaying = !self.isPlaying;
  103. [WKInterfaceController openParentApplication:@{@"name": @"playpause"} reply:^(NSDictionary *replyInfo, NSError *error) {
  104. if (error)
  105. NSLog(@"playpause failed with reply %@ error: %@",replyInfo,error);
  106. }];
  107. }
  108. - (IBAction)skipForward {
  109. [WKInterfaceController openParentApplication:@{@"name": @"skipForward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  110. if (error)
  111. NSLog(@"skipForward failed with reply %@ error: %@",replyInfo,error);
  112. }];
  113. }
  114. - (IBAction)skipBackward {
  115. [WKInterfaceController openParentApplication:@{@"name": @"skipBackward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  116. if (error)
  117. NSLog(@"skipBackward failed with reply %@ error: %@",replyInfo,error);
  118. }];
  119. }
  120. - (void)setIsPlaying:(BOOL)isPlaying {
  121. [self.playPauseButton setBackgroundImageNamed:isPlaying? @"pause":@"play"];
  122. _isPlaying = isPlaying;
  123. }
  124. - (void)setTitleString:(NSString *)titleString {
  125. if (![_titleString isEqualToString:titleString] || (_titleString==nil && titleString)) {
  126. _titleString = [titleString copy];
  127. [self.titleLabel setText:titleString];
  128. }
  129. }
  130. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  131. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  132. _playBackDurationNumber = playBackDurationNumber;
  133. [self.durationLabel setText:[VLCTime timeWithNumber:playBackDurationNumber].stringValue];
  134. }
  135. }
  136. @end