VLCNowPlayingInterfaceController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <MobileVLCKit/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 setPlaying:YES];
  40. [self requestNowPlayingInfo];
  41. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:@"org.videolan.ios-app.nowPlayingInfoUpdate" toLocalName:@"nowPlayingInfoUpdate"];
  42. }
  43. - (void)willActivate {
  44. // This method is called when watch view controller is about to be visible to user
  45. [super willActivate];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNowPlayingInfo) name:@"nowPlayingInfoUpdate" object:nil];
  47. [self requestNowPlayingInfo];
  48. const NSTimeInterval updateInterval = 5;
  49. self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:updateInterval
  50. target:self
  51. selector:@selector(requestNowPlayingInfo)
  52. userInfo:nil
  53. repeats:YES];
  54. }
  55. - (void)didDeactivate {
  56. // This method is called when watch view controller is no longer visible
  57. [super didDeactivate];
  58. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"nowPlayingInfoUpdate" object:nil];
  59. [self.updateTimer invalidate];
  60. self.updateTimer = nil;
  61. }
  62. - (void)requestNowPlayingInfo {
  63. [WKInterfaceController openParentApplication:[VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameGetNowPlayingInfo] reply:^(NSDictionary *replyInfo, NSError *error) {
  64. MLFile *file = nil;
  65. NSString *uriString = replyInfo[@"URIRepresentation"];
  66. if (uriString) {
  67. NSURL *uriRepresentation = [NSURL URLWithString:uriString];
  68. file = [MLFile fileForURIRepresentation:uriRepresentation];
  69. }
  70. [self updateWithNowPlayingInfo:replyInfo[@"nowPlayingInfo"] andFile:file];
  71. NSNumber *currentVolume = replyInfo[@"volume"];
  72. if (currentVolume) {
  73. self.volume = currentVolume.floatValue;
  74. }
  75. }];
  76. }
  77. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo andFile:(MLFile*)file {
  78. self.titleString = file.title ?: nowPlayingInfo[MPMediaItemPropertyTitle];
  79. NSNumber *duration = file.duration;
  80. if (!duration) {
  81. duration = nowPlayingInfo[MPMediaItemPropertyPlaybackDuration];
  82. float durationFloat = duration.floatValue;
  83. duration = @(durationFloat*1000);
  84. }
  85. NSNumber *playbackTime = nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime];
  86. float playbackTimeFloat = playbackTime.floatValue; // seconds
  87. float durationFloat = duration.floatValue; // milliseconds
  88. durationFloat/=1000; // seconds
  89. [self.progressObject vlc_setProgressFromPlaybackTime:playbackTimeFloat duration:durationFloat hideForNoProgess:YES];
  90. self.playBackDurationNumber = duration;
  91. NSNumber *rate = nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate];
  92. self.playing = rate.floatValue > 0.0;
  93. if ([self.currentFile isEqual:file]) {
  94. self.currentFile = file;
  95. /* do not block */
  96. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  97. }
  98. }
  99. - (void)loadThumbnailForFile:(MLFile *)file
  100. {
  101. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
  102. [self.playElementsGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
  103. }
  104. - (IBAction)playPausePressed {
  105. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNamePlayPause];
  106. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  107. NSNumber *playing = replyInfo[@"playing"];
  108. if ([playing isKindOfClass:[NSNumber class]]) {
  109. self.playing = playing.boolValue;
  110. } else {
  111. self.playing = !self.playing;
  112. }
  113. if (error)
  114. NSLog(@"playpause failed with reply %@ error: %@",replyInfo,error);
  115. }];
  116. }
  117. - (IBAction)skipForward {
  118. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipForward];
  119. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  120. if (error)
  121. NSLog(@"skipForward failed with reply %@ error: %@",replyInfo,error);
  122. }];
  123. }
  124. - (IBAction)skipBackward {
  125. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSkipBackward];
  126. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  127. if (error)
  128. NSLog(@"skipBackward failed with reply %@ error: %@",replyInfo,error);
  129. }];
  130. }
  131. - (IBAction)volumeSliderChanged:(float)value {
  132. _volume = value;
  133. NSDictionary *dict = [VLCWatchMessage messageDictionaryForName:VLCWatchMessageNameSetVolume
  134. payload:@(value)];
  135. [WKInterfaceController openParentApplication:dict reply:^(NSDictionary *replyInfo, NSError *error) {
  136. if (error)
  137. NSLog(@"setVolume failed with reply %@ error: %@",replyInfo,error);
  138. }];
  139. }
  140. - (void)setVolume:(float)volume
  141. {
  142. if (_volume != volume) {
  143. _volume = volume;
  144. self.volumeSlider.value = volume;
  145. }
  146. }
  147. - (void)setPlaying:(BOOL)playing {
  148. if (_playing != playing) {
  149. [self.playPauseButton setBackgroundImageNamed:playing? @"pause":@"play"];
  150. _playing = playing;
  151. }
  152. }
  153. - (void)setTitleString:(NSString *)titleString {
  154. if (![_titleString isEqualToString:titleString]) {
  155. _titleString = [titleString copy];
  156. [self.titleLabel setText:titleString];
  157. }
  158. }
  159. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  160. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  161. _playBackDurationNumber = playBackDurationNumber;
  162. [self.durationLabel setText:[VLCTime timeWithNumber:playBackDurationNumber].stringValue];
  163. }
  164. }
  165. @end