VLCNowPlayingInterfaceController.m 7.2 KB

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