VLCNowPlayingInterfaceController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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) BOOL isPlaying;
  27. @property (nonatomic) NSTimer *updateTimer;
  28. @end
  29. @implementation VLCNowPlayingInterfaceController
  30. - (instancetype)init
  31. {
  32. self = [super init];
  33. if (self) {
  34. _isPlaying = YES;
  35. }
  36. return self;
  37. }
  38. - (void)awakeWithContext:(id)context {
  39. [super awakeWithContext:context];
  40. WKInterfaceDevice *currentDevice = [WKInterfaceDevice currentDevice];
  41. _screenBounds = currentDevice.screenBounds;
  42. _screenScale = currentDevice.screenScale;
  43. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  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:@{@"name": @"getNowPlayingInfo"} 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. }];
  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. /* do not block */
  92. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  93. }
  94. - (void)loadThumbnailForFile:(MLFile *)file
  95. {
  96. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
  97. [self.playElementsGroup performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
  98. }
  99. - (IBAction)playPausePressed {
  100. self.isPlaying = !self.isPlaying;
  101. [WKInterfaceController openParentApplication:@{@"name": @"playpause"} reply:^(NSDictionary *replyInfo, NSError *error) {
  102. if (error)
  103. NSLog(@"playpause failed with reply %@ error: %@",replyInfo,error);
  104. }];
  105. }
  106. - (IBAction)skipForward {
  107. [WKInterfaceController openParentApplication:@{@"name": @"skipForward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  108. if (error)
  109. NSLog(@"skipForward failed with reply %@ error: %@",replyInfo,error);
  110. }];
  111. }
  112. - (IBAction)skipBackward {
  113. [WKInterfaceController openParentApplication:@{@"name": @"skipBackward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  114. if (error)
  115. NSLog(@"skipBackward failed with reply %@ error: %@",replyInfo,error);
  116. }];
  117. }
  118. - (void)setIsPlaying:(BOOL)isPlaying {
  119. [self.playPauseButton setBackgroundImageNamed:isPlaying? @"pause":@"play"];
  120. _isPlaying = isPlaying;
  121. }
  122. - (void)setTitleString:(NSString *)titleString {
  123. if (![_titleString isEqualToString:titleString]) {
  124. _titleString = [titleString copy];
  125. [self.titleLabel setText:titleString];
  126. }
  127. }
  128. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  129. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  130. _playBackDurationNumber = playBackDurationNumber;
  131. [self.durationLabel setText:[VLCTime timeWithNumber:playBackDurationNumber].stringValue];
  132. }
  133. }
  134. @end