NIMMediaManagerProtocol.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. - (void)playAudioInterruptionBegin;
  81. /**
  82. * 播放音频结束被打断回调
  83. */
  84. - (void)playAudioInterruptionEnd;
  85. /**
  86. * 开始录制音频的回调
  87. *
  88. * @param filePath 录制的音频的文件路径
  89. * @param error 错误信息
  90. * @discussion 如果录音失败,filePath 有可能为 nil
  91. */
  92. - (void)recordAudio:(nullable NSString *)filePath didBeganWithError:(nullable NSError *)error;
  93. /**
  94. * 录制音频完成后的回调
  95. *
  96. * @param filePath 录制完成的音频文件路径
  97. * @param error 错误信息
  98. */
  99. - (void)recordAudio:(nullable NSString *)filePath didCompletedWithError:(nullable NSError *)error;
  100. /**
  101. * 录音被取消的回调
  102. */
  103. - (void)recordAudioDidCancelled;
  104. /**
  105. * 音频录制进度更新回调
  106. *
  107. * @param currentTime 当前录制的时间
  108. */
  109. - (void)recordAudioProgress:(NSTimeInterval)currentTime;
  110. /**
  111. * 录音开始被打断回调
  112. */
  113. - (void)recordAudioInterruptionBegin;
  114. /**
  115. * 录音结束被打断回调
  116. */
  117. - (void)recordAudioInterruptionEnd;
  118. @end
  119. /**
  120. * 多媒体控制协议
  121. */
  122. @protocol NIMMediaManager <NSObject>
  123. @required
  124. /**
  125. * 录音进度更新间隔
  126. * @discussion 如果值大于0,则会按照相应间隔调用recordAudioProgress:回调,默认值为0.3
  127. */
  128. @property (nonatomic, assign) NSTimeInterval recordProgressUpdateTimeInterval;
  129. #pragma mark - play audio
  130. /**
  131. * 切换音频输出设备
  132. *
  133. * @param outputDevice 音频输出设备
  134. *
  135. * @return 是否切换成功
  136. */
  137. - (BOOL)switchAudioOutputDevice:(NIMAudioOutputDevice)outputDevice;
  138. /**
  139. * 在播放声音的时候,如果手机贴近耳朵,是否需要自动切换成听筒播放
  140. *
  141. * @param needProximityMonitor 是否需要贴耳传感器监听
  142. */
  143. - (void)setNeedProximityMonitor:(BOOL)needProximityMonitor;
  144. /**
  145. * 是否正在播放音频
  146. *
  147. */
  148. - (BOOL)isPlaying;
  149. /**
  150. * 播放音频文件
  151. *
  152. * @discussion 开始播放,NIMMediaManagerDelegate中的playAudio:didBeganWithError:回调会被触发,播放完成后, NIMMediaManagerDelegate中的playAudio:didCompletedWithError:回调会被触发
  153. * @param filepath 音频文件路径
  154. */
  155. - (void)play:(NSString *)filepath;
  156. /**
  157. * 停止播放音频
  158. *
  159. * @discussion 音频播放完成后NIMMediaManagerDelegate中的playAudio:didCompletedWithError:回调会被触发
  160. */
  161. - (void)stopPlay;
  162. /**
  163. * 设置播放音频的起始时间
  164. * @param timestamp 起始时间
  165. * @discussion 起始时间不能大于整个音频的时间,否则播放无效。调用此方法后,不需要再调用 play: 方法,自动播放
  166. */
  167. - (BOOL)seek:(NSTimeInterval)timestamp;
  168. #pragma mark - record audio
  169. /**
  170. * 是否正在录音
  171. *
  172. */
  173. - (BOOL)isRecording;
  174. /**
  175. * 开始录制音频
  176. *
  177. * @param duration 最长录音时间
  178. * @discussion 开始录音,NIMMediaManagerDelegate中的recordAudio:didBeganWithError:回调会被触发,录音完成后, NIMMediaManagerDelgate中的recordAudio:didCompletedWithError:回调会被触发
  179. * 默认使用 aac 编码格式
  180. */
  181. - (void)recordForDuration:(NSTimeInterval)duration;
  182. /**
  183. * 开始录音
  184. *
  185. * @param type 音频类型
  186. * @param duration 最大时长
  187. * @discussion 开始录音,NIMMediaManagerDelegate中的recordAudio:didBeganWithError:回调会被触发,录音完成后, NIMMediaManagerDelegate中的recordAudio:didCompletedWithError:回调会被触发
  188. */
  189. - (void)record:(NIMAudioType)type
  190. duration:(NSTimeInterval)duration;
  191. /**
  192. * 停止录制音频
  193. *
  194. * @discussion 停止录音后NIMMediaManagerDelegate中的recordAudio:didCompletedWithError:回调会被触发
  195. */
  196. - (void)stopRecord;
  197. /**
  198. * 取消录制音频
  199. *
  200. * @discussion 录音取消后,NIMMediaManagerDelegate中的recordAudioDidCancelled回调会被触发
  201. */
  202. - (void)cancelRecord;
  203. /**
  204. * 获取录音分贝
  205. *
  206. */
  207. - (float)recordPeakPower;
  208. /**
  209. * 获取录音分贝
  210. *
  211. */
  212. - (float)recordAveragePower;
  213. /**
  214. * 语音转文字
  215. *
  216. * @param option 语音转文字选项
  217. * @param result 完成回调
  218. */
  219. - (void)transAudioToText:(NIMAudioToTextOption *)option
  220. result:(NIMAudioToTextBlock)result;
  221. #pragma mark - common setting
  222. /**
  223. * 禁止在IM 录制、播放音频时设置AVAudioSession, 防止影响其他音视频效果
  224. * @param disabled YES则禁止重置
  225. */
  226. - (void)disableResetAudioSession:(BOOL)disabled;
  227. /**
  228. * 设置录制或者播放完成以后是否自动deactivate AVAudioSession
  229. *
  230. * @param deactivate 是否deactivate,默认为YES
  231. */
  232. - (void)setDeactivateAudioSessionAfterComplete:(BOOL)deactivate;
  233. #pragma mark - delegates
  234. /**
  235. * 添加多媒体委托
  236. *
  237. * @param delegate 多媒体委托
  238. */
  239. - (void)addDelegate:(id<NIMMediaManagerDelegate>)delegate;
  240. /**
  241. * 移除多媒体委托
  242. *
  243. * @param delegate 多媒体委托
  244. */
  245. - (void)removeDelegate:(id<NIMMediaManagerDelegate>)delegate;
  246. @end
  247. NS_ASSUME_NONNULL_END