VLCNowPlayingInterfaceController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "VLCTime.h"
  14. #import "WKInterfaceObject+VLCProgress.h"
  15. #import "VLCWatchMessage.h"
  16. #import "VLCThumbnailsCache.h"
  17. #import <WatchConnectivity/WatchConnectivity.h>
  18. static NSString *const VLCNowPlayingUpdateNotification = @"VLCPlaybackControllerPlaybackMetadataDidChange";
  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, getter=isPlaying) BOOL playing;
  27. @property (nonatomic) NSTimer *updateTimer;
  28. @property (nonatomic, weak) MLFile *currentFile;
  29. @property (nonatomic) float volume;
  30. @end
  31. @implementation VLCNowPlayingInterfaceController
  32. - (void)awakeWithContext:(id)context {
  33. [super awakeWithContext:context];
  34. WKInterfaceDevice *currentDevice = [WKInterfaceDevice currentDevice];
  35. _screenBounds = currentDevice.screenBounds;
  36. _screenScale = currentDevice.screenScale;
  37. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  38. self.skipBackwardButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", nil);
  39. self.skipForwardButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", nil);
  40. self.volumeSlider.accessibilityLabel = NSLocalizedString(@"VOLUME", nil);
  41. self.durationLabel.accessibilityLabel = NSLocalizedString(@"DURATION", nil);
  42. self.titleLabel.accessibilityLabel = NSLocalizedString(@"TITLE", nil);
  43. self.volume = -1.0;
  44. [self setPlaying:YES];
  45. [self requestNowPlayingInfo];
  46. [self vlc_performBlockIfSessionReachable:nil showUnreachableAlert:YES alertOKAction:^{
  47. [self dismissController];
  48. }];
  49. }
  50. - (void)willActivate {
  51. // This method is called when watch view controller is about to be visible to user
  52. [super willActivate];
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNowPlayingInfo) name:VLCNowPlayingUpdateNotification object:nil];
  54. [self requestNowPlayingInfo];
  55. const NSTimeInterval updateInterval = 5;
  56. self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:updateInterval
  57. target:self
  58. selector:@selector(requestNowPlayingInfo)
  59. userInfo:nil
  60. repeats:YES];
  61. }
  62. - (void)didDeactivate {
  63. // This method is called when watch view controller is no longer visible
  64. [super didDeactivate];
  65. [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCNowPlayingUpdateNotification object:nil];
  66. [self.updateTimer invalidate];
  67. self.updateTimer = nil;
  68. }
  69. // TODO: don't query for after receiving a notification from iPhone instead add user info to message which sends the notification
  70. // and use that user info dictionary
  71. - (void)requestNowPlayingInfo {
  72. [self vlc_performBlockIfSessionReachable:^{
  73. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameGetNowPlayingInfo];
  74. [[WCSession defaultSession] sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyInfo) {
  75. MLFile *file = nil;
  76. NSString *uriString = replyInfo[VLCWatchMessageKeyURIRepresentation];
  77. if (uriString) {
  78. NSURL *uriRepresentation = [NSURL URLWithString:uriString];
  79. file = [MLFile fileForURIRepresentation:uriRepresentation];
  80. }
  81. [self updateWithNowPlayingInfo:replyInfo[@"nowPlayingInfo"] andFile:file];
  82. NSNumber *currentVolume = replyInfo[@"volume"];
  83. if (currentVolume) {
  84. self.volume = currentVolume.floatValue;
  85. }
  86. } errorHandler:nil];
  87. } showUnreachableAlert:NO];
  88. }
  89. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo andFile:(MLFile*)file {
  90. // TODO: fix key use
  91. self.titleString = file.title ?: nowPlayingInfo[@"title"];
  92. NSNumber *duration = file.duration;
  93. if (!duration) {
  94. duration = nowPlayingInfo[@"playbackDuration"];
  95. float durationFloat = duration.floatValue;
  96. duration = @(durationFloat*1000);
  97. }
  98. NSNumber *playbackTime = nowPlayingInfo[@"MPNowPlayingInfoPropertyElapsedPlaybackTime"];
  99. float playbackTimeFloat = playbackTime.floatValue; // seconds
  100. float durationFloat = duration.floatValue; // milliseconds
  101. durationFloat/=1000; // seconds
  102. [self.progressObject vlc_setProgressFromPlaybackTime:playbackTimeFloat duration:durationFloat hideForNoProgess:YES];
  103. self.playBackDurationNumber = duration;
  104. NSNumber *rate = nowPlayingInfo[@"MPNowPlayingInfoPropertyPlaybackRate"];
  105. self.playing = rate.floatValue > 0.0;
  106. if ([self.currentFile isEqual:file]) {
  107. self.currentFile = file;
  108. /* do not block */
  109. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  110. }
  111. }
  112. - (void)loadThumbnailForFile:(MLFile *)file
  113. {
  114. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file refreshCache:YES toFitRect:_screenBounds scale:_screenScale shouldReplaceCache:NO];
  115. [self.playElementsGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
  116. }
  117. - (IBAction)playPausePressed {
  118. [self vlc_performBlockIfSessionReachable:^{
  119. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNamePlayPause];
  120. [[WCSession defaultSession] sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyInfo) {
  121. NSNumber *playing = replyInfo[@"playing"];
  122. if ([playing isKindOfClass:[NSNumber class]]) {
  123. self.playing = playing.boolValue;
  124. } else {
  125. self.playing = !self.playing;
  126. }
  127. } errorHandler:^(NSError * _Nonnull error) {
  128. NSLog(@"playpause failed with error: %@",error);
  129. }];
  130. } showUnreachableAlert:YES];
  131. }
  132. - (IBAction)skipForward {
  133. [self vlc_performBlockIfSessionReachable:^{
  134. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipForward];
  135. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  136. NSLog(@"skipForward failed with error: %@",error);
  137. }];
  138. } showUnreachableAlert:YES];
  139. }
  140. - (IBAction)skipBackward {
  141. [self vlc_performBlockIfSessionReachable:^{
  142. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipBackward];
  143. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  144. NSLog(@"skipBackward failed with error: %@",error);
  145. }];
  146. } showUnreachableAlert:YES];
  147. }
  148. - (IBAction)volumeSliderChanged:(float)value {
  149. _volume = value;
  150. [self vlc_performBlockIfSessionReachable:^{
  151. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSetVolume
  152. payload:@(value)];
  153. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  154. NSLog(@"setVolume failed with error: %@",error);
  155. }];
  156. } showUnreachableAlert:YES];
  157. }
  158. #pragma mark value comparing setters -
  159. - (void)setVolume:(float)volume
  160. {
  161. if (_volume != volume) {
  162. _volume = volume;
  163. self.volumeSlider.value = volume;
  164. }
  165. }
  166. - (void)setPlaying:(BOOL)playing {
  167. if (_playing != playing) {
  168. [self.playPauseButtonGroup setBackgroundImageNamed:playing? @"pause":@"play"];
  169. self.playPauseButton.accessibilityLabel = playing ? NSLocalizedString(@"PAUSE_BUTTON", nil) : NSLocalizedString(@"PLAY_BUTTON", nil);
  170. _playing = playing;
  171. }
  172. }
  173. - (void)setTitleString:(NSString *)titleString {
  174. if (![_titleString isEqualToString:titleString]) {
  175. _titleString = [titleString copy];
  176. self.titleLabel.text = titleString;
  177. self.titleLabel.accessibilityValue = titleString;
  178. }
  179. }
  180. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  181. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  182. _playBackDurationNumber = playBackDurationNumber;
  183. NSString *durationString = [VLCTime timeWithNumber:playBackDurationNumber].stringValue;
  184. self.durationLabel.text = durationString;
  185. self.durationLabel.accessibilityValue = durationString;
  186. }
  187. }
  188. @end