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