VLCNowPlayingInterfaceController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. float durationFloat = duration.floatValue;
  65. duration = @(durationFloat*1000);
  66. }
  67. self.playBackDurationNumber = duration;
  68. self.image.image = [VLCThumbnailsCache thumbnailForManagedObject:file];
  69. }
  70. - (IBAction)playPausePressed {
  71. [WKInterfaceController openParentApplication:@{@"name": @"playpause"} reply:^(NSDictionary *replyInfo, NSError *error) {
  72. NSLog(@"playpause %@",replyInfo);
  73. }];
  74. }
  75. - (IBAction)skipForward {
  76. [WKInterfaceController openParentApplication:@{@"name": @"skipForward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  77. NSLog(@"skipForward %@",replyInfo);
  78. }];
  79. }
  80. - (IBAction)skipBackward {
  81. [WKInterfaceController openParentApplication:@{@"name": @"skipBackward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  82. NSLog(@"skipBackward %@",replyInfo);
  83. }];
  84. }
  85. - (void)setTitleString:(NSString *)titleString {
  86. if (![_titleString isEqualToString:titleString] || (_titleString==nil && titleString)) {
  87. _titleString = [titleString copy];
  88. self.titleLabel.text = titleString;
  89. }
  90. }
  91. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  92. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  93. _playBackDurationNumber = playBackDurationNumber;
  94. self.durationLabel.text = [VLCTime timeWithNumber:playBackDurationNumber].stringValue;
  95. }
  96. }
  97. @end