VLCNowPlayingInterfaceController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <MediaLibraryKit/MediaLibraryKit.h>
  16. #import "VLCNotificationRelay.h"
  17. #import "VLCThumbnailsCache.h"
  18. #import "WKInterfaceObject+VLCProgress.h"
  19. #import "VLCWatchMessage.h"
  20. @interface VLCNowPlayingInterfaceController ()
  21. {
  22. CGRect _screenBounds;
  23. CGFloat _screenScale;
  24. }
  25. @property (nonatomic, copy) NSString *titleString;
  26. @property (nonatomic, copy) NSNumber *playBackDurationNumber;
  27. @property (nonatomic, getter=isPlaying) BOOL playing;
  28. @property (nonatomic) NSTimer *updateTimer;
  29. @property (nonatomic, weak) MLFile *currentFile;
  30. @property (nonatomic) float volume;
  31. @end
  32. @implementation VLCNowPlayingInterfaceController
  33. - (void)awakeWithContext:(id)context {
  34. [super awakeWithContext:context];
  35. WKInterfaceDevice *currentDevice = [WKInterfaceDevice currentDevice];
  36. _screenBounds = currentDevice.screenBounds;
  37. _screenScale = currentDevice.screenScale;
  38. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  39. self.skipBackwardButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", nil);
  40. self.skipForwardButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", nil);
  41. self.volumeSlider.accessibilityLabel = NSLocalizedString(@"VOLUME", nil);
  42. self.durationLabel.accessibilityLabel = NSLocalizedString(@"DURATION", nil);
  43. self.titleLabel.accessibilityLabel = NSLocalizedString(@"TITLE", nil);
  44. [self setPlaying:YES];
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNowPlayingInfo) name:@"nowPlayingInfoUpdate" object:nil];
  52. [self requestNowPlayingInfo];
  53. const NSTimeInterval updateInterval = 5;
  54. self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:updateInterval
  55. target:self
  56. selector:@selector(requestNowPlayingInfo)
  57. userInfo:nil
  58. repeats:YES];
  59. }
  60. - (void)didDeactivate {
  61. // This method is called when watch view controller is no longer visible
  62. [super didDeactivate];
  63. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"nowPlayingInfoUpdate" object:nil];
  64. [self.updateTimer invalidate];
  65. self.updateTimer = nil;
  66. }
  67. - (void)requestNowPlayingInfo {
  68. [WKInterfaceController openParentApplication:[VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameGetNowPlayingInfo] reply:^(NSDictionary *replyInfo, NSError *error) {
  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. }];
  81. }
  82. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo andFile:(MLFile*)file {
  83. self.titleString = file.title ?: nowPlayingInfo[MPMediaItemPropertyTitle];
  84. NSNumber *duration = file.duration;
  85. if (!duration) {
  86. duration = nowPlayingInfo[MPMediaItemPropertyPlaybackDuration];
  87. float durationFloat = duration.floatValue;
  88. duration = @(durationFloat*1000);
  89. }
  90. NSNumber *playbackTime = nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime];
  91. float playbackTimeFloat = playbackTime.floatValue; // seconds
  92. float durationFloat = duration.floatValue; // milliseconds
  93. durationFloat/=1000; // seconds
  94. [self.progressObject vlc_setProgressFromPlaybackTime:playbackTimeFloat duration:durationFloat hideForNoProgess:YES];
  95. self.playBackDurationNumber = duration;
  96. NSNumber *rate = nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate];
  97. self.playing = rate.floatValue > 0.0;
  98. if ([self.currentFile isEqual:file]) {
  99. self.currentFile = file;
  100. /* do not block */
  101. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  102. }
  103. }
  104. - (void)loadThumbnailForFile:(MLFile *)file
  105. {
  106. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
  107. [self.playElementsGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
  108. }
  109. - (IBAction)playPausePressed {
  110. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNamePlayPause];
  111. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  112. NSNumber *playing = replyInfo[@"playing"];
  113. if ([playing isKindOfClass:[NSNumber class]]) {
  114. self.playing = playing.boolValue;
  115. } else {
  116. self.playing = !self.playing;
  117. }
  118. if (error)
  119. NSLog(@"playpause failed with reply %@ error: %@",replyInfo,error);
  120. }];
  121. }
  122. - (IBAction)skipForward {
  123. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipForward];
  124. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  125. if (error)
  126. NSLog(@"skipForward failed with reply %@ error: %@",replyInfo,error);
  127. }];
  128. }
  129. - (IBAction)skipBackward {
  130. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipBackward];
  131. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  132. if (error)
  133. NSLog(@"skipBackward failed with reply %@ error: %@",replyInfo,error);
  134. }];
  135. }
  136. - (IBAction)volumeSliderChanged:(float)value {
  137. _volume = value;
  138. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSetVolume
  139. payload:@(value)];
  140. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  141. if (error)
  142. NSLog(@"setVolume failed with reply %@ error: %@",replyInfo,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