KSYBgmReader.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /** 背景音乐文件解码
  12. 提供背景音乐文件解码的功能,将解码后的音频数据通过回调送出
  13. 输出数据的格式满足如下规律:
  14. * 一定是S16的线性PCM
  15. * 如果是双通道,一定是交织的数据
  16. */
  17. @interface KSYBgmReader : NSObject
  18. /**
  19. @abstract 构造音乐文件对应的reader
  20. @param path 本地音乐的路径
  21. @param loop 是否循环播放此音乐
  22. @return self
  23. */
  24. - (id) initWithFile:(NSString*) path
  25. isLoop:(BOOL) loop;
  26. /**
  27. @abstract reload 一个新的文件到reader中
  28. @param path 本地音乐的路径
  29. @return path对应的文件是否有效
  30. */
  31. - (BOOL) reloadFile:(NSString*) path;
  32. /**
  33. @abstract 关闭文件
  34. */
  35. - (void) closeFile;
  36. /**
  37. @abstract seek到指定时间 (拖动进度条)
  38. @param time 时间, 请参考 bgmDuration (单位,秒)
  39. @return 是否seek 成功
  40. */
  41. - (BOOL) seekTo:(float)time;
  42. #pragma mark - audio data output
  43. /**
  44. @abstract 音乐的格式信息 (一定为S16的数据格式)
  45. */
  46. @property (nonatomic, readonly) AudioStreamBasicDescription bgmFmt;
  47. /**
  48. @abstract 从文件中读取一段音频数据 (二选一)
  49. @param buf, 待填充的音频数据 请保证buf.mDataByteSize 先设置为需要读取的字节数
  50. @return -1: 解码遇到错误, 请检查音乐文件是否有效
  51. 0: 文件读取完毕, 无法提供更多数据 (loop时不会返回0)
  52. 1: 读取正常
  53. */
  54. - (int) readPCMData:(AudioBufferList*)buf nbSample:(UInt32)cnt;
  55. /**
  56. @abstract 从文件中读取一段音频数据 (二选一)
  57. @param buf, 待填充的音频数据(只会输出交织后的数据, 因此只有一个数据指针)
  58. @param cap, buf的空间大小
  59. @return -1: 解码遇到错误, 请检查音乐文件是否有效
  60. >0: 读取正常, 数值为实际取到的数据字节数
  61. */
  62. - (int) readPCMData:(void*)buf capacity:(UInt32)cap;
  63. #pragma mark - bgm info
  64. /**
  65. @abstract 背景音的duration信息(总时长, 单位:秒)
  66. */
  67. @property (nonatomic, readonly) float bgmDuration;
  68. /**
  69. @abstract 背景音的已经播放长度 (单位:秒)
  70. @discussion 从0开始,最大为bgmDuration长度
  71. */
  72. @property (nonatomic, readonly) float bgmPlayTime;
  73. /**
  74. @abstract 音频的播放进度
  75. @discussion 取值从0.0~1.0,大小为bgmPlayTime/bgmDuration;
  76. */
  77. @property (nonatomic, readonly) float bgmProcess;
  78. /**
  79. @abstract  单曲循环
  80. */
  81. @property (nonatomic, assign) BOOL bLoop;
  82. @end