VLCNowPlayingInterfaceController.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "VLCNotificationRelay.h"
  16. @interface VLCNowPlayingInterfaceController ()
  17. @property (nonatomic, copy) NSString *titleString;
  18. @property (nonatomic, copy) NSNumber *playBackDurationNumber;
  19. @end
  20. @implementation VLCNowPlayingInterfaceController
  21. - (instancetype)init
  22. {
  23. self = [super init];
  24. if (self) {
  25. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  26. }
  27. return self;
  28. }
  29. - (void)awakeWithContext:(id)context {
  30. [super awakeWithContext:context];
  31. // Configure interface objects here.
  32. [self requestNowPlayingInfo];
  33. [[VLCNotificationRelay sharedRelay] addRelayRemoteName:@"org.videolan.ios-app.nowPlayingInfoUpdate" toLocalName:@"nowPlayingInfoUpdate"];
  34. }
  35. - (void)willActivate {
  36. // This method is called when watch view controller is about to be visible to user
  37. [super willActivate];
  38. [self setTitle:NSLocalizedString(@"PLAYING", nil)];
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNowPlayingInfo) name:@"nowPlayingInfoUpdate" object:nil];
  40. [self requestNowPlayingInfo];
  41. }
  42. - (void)didDeactivate {
  43. // This method is called when watch view controller is no longer visible
  44. [super didDeactivate];
  45. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"nowPlayingInfoUpdate" object:nil];
  46. }
  47. - (void)requestNowPlayingInfo {
  48. [WKInterfaceController openParentApplication:@{@"name": @"getNowPlayingInfo"} reply:^(NSDictionary *replyInfo, NSError *error) {
  49. [self updateWithNowPlayingInfo:replyInfo];
  50. NSLog(@"nowplayingInfo: %@",replyInfo);
  51. }];
  52. }
  53. - (void)updateWithNowPlayingInfo:(NSDictionary*)nowPlayingInfo {
  54. self.titleString = nowPlayingInfo[MPMediaItemPropertyTitle];
  55. self.playBackDurationNumber = nowPlayingInfo[MPMediaItemPropertyPlaybackDuration];
  56. }
  57. - (IBAction)playPausePressed {
  58. [WKInterfaceController openParentApplication:@{@"name": @"playpause"} reply:^(NSDictionary *replyInfo, NSError *error) {
  59. NSLog(@"playpause %@",replyInfo);
  60. }];
  61. }
  62. - (IBAction)skipForward {
  63. [WKInterfaceController openParentApplication:@{@"name": @"skipForward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  64. NSLog(@"skipForward %@",replyInfo);
  65. }];
  66. }
  67. - (IBAction)skipBackward {
  68. [WKInterfaceController openParentApplication:@{@"name": @"skipBackward"} reply:^(NSDictionary *replyInfo, NSError *error) {
  69. NSLog(@"skipBackward %@",replyInfo);
  70. }];
  71. }
  72. - (void)setTitleString:(NSString *)titleString {
  73. if (![_titleString isEqualToString:titleString] || (_titleString==nil && titleString)) {
  74. _titleString = [titleString copy];
  75. self.titleLabel.text = titleString;
  76. }
  77. }
  78. - (void)setPlayBackDurationNumber:(NSNumber *)playBackDurationNumber {
  79. if (![_playBackDurationNumber isEqualToNumber:playBackDurationNumber] || (_playBackDurationNumber==nil && playBackDurationNumber)) {
  80. _playBackDurationNumber = playBackDurationNumber;
  81. float duratioFloat = playBackDurationNumber.floatValue;
  82. NSNumber *durationNumber = nil;
  83. if (duratioFloat>0.0) {
  84. durationNumber = @(playBackDurationNumber.floatValue*1000);
  85. }
  86. self.durationLabel.text = [VLCTime timeWithNumber:durationNumber].stringValue;
  87. }
  88. }
  89. @end