FSAudioController.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * This file is part of the FreeStreamer project,
  3. * (C)Copyright 2011-2018 Matias Muhonen <mmu@iki.fi> 穆马帝
  4. * See the file ''LICENSE'' for using the code.
  5. *
  6. * https://github.com/muhku/FreeStreamer
  7. */
  8. #import <Foundation/Foundation.h>
  9. #include "FSAudioStream.h"
  10. @class FSCheckContentTypeRequest;
  11. @class FSParsePlaylistRequest;
  12. @class FSParseRssPodcastFeedRequest;
  13. @class FSPlaylistItem;
  14. @protocol FSAudioControllerDelegate;
  15. /**
  16. * FSAudioController is functionally equivalent to FSAudioStream with
  17. * one addition: it can be directly fed with a playlist (PLS, M3U) URL
  18. * or an RSS podcast feed. It determines the content type and forms
  19. * a playlist for playback. Notice that this generates more traffic and
  20. * is generally more slower than using an FSAudioStream directly.
  21. *
  22. * It is also possible to construct a playlist by yourself by providing
  23. * the playlist items. In this case see the methods for managing the playlist.
  24. *
  25. * If you have a playlist with multiple items, FSAudioController attemps
  26. * automatically preload the next item in the playlist. This helps to
  27. * start the next item playback immediately without the need for the
  28. * user to wait for buffering.
  29. *
  30. * Notice that do not attempt to set your own blocks to the audio stream
  31. * owned by the controller. FSAudioController uses the blocks internally
  32. * and any user set blocks will be overwritten. Instead use the blocks
  33. * offered by FSAudioController.
  34. */
  35. @interface FSAudioController : NSObject {
  36. NSURL *_url;
  37. NSMutableArray *_streams;
  38. float _volume;
  39. BOOL _readyToPlay;
  40. FSCheckContentTypeRequest *_checkContentTypeRequest;
  41. FSParsePlaylistRequest *_parsePlaylistRequest;
  42. FSParseRssPodcastFeedRequest *_parseRssPodcastFeedRequest;
  43. void (^_onStateChangeBlock)(FSAudioStreamState);
  44. void (^_onMetaDataAvailableBlock)(NSDictionary*);
  45. void (^_onFailureBlock)(FSAudioStreamError error, NSString *errorDescription);
  46. }
  47. /**
  48. * Initializes the audio stream with an URL.
  49. *
  50. * @param url The URL from which the stream data is retrieved.
  51. */
  52. - (id)initWithUrl:(NSURL *)url;
  53. /**
  54. * Starts playing the stream. Before the playback starts,
  55. * the URL content type is checked and playlists resolved.
  56. */
  57. - (void)play;
  58. /**
  59. * Starts playing the stream from an URL. Before the playback starts,
  60. * the URL content type is checked and playlists resolved.
  61. *
  62. * @param url The URL from which the stream data is retrieved.
  63. */
  64. - (void)playFromURL:(NSURL *)url;
  65. /**
  66. * Starts playing the stream from the given playlist. Each item in the array
  67. * must an FSPlaylistItem.
  68. *
  69. * @param playlist The playlist items.
  70. */
  71. - (void)playFromPlaylist:(NSArray *)playlist;
  72. /**
  73. * Starts playing the stream from the given playlist. Each item in the array
  74. * must an FSPlaylistItem. The playback starts from the given index
  75. * in the playlist.
  76. *
  77. * @param playlist The playlist items.
  78. * @param index The playlist index where to start playback from.
  79. */
  80. - (void)playFromPlaylist:(NSArray *)playlist itemIndex:(NSUInteger)index;
  81. /**
  82. * Plays a playlist item at the specified index.
  83. *
  84. * @param index The playlist index where to start playback from.
  85. */
  86. - (void)playItemAtIndex:(NSUInteger)index;
  87. /**
  88. * Returns the count of playlist items.
  89. */
  90. - (NSUInteger)countOfItems;
  91. /**
  92. * Adds an item to the playlist.
  93. *
  94. * @param item The playlist item to be added.
  95. */
  96. - (void)addItem:(FSPlaylistItem *)item;
  97. /**
  98. * Adds an item to the playlist at a specific position.
  99. *
  100. * @param item The playlist item to be added.
  101. * @param index The location in the playlist to place the new item
  102. */
  103. - (void)insertItem:(FSPlaylistItem *)item atIndex:(NSInteger)index;
  104. /**
  105. * Moves an item already in the playlist to a different position in the playlist
  106. *
  107. * @param from The original index of the track to move
  108. * @param to The destination of the the track at the index specified in `from`
  109. */
  110. - (void)moveItemAtIndex:(NSUInteger)from toIndex:(NSUInteger)to;
  111. /**
  112. * Replaces a playlist item.
  113. *
  114. * @param index The index of the playlist item to be replaced.
  115. * @param item The playlist item used the replace the existing one.
  116. */
  117. - (void)replaceItemAtIndex:(NSUInteger)index withItem:(FSPlaylistItem *)item;
  118. /**
  119. * Removes a playlist item.
  120. *
  121. * @param index The index of the playlist item to be removed.
  122. */
  123. - (void)removeItemAtIndex:(NSUInteger)index;
  124. /**
  125. * Stops the stream playback.
  126. */
  127. - (void)stop;
  128. /**
  129. * If the stream is playing, the stream playback is paused upon calling pause.
  130. * Otherwise (the stream is paused), calling pause will continue the playback.
  131. */
  132. - (void)pause;
  133. /**
  134. * Returns the playback status: YES if the stream is playing, NO otherwise.
  135. */
  136. - (BOOL)isPlaying;
  137. /**
  138. * Returns if the current multiple-item playlist has next item
  139. */
  140. - (BOOL)hasNextItem;
  141. /**
  142. * Returns if the current multiple-item playlist has Previous item
  143. */
  144. - (BOOL)hasPreviousItem;
  145. /**
  146. * Play the next item of multiple-item playlist
  147. */
  148. - (void)playNextItem;
  149. /**
  150. * Play the previous item of multiple-item playlist
  151. */
  152. - (void)playPreviousItem;
  153. /**
  154. * This property holds the current playback volume of the stream,
  155. * from 0.0 to 1.0.
  156. *
  157. * Note that the overall volume is still constrained by the volume
  158. * set by the user! So the actual volume cannot be higher
  159. * than the volume currently set by the user. For example, if
  160. * requesting a volume of 0.5, then the volume will be 50%
  161. * lower than the current playback volume set by the user.
  162. */
  163. @property (nonatomic,assign) float volume;
  164. /**
  165. * The controller URL.
  166. */
  167. @property (nonatomic,assign) NSURL *url;
  168. /**
  169. * The the active playing stream, which may change
  170. * from time to time during the playback. In this way, do not
  171. * set your own blocks to the stream but use the blocks
  172. * provides by FSAudioController.
  173. */
  174. @property (readonly) FSAudioStream *activeStream;
  175. /**
  176. * The playlist item the controller is currently using.
  177. */
  178. @property (nonatomic,readonly) FSPlaylistItem *currentPlaylistItem;
  179. /**
  180. * This property determines if the next playlist item should be loaded
  181. * automatically. This is YES by default.
  182. */
  183. @property (nonatomic,assign) BOOL preloadNextPlaylistItemAutomatically;
  184. /**
  185. * This property determines if the debug output is enabled. Disabled
  186. * by default
  187. */
  188. @property (nonatomic,assign) BOOL enableDebugOutput;
  189. /**
  190. * This property determines if automatic audio session handling is enabled.
  191. * This is YES by default.
  192. */
  193. @property (nonatomic,assign) BOOL automaticAudioSessionHandlingEnabled;
  194. /**
  195. * This property holds the configuration used for the streaming.
  196. */
  197. @property (nonatomic,strong) FSStreamConfiguration *configuration;
  198. /**
  199. * Called upon a state change.
  200. */
  201. @property (copy) void (^onStateChange)(FSAudioStreamState state);
  202. /**
  203. * Called upon a meta data is available.
  204. */
  205. @property (copy) void (^onMetaDataAvailable)(NSDictionary *metadata);
  206. /**
  207. * Called upon a failure.
  208. */
  209. @property (copy) void (^onFailure)(FSAudioStreamError error, NSString *errorDescription);
  210. /**
  211. * Delegate.
  212. */
  213. @property (nonatomic,unsafe_unretained) IBOutlet id<FSAudioControllerDelegate> delegate;
  214. @end
  215. /**
  216. * To check the preloading status, use this delegate.
  217. */
  218. @protocol FSAudioControllerDelegate <NSObject>
  219. @optional
  220. /**
  221. * Called when the controller wants to start preloading an item. Return YES or NO
  222. * depending if you want this item to be preloaded.
  223. *
  224. * @param audioController The audio controller which is doing the preloading.
  225. * @param stream The stream which is wanted to be preloaded.
  226. */
  227. - (BOOL)audioController:(FSAudioController *)audioController allowPreloadingForStream:(FSAudioStream *)stream;
  228. /**
  229. * Called when the controller starts to preload an item.
  230. *
  231. * @param audioController The audio controller which is doing the preloading.
  232. * @param stream The stream which is preloaded.
  233. */
  234. - (void)audioController:(FSAudioController *)audioController preloadStartedForStream:(FSAudioStream *)stream;
  235. @end