NIMMediaManagerProtocol.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // NIMMediaManagerProtocol.h
  3. // NIMLib
  4. //
  5. // Created by Netease.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. /**
  11. * 音频输出设备类型
  12. */
  13. typedef NS_ENUM(NSInteger, NIMAudioOutputDevice){
  14. /**
  15. * 听筒
  16. */
  17. NIMAudioOutputDeviceReceiver,
  18. /**
  19. * 扬声器
  20. */
  21. NIMAudioOutputDeviceSpeaker
  22. };
  23. /**
  24. * 云信支持的音频类型
  25. */
  26. typedef NS_ENUM(NSInteger, NIMAudioType) {
  27. /**
  28. * aac
  29. */
  30. NIMAudioTypeAAC,
  31. /**
  32. * amr
  33. */
  34. NIMAudioTypeAMR
  35. };
  36. /**
  37. * 语音转文字
  38. *
  39. * @param error 执行结果,如果成功error为nil
  40. * @prarm text 转换后的文本
  41. */
  42. typedef void(^NIMAudioToTextBlock)(NSError * __nullable error,NSString * __nullable text);
  43. /**
  44. * 语音转文字选项
  45. */
  46. @interface NIMAudioToTextOption : NSObject
  47. /**
  48. * 音频URL
  49. * @discussion 目前只支持云信服务器的URL,不支持外链
  50. */
  51. @property (nonatomic,copy) NSString *url;
  52. /**
  53. * 音频本地地址
  54. * @discussion APP需要保证音频已经下载到本地
  55. */
  56. @property (nonatomic,copy) NSString *filepath;
  57. @end
  58. /**
  59. * 多媒体委托
  60. */
  61. @protocol NIMMediaManagerDelegate <NSObject>
  62. @optional
  63. /**
  64. * 开始播放音频的回调
  65. *
  66. * @param filePath 音频文件路径
  67. * @param error 错误信息
  68. */
  69. - (void)playAudio:(NSString *)filePath didBeganWithError:(nullable NSError *)error;
  70. /**
  71. * 播放完音频的回调
  72. *
  73. * @param filePath 音频文件路径
  74. * @param error 错误信息
  75. */
  76. - (void)playAudio:(NSString *)filePath didCompletedWithError:(nullable NSError *)error;
  77. /**
  78. * 播放完音频的进度回调
  79. *
  80. * @param filePath 音频文件路径
  81. * @param value 播放进度 0.0 - 1.0
  82. */
  83. - (void)playAudio:(NSString *)filePath progress:(float)value;
  84. /**
  85. * 停止播放音频的回调
  86. *
  87. * @param filePath 音频文件路径
  88. * @param error 错误信息
  89. */
  90. - (void)stopPlayAudio:(NSString *)filePath didCompletedWithError:(nullable NSError *)error;
  91. /**
  92. * 播放音频开始被打断回调
  93. */
  94. - (void)playAudioInterruptionBegin;
  95. /**
  96. * 播放音频结束被打断回调
  97. */
  98. - (void)playAudioInterruptionEnd;
  99. /**
  100. * 开始录制音频的回调
  101. *
  102. * @param filePath 录制的音频的文件路径
  103. * @param error 错误信息
  104. * @discussion 如果录音失败,filePath 有可能为 nil
  105. */
  106. - (void)recordAudio:(nullable NSString *)filePath didBeganWithError:(nullable NSError *)error;
  107. /**
  108. * 录制音频完成后的回调
  109. *
  110. * @param filePath 录制完成的音频文件路径
  111. * @param error 错误信息
  112. */
  113. - (void)recordAudio:(nullable NSString *)filePath didCompletedWithError:(nullable NSError *)error;
  114. /**
  115. * 录音被取消的回调
  116. */
  117. - (void)recordAudioDidCancelled;
  118. /**
  119. * 音频录制进度更新回调
  120. *
  121. * @param currentTime 当前录制的时间
  122. */
  123. - (void)recordAudioProgress:(NSTimeInterval)currentTime;
  124. /**
  125. * 录音开始被打断回调
  126. */
  127. - (void)recordAudioInterruptionBegin;
  128. /**
  129. * 录音结束被打断回调
  130. */
  131. - (void)recordAudioInterruptionEnd;
  132. @end
  133. /**
  134. * 多媒体控制协议
  135. */
  136. @protocol NIMMediaManager <NSObject>
  137. @required
  138. /**
  139. * 录音进度更新间隔
  140. * @discussion 如果值大于0,则会按照相应间隔调用recordAudioProgress:回调,默认值为0.3
  141. */
  142. @property (nonatomic, assign) NSTimeInterval recordProgressUpdateTimeInterval;
  143. #pragma mark - play audio
  144. /**
  145. * 切换音频输出设备
  146. *
  147. * @param outputDevice 音频输出设备
  148. *
  149. * @return 是否切换成功
  150. */
  151. - (BOOL)switchAudioOutputDevice:(NIMAudioOutputDevice)outputDevice;
  152. /**
  153. * 在播放声音的时候,如果手机贴近耳朵,是否需要自动切换成听筒播放
  154. *
  155. * @param needProximityMonitor 是否需要贴耳传感器监听
  156. */
  157. - (void)setNeedProximityMonitor:(BOOL)needProximityMonitor;
  158. /**
  159. * 是否正在播放音频
  160. *
  161. */
  162. - (BOOL)isPlaying;
  163. /**
  164. * 播放音频文件
  165. *
  166. * @discussion 开始播放,NIMMediaManagerDelegate中的playAudio:didBeganWithError:回调会被触发,播放完成后, NIMMediaManagerDelegate中的playAudio:didCompletedWithError:回调会被触发
  167. * @param filepath 音频文件路径
  168. */
  169. - (void)play:(NSString *)filepath;
  170. /**
  171. * 停止播放音频
  172. *
  173. * @discussion 音频播放完成后NIMMediaManagerDelegate中的playAudio:didCompletedWithError:回调会被触发
  174. */
  175. - (void)stopPlay;
  176. /**
  177. * 设置播放音频的起始时间
  178. * @param timestamp 起始时间
  179. * @discussion 起始时间不能大于整个音频的时间,否则播放无效。调用此方法后,不需要再调用 play: 方法,自动播放
  180. */
  181. - (BOOL)seek:(NSTimeInterval)timestamp;
  182. #pragma mark - record audio
  183. /**
  184. * 是否正在录音
  185. *
  186. */
  187. - (BOOL)isRecording;
  188. /**
  189. * 开始录制音频
  190. *
  191. * @param duration 最长录音时间
  192. * @discussion 开始录音,NIMMediaManagerDelegate中的recordAudio:didBeganWithError:回调会被触发,录音完成后, NIMMediaManagerDelgate中的recordAudio:didCompletedWithError:回调会被触发
  193. * 默认使用 aac 编码格式
  194. */
  195. - (void)recordForDuration:(NSTimeInterval)duration;
  196. /**
  197. * 开始录音
  198. *
  199. * @param type 音频类型
  200. * @param duration 最大时长
  201. * @discussion 开始录音,NIMMediaManagerDelegate中的recordAudio:didBeganWithError:回调会被触发,录音完成后, NIMMediaManagerDelegate中的recordAudio:didCompletedWithError:回调会被触发
  202. */
  203. - (void)record:(NIMAudioType)type
  204. duration:(NSTimeInterval)duration;
  205. /**
  206. * 停止录制音频
  207. *
  208. * @discussion 停止录音后NIMMediaManagerDelegate中的recordAudio:didCompletedWithError:回调会被触发
  209. */
  210. - (void)stopRecord;
  211. /**
  212. * 取消录制音频
  213. *
  214. * @discussion 录音取消后,NIMMediaManagerDelegate中的recordAudioDidCancelled回调会被触发
  215. */
  216. - (void)cancelRecord;
  217. /**
  218. * 获取录音分贝
  219. *
  220. */
  221. - (float)recordPeakPower;
  222. /**
  223. * 获取录音分贝
  224. *
  225. */
  226. - (float)recordAveragePower;
  227. /**
  228. * 语音转文字
  229. *
  230. * @param option 语音转文字选项
  231. * @param result 完成回调
  232. */
  233. - (void)transAudioToText:(NIMAudioToTextOption *)option
  234. result:(NIMAudioToTextBlock)result;
  235. #pragma mark - common setting
  236. /**
  237. * 禁止在IM 录制、播放音频时设置AVAudioSession, 防止影响其他音视频效果
  238. * @param disabled YES则禁止重置
  239. */
  240. - (void)disableResetAudioSession:(BOOL)disabled;
  241. /**
  242. * 设置录制或者播放完成以后是否自动deactivate AVAudioSession
  243. *
  244. * @param deactivate 是否deactivate,默认为YES
  245. */
  246. - (void)setDeactivateAudioSessionAfterComplete:(BOOL)deactivate;
  247. #pragma mark - delegates
  248. /**
  249. * 添加多媒体委托
  250. *
  251. * @param delegate 多媒体委托
  252. */
  253. - (void)addDelegate:(id<NIMMediaManagerDelegate>)delegate;
  254. /**
  255. * 移除多媒体委托
  256. *
  257. * @param delegate 多媒体委托
  258. */
  259. - (void)removeDelegate:(id<NIMMediaManagerDelegate>)delegate;
  260. @end
  261. NS_ASSUME_NONNULL_END