VLCMediaListPlayer.m 4.3 KB

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