VLCMediaListPlayer.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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
  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. }
  52. return self;
  53. }
  54. - (instancetype)init
  55. {
  56. return [self initWithOptions:nil];
  57. }
  58. - (void)dealloc
  59. {
  60. [_mediaPlayer stop];
  61. libvlc_media_list_player_release(instance);
  62. }
  63. - (VLCMediaPlayer *)mediaPlayer
  64. {
  65. return _mediaPlayer;
  66. }
  67. - (void)setMediaList:(VLCMediaList *)mediaList
  68. {
  69. if (_mediaList == mediaList)
  70. return;
  71. _mediaList = mediaList;
  72. libvlc_media_list_player_set_media_list(instance, [mediaList libVLCMediaList]);
  73. [self willChangeValueForKey:@"rootMedia"];
  74. _rootMedia = nil;
  75. [self didChangeValueForKey:@"rootMedia"];
  76. }
  77. - (VLCMediaList *)mediaList
  78. {
  79. return _mediaList;
  80. }
  81. - (void)setRootMedia:(VLCMedia *)media
  82. {
  83. if (_rootMedia == media)
  84. return;
  85. _rootMedia = nil;
  86. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  87. if (media)
  88. [mediaList addMedia:media];
  89. // This will clean rootMedia
  90. [self setMediaList:mediaList];
  91. // Thus set rootMedia here.
  92. _rootMedia = media;
  93. }
  94. - (VLCMedia *)rootMedia
  95. {
  96. return _rootMedia;
  97. }
  98. - (void)playMedia:(VLCMedia *)media
  99. {
  100. libvlc_media_list_player_play_item(instance, [media libVLCMediaDescriptor]);
  101. }
  102. - (void)play
  103. {
  104. libvlc_media_list_player_play(instance);
  105. }
  106. - (void)pause
  107. {
  108. libvlc_media_list_player_pause(instance);
  109. }
  110. - (void)stop
  111. {
  112. libvlc_media_list_player_stop(instance);
  113. }
  114. - (BOOL)next
  115. {
  116. return libvlc_media_list_player_next(instance) == 0 ? YES : NO;
  117. }
  118. - (BOOL)previous
  119. {
  120. return libvlc_media_list_player_previous(instance) == 0 ? YES : NO;
  121. }
  122. - (BOOL)playItemAtIndex:(int)index
  123. {
  124. return libvlc_media_list_player_play_item_at_index(instance, index) == 0 ? YES : NO;
  125. }
  126. - (void)setRepeatMode:(VLCRepeatMode)repeatMode
  127. {
  128. libvlc_playback_mode_t mode;
  129. switch (repeatMode) {
  130. case VLCRepeatAllItems:
  131. mode = libvlc_playback_mode_loop;
  132. break;
  133. case VLCDoNotRepeat:
  134. mode = libvlc_playback_mode_default;
  135. break;
  136. case VLCRepeatCurrentItem:
  137. mode = libvlc_playback_mode_repeat;
  138. break;
  139. default:
  140. NSAssert(0, @"Should not be reached");
  141. break;
  142. }
  143. libvlc_media_list_player_set_playback_mode(instance, mode);
  144. _repeatMode = repeatMode;
  145. }
  146. - (VLCRepeatMode)repeatMode
  147. {
  148. return _repeatMode;
  149. }
  150. @end