Controller.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*****************************************************************************
  2. * test: Controller.m
  3. *****************************************************************************
  4. * Copyright (C) 2007-2013 Pierre d'Herbemont and VideoLAN
  5. *
  6. * Authors: Pierre d'Herbemont
  7. * Felix Paul Kühne
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22. *****************************************************************************/
  23. #import "Controller.h"
  24. @implementation Controller
  25. - (void)awakeFromNib
  26. {
  27. [NSApp setDelegate:self];
  28. // Allocate a VLCVideoView instance and tell it what area to occupy.
  29. NSRect rect = NSMakeRect(0, 0, 0, 0);
  30. rect.size = [videoHolderView frame].size;
  31. videoView = [[VLCVideoView alloc] initWithFrame:rect];
  32. [videoHolderView addSubview:videoView];
  33. [videoView setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable];
  34. videoView.fillScreen = YES;
  35. [VLCLibrary sharedLibrary];
  36. playlist = [[VLCMediaList alloc] init];
  37. [playlist addObserver:self forKeyPath:@"media" options:NSKeyValueObservingOptionNew context:nil];
  38. player = [[VLCMediaPlayer alloc] initWithVideoView:videoView];
  39. player.delegate = self;
  40. mediaIndex = -1;
  41. [playlistOutline registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSURLPboardType, nil]];
  42. [playlistOutline setDoubleAction:@selector(changeAndPlay:)];
  43. }
  44. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  45. {
  46. }
  47. - (void)applicationWillTerminate:(NSNotification *)aNotification
  48. {
  49. [playlist removeObserver:self forKeyPath:@"media"];
  50. [player pause];
  51. [player setMedia:nil];
  52. [player release];
  53. [playlist release];
  54. [videoView release];
  55. }
  56. - (void)changeAndPlay:(id)sender
  57. {
  58. if ([playlistOutline selectedRow] != mediaIndex) {
  59. [self setMediaIndex:[playlistOutline selectedRow]];
  60. if (![player isPlaying])
  61. [player play];
  62. }
  63. }
  64. - (void)setMediaIndex:(int)value
  65. {
  66. if ([playlist count] <= 0)
  67. return;
  68. if (value < 0)
  69. value = 0;
  70. if (value > [playlist count] - 1)
  71. value = [playlist count] - 1;
  72. mediaIndex = value;
  73. [player setMedia:[playlist mediaAtIndex:mediaIndex]];
  74. }
  75. - (void)play:(id)sender
  76. {
  77. [self setMediaIndex:mediaIndex+1];
  78. if (![player isPlaying] && [playlist count] > 0) {
  79. NSLog(@"%@ length = %@", [playlist mediaAtIndex:mediaIndex], [[playlist mediaAtIndex:mediaIndex] lengthWaitUntilDate:[NSDate dateWithTimeIntervalSinceNow:60]]);
  80. [player play];
  81. }
  82. }
  83. - (void)pause:(id)sender
  84. {
  85. NSLog(@"Sending pause message to media player...");
  86. [player pause];
  87. }
  88. - (void)mediaPlayerStateChanged:(NSNotification *)aNotification
  89. {
  90. if (player.media) {
  91. NSArray *spuTracks = [player videoSubTitlesNames];
  92. NSArray *spuTrackIndexes = [player videoSubTitlesIndexes];
  93. NSUInteger count = [spuTracks count];
  94. [spuPopup removeAllItems];
  95. if (count <= 1)
  96. return;
  97. for (NSUInteger x = 0; x < count; x++) {
  98. [spuPopup addItemWithTitle:spuTracks[x]];
  99. [[spuPopup lastItem] setTag:spuTrackIndexes[x]];
  100. }
  101. }
  102. }
  103. - (void)setSPU:(id)sender
  104. {
  105. if (player.media)
  106. player.currentVideoSubTitleIndex = [[spuPopup selectedItem] tag];
  107. }
  108. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  109. {
  110. if ([keyPath isEqualToString:@"media"] && object == playlist)
  111. [playlistOutline reloadData];
  112. }
  113. // NSTableView Implementation
  114. - (int)numberOfRowsInTableView:(NSTableView *)tableView
  115. {
  116. return [playlist count];
  117. }
  118. - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
  119. row:(int)row
  120. {
  121. NSString *title = [(VLCMedia *)[playlist mediaAtIndex:row].metaDictionary valueForKey:VLCMetaInformationTitle];
  122. return title ? title : [playlist mediaAtIndex:row].url.lastPathComponent;
  123. }
  124. - (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info
  125. proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op
  126. {
  127. return NSDragOperationEvery; /* This is for now */
  128. }
  129. - (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
  130. row:(int)row dropOperation:(NSTableViewDropOperation)operation
  131. {
  132. NSArray *droppedItems = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
  133. for (int i = 0; i < [droppedItems count]; i++) {
  134. NSString * filename = [droppedItems objectAtIndex:i];
  135. VLCMedia * media = [VLCMedia mediaWithPath:filename];
  136. [playlist addMedia:media];
  137. }
  138. return YES;
  139. }
  140. @end