VLCNowPlayingInterfaceController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 setPlaying:YES];
  44. [self requestNowPlayingInfo];
  45. }
  46. // TODO: check if iPhone is connected when inteface controller activates and for each user action
  47. - (void)willActivate {
  48. // This method is called when watch view controller is about to be visible to user
  49. [super willActivate];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNowPlayingInfo) name:VLCNowPlayingUpdateNotification object:nil];
  51. [self requestNowPlayingInfo];
  52. const NSTimeInterval updateInterval = 5;
  53. self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:updateInterval
  54. target:self
  55. selector:@selector(requestNowPlayingInfo)
  56. userInfo:nil
  57. repeats:YES];
  58. }
  59. - (void)didDeactivate {
  60. // This method is called when watch view controller is no longer visible
  61. [super didDeactivate];
  62. [[NSNotificationCenter defaultCenter] removeObserver:self name:VLCNowPlayingUpdateNotification object:nil];
  63. [self.updateTimer invalidate];
  64. self.updateTimer = nil;
  65. }
  66. // TODO: don't query for after receiving a notification from iPhone instead add user info to message which sends the notification
  67. // and use that user info dictionary
  68. - (void)requestNowPlayingInfo {
  69. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameGetNowPlayingInfo];
  70. [[WCSession defaultSession] sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyInfo) {
  71. MLFile *file = nil;
  72. NSString *uriString = replyInfo[@"URIRepresentation"];
  73. if (uriString) {
  74. NSURL *uriRepresentation = [NSURL URLWithString:uriString];
  75. file = [MLFile fileForURIRepresentation:uriRepresentation];
  76. }
  77. [self updateWithNowPlayingInfo:replyInfo[@"nowPlayingInfo"] andFile:file];
  78. NSNumber *currentVolume = replyInfo[@"volume"];
  79. if (currentVolume) {
  80. self.volume = currentVolume.floatValue;
  81. }
  82. } errorHandler:nil];
  83. }
  84. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo andFile:(MLFile*)file {
  85. // TODO: fix key use
  86. self.titleString = file.title ?: nowPlayingInfo[@"title"];
  87. NSNumber *duration = file.duration;
  88. if (!duration) {
  89. duration = nowPlayingInfo[@"playbackDuration"];
  90. float durationFloat = duration.floatValue;
  91. duration = @(durationFloat*1000);
  92. }
  93. NSNumber *playbackTime = nowPlayingInfo[@"MPNowPlayingInfoPropertyElapsedPlaybackTime"];
  94. float playbackTimeFloat = playbackTime.floatValue; // seconds
  95. float durationFloat = duration.floatValue; // milliseconds
  96. durationFloat/=1000; // seconds
  97. [self.progressObject vlc_setProgressFromPlaybackTime:playbackTimeFloat duration:durationFloat hideForNoProgess:YES];
  98. self.playBackDurationNumber = duration;
  99. NSNumber *rate = nowPlayingInfo[@"MPNowPlayingInfoPropertyPlaybackRate"];
  100. self.playing = rate.floatValue > 0.0;
  101. if ([self.currentFile isEqual:file]) {
  102. self.currentFile = file;
  103. /* do not block */
  104. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  105. }
  106. }
  107. - (void)loadThumbnailForFile:(MLFile *)file
  108. {
  109. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
  110. [self.playElementsGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
  111. }
  112. - (IBAction)playPausePressed {
  113. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNamePlayPause];
  114. [[WCSession defaultSession] sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyInfo) {
  115. NSNumber *playing = replyInfo[@"playing"];
  116. if ([playing isKindOfClass:[NSNumber class]]) {
  117. self.playing = playing.boolValue;
  118. } else {
  119. self.playing = !self.playing;
  120. }
  121. } errorHandler:^(NSError * _Nonnull error) {
  122. NSLog(@"playpause failed with error: %@",error);
  123. }];
  124. }
  125. - (IBAction)skipForward {
  126. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipForward];
  127. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  128. NSLog(@"skipForward failed with error: %@",error);
  129. }];
  130. }
  131. - (IBAction)skipBackward {
  132. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipBackward];
  133. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  134. NSLog(@"skipBackward failed with error: %@",error);
  135. }];
  136. }
  137. - (IBAction)volumeSliderChanged:(float)value {
  138. _volume = value;
  139. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSetVolume
  140. payload:@(value)];
  141. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  142. NSLog(@"setVolume failed with error: %@",error);
  143. }];
  144. }
  145. - (void)setVolume:(float)volume
  146. {
  147. if (_volume != volume) {
  148. _volume = volume;
  149. self.volumeSlider.value = volume;
  150. }
  151. }
  152. - (void)setPlaying:(BOOL)playing {
  153. if (_playing != playing) {
  154. [self.playPauseButtonGroup setBackgroundImageNamed:playing? @"pause":@"play"];
  155. self.playPauseButton.accessibilityLabel = playing ? NSLocalizedString(@"PAUSE_BUTTON", nil) : NSLocalizedString(@"PLAY_BUTTON", nil);
  156. _playing = playing;
  157. }
  158. }
  159. - (void)setTitleString:(NSString *)titleString {
  160. if (![_titleString isEqualToString:titleString]) {
  161. _titleString = [titleString copy];
  162. self.titleLabel.text = titleString;
  163. self.titleLabel.accessibilityValue = titleString;
  164. }
  165. }
  166. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  167. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  168. _playBackDurationNumber = playBackDurationNumber;
  169. NSString *durationString = [VLCTime timeWithNumber:playBackDurationNumber].stringValue;
  170. self.durationLabel.text = durationString;
  171. self.durationLabel.accessibilityValue = durationString;
  172. }
  173. }
  174. @end