VLCNowPlayingInterfaceController.m 4.8 KB

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