VLCNowPlayingInterfaceController.m 5.3 KB

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