VLCMediaListPlayer.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*****************************************************************************
  2. * VLCMediaListPlayer.m: VLCKit.framework VLCMediaListPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2009 Pierre d'Herbemont
  5. * Partial Copyright (C) 2009-2013 Felix Paul Kühne
  6. * Copyright (C) 2009-2013 VLC authors and VideoLAN
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan.org
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCMediaListPlayer.h"
  27. #import "VLCMedia.h"
  28. #import "VLCMediaPlayer.h"
  29. #import "VLCMediaList.h"
  30. #import "VLCLibVLCBridging.h"
  31. #import "VLCLibrary.h"
  32. @interface VLCMediaListPlayer () {
  33. void *instance;
  34. VLCMedia *_rootMedia;
  35. VLCMediaPlayer *_mediaPlayer;
  36. VLCMediaList *_mediaList;
  37. VLCRepeatMode _repeatMode;
  38. }
  39. @end
  40. @implementation VLCMediaListPlayer
  41. - (instancetype)initWithOptions:(NSArray *)options andDrawable:(id)drawable
  42. {
  43. if (self = [super init]) {
  44. VLCLibrary *library;
  45. if (options != nil) {
  46. library = [[VLCLibrary alloc] initWithOptions:options];
  47. } else
  48. library = [VLCLibrary sharedLibrary];
  49. instance = libvlc_media_list_player_new([library instance]);
  50. _mediaPlayer = [[VLCMediaPlayer alloc] initWithLibVLCInstance:libvlc_media_list_player_get_media_player(instance) andLibrary:library];
  51. if (drawable != nil)
  52. [_mediaPlayer setDrawable:drawable];
  53. }
  54. return self;
  55. }
  56. - (instancetype)initWithOptions:(NSArray *)options
  57. {
  58. return [self initWithOptions:options andDrawable:nil];
  59. }
  60. - (instancetype)init
  61. {
  62. return [self initWithOptions:nil andDrawable:nil];
  63. }
  64. - (instancetype)initWithDrawable:(id)drawable
  65. {
  66. return [self initWithOptions:nil andDrawable:drawable];
  67. }
  68. - (void)dealloc
  69. {
  70. [_mediaPlayer stop];
  71. libvlc_media_list_player_release(instance);
  72. }
  73. - (VLCMediaPlayer *)mediaPlayer
  74. {
  75. return _mediaPlayer;
  76. }
  77. - (void)setMediaList:(VLCMediaList *)mediaList
  78. {
  79. if (_mediaList == mediaList)
  80. return;
  81. _mediaList = mediaList;
  82. libvlc_media_list_player_set_media_list(instance, [mediaList libVLCMediaList]);
  83. [self willChangeValueForKey:@"rootMedia"];
  84. _rootMedia = nil;
  85. [self didChangeValueForKey:@"rootMedia"];
  86. }
  87. - (VLCMediaList *)mediaList
  88. {
  89. return _mediaList;
  90. }
  91. - (void)setRootMedia:(VLCMedia *)media
  92. {
  93. if (_rootMedia == media)
  94. return;
  95. _rootMedia = nil;
  96. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  97. if (media)
  98. [mediaList addMedia:media];
  99. // This will clean rootMedia
  100. [self setMediaList:mediaList];
  101. // Thus set rootMedia here.
  102. _rootMedia = media;
  103. }
  104. - (VLCMedia *)rootMedia
  105. {
  106. return _rootMedia;
  107. }
  108. - (void)playMedia:(VLCMedia *)media
  109. {
  110. if ([NSThread isMainThread]) {
  111. [self performSelectorInBackground:@selector(playMedia:) withObject:media];
  112. return;
  113. }
  114. libvlc_media_list_player_play_item(instance, [media libVLCMediaDescriptor]);
  115. }
  116. - (void)play
  117. {
  118. if ([NSThread isMainThread]) {
  119. [self performSelectorInBackground:@selector(play) withObject:nil];
  120. return;
  121. }
  122. libvlc_media_list_player_play(instance);
  123. }
  124. - (void)pause
  125. {
  126. if ([NSThread isMainThread]) {
  127. [self performSelectorInBackground:@selector(pause) withObject:nil];
  128. return;
  129. }
  130. libvlc_media_list_player_pause(instance);
  131. }
  132. - (void)stop
  133. {
  134. if ([NSThread isMainThread]) {
  135. [self performSelectorInBackground:@selector(stop) withObject:nil];
  136. return;
  137. }
  138. libvlc_media_list_player_stop(instance);
  139. }
  140. - (BOOL)next
  141. {
  142. return libvlc_media_list_player_next(instance) == 0 ? YES : NO;
  143. }
  144. - (BOOL)previous
  145. {
  146. return libvlc_media_list_player_previous(instance) == 0 ? YES : NO;
  147. }
  148. - (BOOL)playItemAtIndex:(int)index
  149. {
  150. return libvlc_media_list_player_play_item_at_index(instance, index) == 0 ? YES : NO;
  151. }
  152. - (void)playItemAtNumber:(NSNumber *)index
  153. {
  154. if ([NSThread isMainThread]) {
  155. [self performSelectorInBackground:@selector(playItemAtNumber:) withObject:index];
  156. return;
  157. }
  158. libvlc_media_list_player_play_item_at_index(instance, [index intValue]);
  159. }
  160. - (void)setRepeatMode:(VLCRepeatMode)repeatMode
  161. {
  162. libvlc_playback_mode_t mode;
  163. switch (repeatMode) {
  164. case VLCRepeatAllItems:
  165. mode = libvlc_playback_mode_loop;
  166. break;
  167. case VLCDoNotRepeat:
  168. mode = libvlc_playback_mode_default;
  169. break;
  170. case VLCRepeatCurrentItem:
  171. mode = libvlc_playback_mode_repeat;
  172. break;
  173. default:
  174. NSAssert(0, @"Should not be reached");
  175. break;
  176. }
  177. libvlc_media_list_player_set_playback_mode(instance, mode);
  178. _repeatMode = repeatMode;
  179. }
  180. - (VLCRepeatMode)repeatMode
  181. {
  182. return _repeatMode;
  183. }
  184. @end