VLCNowPlayingInterfaceController.m 6.4 KB

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