BarrageRenderer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Part of BarrageRenderer. Created by UnAsh.
  2. // Blog: http://blog.exbye.com
  3. // Github: https://github.com/unash/BarrageRenderer
  4. // This code is distributed under the terms and conditions of the MIT license.
  5. // Copyright (c) 2015年 UnAsh.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import <Foundation/Foundation.h>
  25. #import "BarrageHeader.h"
  26. @class BarrageDescriptor;
  27. @class BarrageRenderer;
  28. typedef NS_ENUM(NSInteger, BarrageSpriteStage) {
  29. BarrageSpriteStageBegin = 1, // 弹幕进入屏幕阶段
  30. BarrageSpriteStageEnd = 2 // 弹幕退出屏幕阶段
  31. };
  32. @protocol BarrageRendererDelegate <NSObject>
  33. @optional
  34. /// 通过外部渠道获取当前时间,用于内部时间系统; 当依附的视频具有快进快退功能时,必须实现这个函数,并返回时间轴上的当前时刻,即已经播放的时间.
  35. - (NSTimeInterval)timeForBarrageRenderer:(BarrageRenderer *)renderer;
  36. /// 弹幕生命周期行为,实验特性; 可试用, 亦可以通过继承 BarrageRenderer 来实现相同功能
  37. - (void)barrageRenderer:(BarrageRenderer *)renderer spriteStage:(BarrageSpriteStage)stage spriteParams:(NSDictionary *)params;
  38. @end
  39. /// 弹幕渲染器
  40. @interface BarrageRenderer : NSObject
  41. #pragma mark - life cycle
  42. #pragma mark - control
  43. /// 启动弹幕, 内部时钟从0开始;
  44. /// 若是stop之后的start,则start函数内部会清空records;
  45. /// 视频开始的时候需要同时运行此方法.
  46. - (void)start;
  47. /// 暂停, 已经渲染上去的保持不变, 此时发送弹幕无效, 内部时钟暂停;
  48. /// 视频暂停的时候需要同时运行此方法.
  49. - (void)pause;
  50. /// 停止弹幕渲染, 会清空所有; 再发弹幕就无效了; 一切都会停止;
  51. /// 此方法在不再需要弹幕的时候必须调用,否则可能造成内存泄露.
  52. - (void)stop;
  53. /// 接收弹幕消息, 如果尚未start, 则调用无效.
  54. - (void)receive:(BarrageDescriptor *)descriptor;
  55. #pragma mark - config
  56. /// 画布的边距
  57. @property(nonatomic,assign)UIEdgeInsets canvasMargin;
  58. /// 画布是否拦截事件
  59. @property(nonatomic,assign)BOOL masked;
  60. /// 是否开启平滑; 对于突发弹幕,开启平滑可以降低瞬间的掉帧,默认关闭
  61. /// 对于 CPU 性能不错的手机(比如>=iphone7),推荐开启此功能
  62. /// 设置时间平滑, 应对弹幕激增;
  63. /// 范围为[0,1],当为0时,无平滑; 否则越大,越平滑;
  64. /// 高平滑值在大量弹幕(一般100+)的时候,可能造成弹幕丢失
  65. @property(nonatomic,assign)CGFloat smoothness;
  66. /// 调整弹幕整体速度, 需要>0, 否则会被抛弃.
  67. @property(nonatomic,assign)CGFloat speed;
  68. /// 如果时光倒流, 弹幕能不能重新显示. 若设置为YES, 则当执行后退8s->5s时,会显示6s时刻上的弹幕.
  69. @property(nonatomic,assign)BOOL redisplay;
  70. /// 获取外部时间的代理,若需将弹幕固定到时间点上,则需设置此代理
  71. @property(nonatomic,weak)id<BarrageRendererDelegate> delegate;
  72. #pragma mark - output
  73. /// 返回给外部的view
  74. @property(nonatomic,weak)UIView * view;
  75. /// 获取当前屏幕弹幕数量,spriteName表示弹幕类名,如果传入nil,则计算屏幕显示出的所有弹幕数量.
  76. - (NSInteger)spritesNumberWithName:(NSString *)spriteName;
  77. /// 移除当前屏幕上的弹幕; 可在转屏幕的时候调用,以应对位置错乱的情况
  78. /// spriteName表示弹幕类名,如果传入nil,则计算屏幕显示出的所有弹幕数量.
  79. - (void)removePresentSpritesWithName:(NSString *)spriteName;
  80. /// 移除标识符为 identifier 的弹幕
  81. - (void)removeSpriteWithIdentifier:(NSString *)identifier;
  82. /// 逻辑时间,露出参考.
  83. @property(nonatomic,assign,readonly)NSTimeInterval time;
  84. #pragma mark - z-index
  85. /// 是否开启z-index功能,开启之后,性能会稍有降低,绘图会按照z_index进行,值越大,越靠上;默认关闭.
  86. @property(nonatomic,assign)BOOL zIndex;
  87. #pragma mark - record
  88. /// 如需要记录,需要在运行start之后立即运行此函数,内部会通过时间差计算delay;
  89. /// 记录弹幕,可能会序列化到本地; 默认为NO.
  90. @property(nonatomic,assign)BOOL recording;
  91. /// 加载已经存在的弹幕,如果已经start, 会立刻被调用receive; 否则, 会等到start的时候再调用receive.
  92. /// 在 v2.1 之后, 使用 load 不会重新调整 descriptor 的 delay; 而在之前的版本, 会调整 descriptor 的 delay
  93. - (void)load:(NSArray *)descriptors;
  94. /// 弹幕记录数组.
  95. - (NSArray *)records;
  96. @end