KSYBgmPlayer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // KSYBgmPlayer.h
  3. // KSYStreamer
  4. //
  5. //
  6. // Created by pengbin on 10/15/15.
  7. // Copyright © 2015 ksyun. All rights reserved.
  8. //
  9. #import <AVFoundation/AVFoundation.h>
  10. #import "KSYTypeDef.h"
  11. @class KSYAudioMixer;
  12. /** 背景音乐播放器
  13. 提供背景音乐播放的功能, 并能将被播放的音频数据通过回调送出
  14. */
  15. @interface KSYBgmPlayer : NSObject
  16. #pragma mark - player control
  17. /**
  18. @abstract 开始播放背景音乐
  19. @param path 本地音乐的路径
  20. @param loop 是否循环播放此音乐
  21. @return 是否能够开始播放
  22. */
  23. - (BOOL) startPlayBgm:(NSString*) path
  24. isLoop:(BOOL) loop;
  25. /**
  26. @abstract 停止播放背景音乐
  27. */
  28. - (void) stopPlayBgm;
  29. /**
  30. @abstract 停止播放背景音乐
  31. @param 停止完成的回调函数
  32. */
  33. - (void) stopPlayBgm: (void (^)())completion;
  34. /**
  35. @abstract 暂停播放背景音乐
  36. */
  37. - (void) pauseBgm;
  38. /**
  39. @abstract 恢复播放背景音乐
  40. */
  41. - (void) resumeBgm;
  42. /**
  43. @abstract seek到指定时间 (拖动进度条)
  44. @param time 时间, 请参考 bgmDuration (单位,秒)
  45. @return 是否seek 成功
  46. @discussion 从v3.1.0版本起,该方法返回值均为YES,推荐使用– seekToTime:onComplete:方法获取真正的返回结果
  47. */
  48. - (BOOL) seekToTime:(float)time;
  49. /**
  50. @abstract seek到指定时间 (拖动进度条)
  51. @param time 时间, 请参考 bgmDuration (单位,秒)
  52. @return 是否seek 成功
  53. */
  54. - (void) seekToTime:(float)time onComplete:(void (^)(BOOL))completion;
  55. /**
  56. @abstract seek到指定进度 (拖动进度条)
  57. @param prog 进度, 请参考 bgmProgress
  58. @return 是否seek 成功
  59. @discussion 从v3.1.0版本起,该方法返回值均为YES,推荐使用– seekToProgress:onComplete:方法获取真正的返回结果
  60. */
  61. - (BOOL) seekToProgress:(float)prog;
  62. /**
  63. @abstract seek到指定进度 (拖动进度条)
  64. @param prog 进度, 请参考 bgmProgress
  65. @return 是否seek 成功
  66. */
  67. - (void) seekToProgress:(float)prog onComplete:(void (^)(BOOL))completion;
  68. /**
  69. @abstract 背景音乐的音量
  70. @discussion 调整范围 0.0~1.0
  71. @discussion 仅仅调整播放的音量, 不影响回调的音频数据
  72. */
  73. @property (nonatomic, assign) double bgmVolume;
  74. /**
  75. @abstract 播放声音的音调
  76. @discussion 调整范围 [-24.0 ~ 24.0], 默认为0.01, 单位为半音
  77. @discussion 0.01 为1度, 1.0为一个半音, 12个半音为1个八度
  78. */
  79. @property (nonatomic, assign) double bgmPitch;
  80. /**
  81. @abstract 播放速率
  82. @discussion 调整范围 0.5 ~ 2.0,默认值 1.0
  83. */
  84. @property (nonatomic, assign) double playRate;
  85. /**
  86. @abstract 背景音乐播放静音
  87. @discussion 仅仅静音播放, 不影响回调的音频数据
  88. */
  89. @property (nonatomic, assign) BOOL bMuteBgmPlay;
  90. /**
  91. @abstract 调用startPlayBgm后是否立刻启动播放,默认为YES
  92. @discussion 如果设置NO,则播放准备工作完成后进入pause状态,启动播放需调用resumeBgm方法
  93. */
  94. @property (nonatomic, assign) BOOL bShouldAutoPlay;
  95. #pragma mark - callbacks
  96. /**
  97. @abstract 设置 audioDataBlock 是否返回原始数据
  98. @discussion 默认为NO,audioDataBlock 返回的数据为经过变速、变调后的数据
  99. 如果为YES,audioDataBlock 返回的数据为原始数据
  100. */
  101. @property(nonatomic, assign) BOOL callBackRawData;
  102. /**
  103. @abstract 音频数据输出回调
  104. @discussion sampleBuffer 从音乐文件中解码得到的PCM数据
  105. */
  106. @property(nonatomic, copy) BOOL (^audioDataBlock)(uint8_t** pData, int len, const AudioStreamBasicDescription* fmt, CMTime pts);
  107. /**
  108. @abstract 当背景音乐播放完成时,调用此回调函数
  109. @discussion 只有设置 loop为NO时才有效, 在开始播放前设置有效
  110. */
  111. @property(nonatomic, copy) void(^bgmFinishBlock)(void);
  112. #pragma mark - player state
  113. /**
  114. @abstract 背景音的duration信息(总时长, 单位:秒)
  115. */
  116. @property (nonatomic, readonly) float bgmDuration;
  117. /**
  118. @abstract 背景音的已经播放长度 (单位:秒)
  119. @discussion 从0开始,最大为bgmDuration长度
  120. */
  121. @property (nonatomic, readonly) float bgmPlayTime;
  122. /**
  123. @abstract 音频的播放进度
  124. @discussion 取值从0.0~1.0,大小为bgmPlayTime/bgmDuration;
  125. */
  126. @property (nonatomic, readonly) float bgmProcess;
  127. /**
  128. @abstract 音频播放是否运行
  129. @discussion 音频是否输出到speaker播放
  130. */
  131. @property (nonatomic, readonly) BOOL isRunning;
  132. /**
  133. @abstract  播放错误码
  134. @discussion 播放错误码具体内容可以参考AudioQueue的Apple文档。
  135. */
  136. @property (nonatomic, readonly) OSStatus audioErrorCode;
  137. /**
  138. @abstract  播放状态
  139. */
  140. @property (nonatomic, readonly) KSYBgmPlayerState bgmPlayerState;
  141. /**
  142. @abstract  单曲循环
  143. */
  144. @property (nonatomic, assign) BOOL bLoop;
  145. /**
  146. @abstract 获取状态对应的字符串
  147. @param stat 状态
  148. */
  149. - (NSString*) getBgmStateName : (KSYBgmPlayerState) stat;
  150. /**
  151. @abstract 获取当前状态对应的字符串
  152. */
  153. - (NSString*) getCurBgmStateName;
  154. // Posted when audio state changes
  155. FOUNDATION_EXPORT NSString *const KSYAudioStateDidChangeNotification NS_AVAILABLE_IOS(7_0);
  156. @end