VLCNowPlayingInterfaceController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. @interface VLCNowPlayingInterfaceController ()
  19. {
  20. CGRect _screenBounds;
  21. CGFloat _screenScale;
  22. }
  23. @property (nonatomic, copy) NSString *titleString;
  24. @property (nonatomic, copy) NSNumber *playBackDurationNumber;
  25. @property (nonatomic) BOOL isPlaying;
  26. @end
  27. @implementation VLCNowPlayingInterfaceController
  28. - (instancetype)init
  29. {
  30. self = [super init];
  31. if (self) {
  32. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  33. _isPlaying = YES;
  34. }
  35. return self;
  36. }
  37. - (void)awakeWithContext:(id)context {
  38. [super awakeWithContext:context];
  39. WKInterfaceDevice *currentDevice = [WKInterfaceDevice currentDevice];
  40. _screenBounds = currentDevice.screenBounds;
  41. _screenScale = currentDevice.screenScale;
  42. // Configure interface objects here.
  43. [self requestNowPlayingInfo];
  44. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:@"org.videolan.ios-app.nowPlayingInfoUpdate" toLocalName:@"nowPlayingInfoUpdate"];
  45. }
  46. - (void)willActivate {
  47. // This method is called when watch view controller is about to be visible to user
  48. [super willActivate];
  49. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNowPlayingInfo) name:@"nowPlayingInfoUpdate" object:nil];
  51. [self requestNowPlayingInfo];
  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. }
  58. - (void)requestNowPlayingInfo {
  59. [WKInterfaceController openParentApplication:@{@"name": @"getNowPlayingInfo"} reply:^(NSDictionary *replyInfo, NSError *error) {
  60. MLFile *file = nil;
  61. NSString *uriString = replyInfo[@"URIRepresentation"];
  62. if (uriString) {
  63. NSURL *uriRepresentation = [NSURL URLWithString:uriString];
  64. file = [MLFile fileForURIRepresentation:uriRepresentation];
  65. }
  66. [self updateWithNowPlayingInfo:replyInfo[@"nowPlayingInfo"] andFile:file];
  67. }];
  68. }
  69. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo andFile:(MLFile*)file {
  70. self.titleString = file.title ?: nowPlayingInfo[MPMediaItemPropertyTitle];
  71. NSNumber *duration = file.duration;
  72. if (!duration) {
  73. duration = nowPlayingInfo[MPMediaItemPropertyPlaybackDuration];
  74. float durationFloat = duration.floatValue;
  75. duration = @(durationFloat*1000);
  76. }
  77. NSNumber *playbackTime = nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime];
  78. float playbackProgress = 0.0;
  79. float playbackTimeFloat = playbackTime.floatValue; // seconds
  80. float durationFloat = duration.floatValue; // milliseconds
  81. if (playbackTimeFloat > 0.0 && durationFloat > 0.0) {
  82. playbackProgress = playbackTimeFloat / (durationFloat/1000);
  83. }
  84. BOOL noProgress = (playbackProgress == 0.0 || playbackProgress == 1.0);
  85. CGFloat progressWidth = floor(playbackProgress * CGRectGetWidth([WKInterfaceDevice currentDevice].screenBounds));;
  86. self.progressSeparator.width = noProgress ? 0.0 : progressWidth;
  87. self.playBackDurationNumber = duration;
  88. /* do not block */
  89. [self performSelectorInBackground:@selector(loadThumbnailForFile:) withObject:file];
  90. }
  91. - (void)loadThumbnailForFile:(MLFile *)file
  92. {
  93. UIImage *image = [VLCThumbnailsCache thumbnailForManagedObject:file toFitRect:CGRectMake(0., 0., _screenBounds.size.width * _screenScale, _screenBounds.size.height * _screenScale) shouldReplaceCache:NO];
  94. [self.playElementsGroup setBackgroundImage:image];
  95. }
  96. - (IBAction)playPausePressed {
  97. self.isPlaying = !self.isPlaying;
  98. [WKInterfaceController openParentApplication:@{@"name": @"playpause"} reply:^(NSDictionary *replyInfo, NSError *error) {
  99. NSLog(@"playpause %@",replyInfo);
  100. }];
  101. }
  102. - (IBAction)skipForward {
  103. [WKInterfaceController openParentApplication:@{@"name": @"skipForward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  104. NSLog(@"skipForward %@",replyInfo);
  105. }];
  106. }
  107. - (IBAction)skipBackward {
  108. [WKInterfaceController openParentApplication:@{@"name": @"skipBackward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  109. NSLog(@"skipBackward %@",replyInfo);
  110. }];
  111. }
  112. - (void)setIsPlaying:(BOOL)isPlaying {
  113. [self.playPauseButton setBackgroundImageNamed:isPlaying? @"pause":@"play"];
  114. _isPlaying = isPlaying;
  115. }
  116. - (void)setTitleString:(NSString *)titleString {
  117. if (![_titleString isEqualToString:titleString] || (_titleString==nil && titleString)) {
  118. _titleString = [titleString copy];
  119. [self.titleLabel setText:titleString];
  120. }
  121. }
  122. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  123. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  124. _playBackDurationNumber = playBackDurationNumber;
  125. [self.durationLabel setText:[VLCTime timeWithNumber:playBackDurationNumber].stringValue];
  126. }
  127. }
  128. @end