VLCNowPlayingInterfaceController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "VLCNotificationRelay.h"
  15. #import "WKInterfaceObject+VLCProgress.h"
  16. #import "VLCWatchMessage.h"
  17. #import "VLCThumbnailsCache.h"
  18. #import <WatchConnectivity/WatchConnectivity.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, 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. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:@"org.videolan.ios-app.nowPlayingInfoUpdate" toLocalName:@"nowPlayingInfoUpdate"];
  46. }
  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:@"nowPlayingInfoUpdate" 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:@"nowPlayingInfoUpdate" object:nil];
  63. [self.updateTimer invalidate];
  64. self.updateTimer = nil;
  65. }
  66. - (void)requestNowPlayingInfo {
  67. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameGetNowPlayingInfo];
  68. [[WCSession defaultSession] sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyInfo) {
  69. MLFile *file = nil;
  70. NSString *uriString = replyInfo[@"URIRepresentation"];
  71. if (uriString) {
  72. NSURL *uriRepresentation = [NSURL URLWithString:uriString];
  73. file = [MLFile fileForURIRepresentation:uriRepresentation];
  74. }
  75. [self updateWithNowPlayingInfo:replyInfo[@"nowPlayingInfo"] andFile:file];
  76. NSNumber *currentVolume = replyInfo[@"volume"];
  77. if (currentVolume) {
  78. self.volume = currentVolume.floatValue;
  79. }
  80. } errorHandler:nil];
  81. }
  82. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo andFile:(MLFile*)file {
  83. // TODO: fix key use
  84. self.titleString = file.title ?: nowPlayingInfo[@"title"];
  85. NSNumber *duration = file.duration;
  86. if (!duration) {
  87. duration = nowPlayingInfo[@"playbackDuration"];
  88. float durationFloat = duration.floatValue;
  89. duration = @(durationFloat*1000);
  90. }
  91. NSNumber *playbackTime = nowPlayingInfo[@"MPNowPlayingInfoPropertyElapsedPlaybackTime"];
  92. float playbackTimeFloat = playbackTime.floatValue; // seconds
  93. float durationFloat = duration.floatValue; // milliseconds
  94. durationFloat/=1000; // seconds
  95. [self.progressObject vlc_setProgressFromPlaybackTime:playbackTimeFloat duration:durationFloat hideForNoProgess:YES];
  96. self.playBackDurationNumber = duration;
  97. NSNumber *rate = nowPlayingInfo[@"MPNowPlayingInfoPropertyPlaybackRate"];
  98. self.playing = rate.floatValue > 0.0;
  99. if ([self.currentFile isEqual:file]) {
  100. self.currentFile = file;
  101. /* do not block */
  102. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  103. }
  104. }
  105. - (void)loadThumbnailForFile:(MLFile *)file
  106. {
  107. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
  108. [self.playElementsGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
  109. }
  110. - (IBAction)playPausePressed {
  111. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNamePlayPause];
  112. [[WCSession defaultSession] sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyInfo) {
  113. NSNumber *playing = replyInfo[@"playing"];
  114. if ([playing isKindOfClass:[NSNumber class]]) {
  115. self.playing = playing.boolValue;
  116. } else {
  117. self.playing = !self.playing;
  118. }
  119. } errorHandler:^(NSError * _Nonnull error) {
  120. NSLog(@"playpause failed with error: %@",error);
  121. }];
  122. }
  123. - (IBAction)skipForward {
  124. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipForward];
  125. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  126. NSLog(@"skipForward failed with error: %@",error);
  127. }];
  128. }
  129. - (IBAction)skipBackward {
  130. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipBackward];
  131. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  132. NSLog(@"skipBackward failed with error: %@",error);
  133. }];
  134. }
  135. - (IBAction)volumeSliderChanged:(float)value {
  136. _volume = value;
  137. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSetVolume
  138. payload:@(value)];
  139. [[WCSession defaultSession] sendMessage:dict replyHandler:nil errorHandler:^(NSError * _Nonnull error) {
  140. NSLog(@"setVolume failed with error: %@",error);
  141. }];
  142. }
  143. - (void)setVolume:(float)volume
  144. {
  145. if (_volume != volume) {
  146. _volume = volume;
  147. self.volumeSlider.value = volume;
  148. }
  149. }
  150. - (void)setPlaying:(BOOL)playing {
  151. if (_playing != playing) {
  152. [self.playPauseButtonGroup setBackgroundImageNamed:playing? @"pause":@"play"];
  153. self.playPauseButton.accessibilityLabel = playing ? NSLocalizedString(@"PAUSE_BUTTON", nil) : NSLocalizedString(@"PLAY_BUTTON", nil);
  154. _playing = playing;
  155. }
  156. }
  157. - (void)setTitleString:(NSString *)titleString {
  158. if (![_titleString isEqualToString:titleString]) {
  159. _titleString = [titleString copy];
  160. self.titleLabel.text = titleString;
  161. self.titleLabel.accessibilityValue = titleString;
  162. }
  163. }
  164. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  165. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  166. _playBackDurationNumber = playBackDurationNumber;
  167. NSString *durationString = [VLCTime timeWithNumber:playBackDurationNumber].stringValue;
  168. self.durationLabel.text = durationString;
  169. self.durationLabel.accessibilityValue = durationString;
  170. }
  171. }
  172. @end