ohos_danmaku_flame_master:基于 OpenHarmony 的弹幕框架项目

用户可快速在 OpenHarmony 应用中集成弹幕功能,支持纯文本与 GIF 弹幕发送,可设置显示区域、播放状态及点击交互,提供丰富的弹幕样式与管理接口,满足多样化弹幕展示需求。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
9 months ago
9 months ago
2 years ago
2 years ago
5 months ago
3 years ago
9 months ago
4 years ago
3 years ago
2 years ago
1 year ago
5 months ago
5 months ago
2 years ago
3 years ago
2 years ago
2 years ago
9 months ago

DanmakuFlameMaster

Introduction

DanmakuFlameMaster is a framework for rendering and managing bullet comments. It provides APIs for sending text-only bullet comments, setting the display area of bullet comments on the screen, and controlling the playback status of bullet comments.

How to Install

ohpm install @ohos/danmakuflamemaster

For details about the OpenHarmony ohpm environment configuration, see OpenHarmony HAR.

How to Use

Initializing and Setting Bullet Screen UI Properties

 this.model.setWidth(lpx2px(720))
    this.model.setHeight(lpx2px(720))
    let maxLinesPair: Map<number, number> = new Map();
    maxLinesPair.set(BaseDanmaku.TYPE_SCROLL_RL, 5);
    let overlappingEnablePair: Map<number, boolean> = new Map();
    overlappingEnablePair.set(BaseDanmaku.TYPE_SCROLL_RL, true);
    overlappingEnablePair.set(BaseDanmaku.TYPE_FIX_TOP, true);
    this.mContext = DanmakuContext.create();
    this.mContext.setDanmakuStyle(DANMAKU_STYLE_STROKEN, 3)
      .setDuplicateMergingEnabled(false)
      .setScrollSpeedFactor(1.2)
      .setScaleTextSize(1.2)
      .setCacheStuffer(new SpannedCacheStuffer(), this.mCacheStufferAdapter) // Use SpannedCacheStuffer to implement rich text (spanned text) rendering.
      .setMaximumLines(maxLinesPair)
      .preventOverlapping(overlappingEnablePair)
      .setDanmakuMargin(40);
    let that = this
    if (this.model != null) {
      this.mParser = this.createParser();
      this.model.setCallback(new Call(that));
      this.model.setOnDanmakuClickListener(new OnDanMu(that))
      this.model.prepare(this.mParser, this.mContext);
      this.model.showFPS(true);
    }
  class Call implements Callback {
  private that: ESObject;

  constructor(that: ESObject) {
    this.that = that
  }

  public updateTimer(timer: DanmakuTimer): void {
  }

  public drawingFinished(): void {

  }

  public danmakuShown(danmaku: BaseDanmaku): void {
  }

  public prepared(): void {
    this.that.model.start();
  }
}
class OnDanMu implements OnDanmakuClickListener {
  private that:ESObject;
  constructor(that :ESObject) {
    this.that = that
  }
  onDanmakuClick(danmakus: IDanmakus): boolean{
    console.log('DFM onDanmakuClick: danmakus size:' + danmakus.size())
    let latest: BaseDanmaku = danmakus.last()
    if (null != latest) {
      console.log('DFM onDanmakuClick: text of latest danmaku:' + latest.text)
      return true
    }
    return false
  };

  onDanmakuLongClick(danmakus: IDanmakus): boolean{
    return false
  };

  onViewClick(view: IDanmakuView): boolean{
    this.that.isVisible = true
    return false
  };
}

Adding a Bullet Comment

let danmaku: BaseDanmaku = this.mContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
    danmaku.text = "This is a bullet comment." + SystemClock.uptimeMillis ();
    danmaku.padding = 5;
    danmaku.priority = 0; // Priority of the bullet comment. The value 0 means the bullet comment will be one of the last to be displayed when there is limited space.
    danmaku.isLive = isLive.valueOf();
    danmaku.setTime(this.model.getCurrentTime() + 1200);
    danmaku.textSize = 25 * (this.mParser.getDisplayer().getDensity() * 0.8);
    danmaku.textColor = 0xffff0000;
    danmaku.textShadowColor = 0xffffffff;
    danmaku.borderColor = 0xff00ff00;
    this.model.addDanmaku(danmaku);

Adding a gif barrage

let file = fs.openSync(this.gifPath, fs.OpenMode.READ_ONLY)
const imageSource = image.createImageSource(file.fd);
imageSource.createPixelMapList().then((pixelMapListParam: Array<image.PixelMap>)=> {
  let danmaku: BaseDanmaku = this.mContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
  //Set the frame data for the GIF barrage, used for canvas rendering.
  danmaku.setPixelMapList(pixelMapListParam)
  //if the image is in GIF format,Set to true when a GIF image is provided; default value is false.
  danmaku.setIsAnimation(true)
  danmaku.text = "<img src="https://raw.gitcode.com/""" + this.gifPath + "\" width=\"" + 99 + "\" height=\"" + 99 + "\"/>";
  danmaku.padding = 0;
  danmaku.priority = 1;
  danmaku.isLive = islive;
  danmaku.setTime(this.model.getCurrentTime() + 1200);
  danmaku.textShadowColor = 0;
  this.model.addDanmaku(danmaku);
})

Available APIs

model: DanmakuView.Model = new DanmakuView.Model()

API Description
model.addDanmaku(danmaku) Adds a bullet comment.
model.getCurrentTime() Obtains the current bullet screen time.
model.hide() Hides bullet comments.
model.show() Shows bullet comments.
model.pause() Pauses the playback of bullet comments.
model.resume() Resumes the playback of bullet comments.
model.setWidth(lpx2px(1280)) Sets the width of the bullet comment view.
model.setHeight(lpx2px(720)) Sets the height of the bullet comment view.
model.prepare(this.mParser, this.mContext) Prepares the bullet comment view with a parser and context, initializing it for use.
model.showFPS(true) Shows the frames per second (FPS) on the bullet screen.
model.setOnDanmakuClickListener() Sets a listener for bullet screen click events.
setIsAnimation(boolean) if the image is in GIF format,Set to true when a GIF image is provided; default value is false.
setPixelMapList(pixelMapListParam) Set the frame data for the GIF barrage, used for canvas rendering.

DanmakuContext()

API Description
public static create(): DanmakuContext A constructor used to create a DanmakuContext instance.
public getBaseComparator(): BaseComparator Obtains the comparator used for sorting bullet comments.
public setBaseComparator(baseComparator: BaseComparator) Sets a bullet comment comparator.
public getDisplayer(): AbsDisplayer<ESObject, ESObject> Obtains the displayer object responsible for rendering bullet comments.
public setTypeface(font: string): DanmakuContext Sets the font for the bullet comment text.
public setDanmakuTransparency(p: number): DanmakuContext Sets the transparency for bullet comments.
public setScaleTextSize(p: number): DanmakuContext Sets the scale of the bullet comment font size.
public setDanmakuMargin(m: number): DanmakuContext Sets the margin around each bullet comment.
public setMarginTop(m: number): DanmakuContext Sets the top margin for each bullet comment.
public getFTDanmakuVisibility(): boolean Obtains the visibility of the bullet comments on the top.
public setFBDanmakuVisibility(visible: boolean): DanmakuContext Sets whether to display the bullet comments at the bottom.
public getL2RDanmakuVisibility(): boolean Obtains the visibility of the left-to-right scrolling bullet screen.
public setL2RDanmakuVisibility(visible: boolean): DanmakuContext Sets the visibility of the left-to-right scrolling bullet screen.
`public getR2LDanmakuVisibility(): boolean Obtains the visibility of the right-to-left scrolling bullet screen.
public setR2LDanmakuVisibility(visible: boolean): DanmakuContext Sets the visibility of the right-to-left scrolling bullet screen.
public getSpecialDanmakuVisibility(): boolean Obtains the visibility of special bullet comments (such as animated or styled comments).
public setSpecialDanmakuVisibility(visible: boolean): DanmakuContext Sets the visibility of special bullet comments (such as animated or styled comments).
public setMaximumVisibleSizeInScreen(maxSize: number): DanmakuContext Sets the maximum number of bullet comments visible on the screen. The value -1 means automatic, and 0 means the number of bullet comments visible on the screen is unlimited.
public setDanmakuStyle(style: number, ...values: number[]): DanmakuContext Sets the style of bullet comments, such as stroke or shadow effects.
public setDanmakuBold(bold: boolean): DanmakuContext Sets whether the bullet comment text is in bold.
public setColorValueWhiteList(colors: number[]): DanmakuContext Sets the colors that can be used by bullet comments.
public getColorValueWhiteList(): number[] Obtains the colors that can be used by bullet comments.
public setUserHashBlackList(hashes: string[]): DanmakuContext Sets a user blocklist. The bullet comments of these users will be blocked.
public removeUserHashBlackList(hashes: string[]): DanmakuContext Removes a user from the user blocklist.
public addUserHashBlackList(hashes: string[]): DanmakuContext Adds a user to the user blocklist.
public getUserHashBlackList(): string[] Obtains the user blocklist.
public blockGuestDanmaku(block: boolean): DanmakuContext Enables or disables the bullet comments from guests.
public setScrollSpeedFactor(p: number): DanmakuContext Sets the speed for scrolling bullet comments.
public setDuplicateMergingEnabled(enable: boolean): DanmakuContext Enables or disables the merging of duplicate bullet comments.
public isDuplicateMergingEnabled(): boolean Checks whether merging of duplicate bullet comments is enabled.
public alignBottom(enable: boolean): DanmakuContext Enables or disables aligning bullet comments to the bottom of the screen.
public isAlignBottom(): boolean Checks whether bullet comments are aligned to the bottom of the screen.
public setMaximumLines(pairs: Map<number, number>): DanmakuContext Sets the maximum number of bullet comment lines.
public setOverlapping(pairs: Map<number, boolean>): DanmakuContext Sets whether bullet comments can overlap.
public isMaxLinesLimited(): boolean Checks whether there is a limit on the maximum number of lines for bullet comments.
public isPreventOverlappingEnabled(): boolean Checks whether the function for preventing bullet comment overlapping is enabled.
public setDanmakuSync(danmakuSync: AbsDanmakuSync): DanmakuContext Sets a bullet comment synchronizer.
public setCacheStuffer(cacheStuffer: BaseCacheStuffer, cacheStufferAdapter: Proxy): DanmakuContext Sets a cache stuffer for optimizing bullet comment rendering performance.
public registerConfigChangedCallback(listener: ConfigChangedCallback): void Registers a callback to listen for changes in bullet comment configuration.
public unregisterConfigChangedCallback(listener: ConfigChangedCallback): void Unregisters a callback for changes in bullet comment configuration.
public registerFilter(filter: BaseDanmakuFilter<ESObject>): DanmakuContext Registers a filter for bullet comments.
public unregisterFilter(filter: BaseDanmakuFilter<ESObject>): DanmakuContext Unregisters a filter for bullet comments.
public resetContext(): DanmakuContext Resets DanmakuContext to default.

Proxy()

API Description
public abstract prepareDrawing(danmaku: BaseDanmaku, fromWorkerThread: boolean) Prepares the drawing of the cached bullet comments.
public abstract releaseResource(danmaku: BaseDanmaku) Releases bullet comment resources.

DanmakuTimer()

API Description
public update(curr: number): number Updates the timer.
public add(mills: number): number Adds a time value to a timer.
public getLastInterval(): number Obtains the remaining time before the timer expires.

Duration()

API Description
public setValue(initialDuration: number) Sets the initial duration value for bullet comments.
public setFactor(f: number) Sets a factor for the initial duration of bullet comments.

SpecialDanmaku()

API Description
public getLeft(): number Obtains the left margin of the fixed bullet screen on the top.
public getTop(): number Obtains the top margin of the fixed bullet screen on the top.
public getRight(): number Obtains the right margin of the fixed bullet screen on the top.
public getBottom(): number Obtains the bottom margin of the fixed bullet screen on the top.
public getType(): number Obtains the bullet comment type.
public setTranslationData(beginX: number, beginY: number, endX: number, endY: number, translationDuration: number, translationStartDelay: number) Sets a translation animation for bullet comments.
public setAlphaData(beginAlpha: number, endAlpha: number, alphaDuration: number) Sets an alpha (transparency) animation for bullet comments.

DanmakuUtils()

API Description
public static compare(obj1: BaseDanmaku, obj2: BaseDanmaku): number Compares whether two bullet comments are the same.
public static isDuplicate(obj1: BaseDanmaku, obj2: BaseDanmaku): boolean Whether the two bullet comments are duplicate.
public static create(): DanmakuFactory Creates a DanmakuFactory instance.

About obfuscation

  • Code obfuscation, please seeCode Obfuscation
  • If you want the danmakuflamemaster library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/danmakuflamemaster

Constraints

This project has been verified in the following version:

  • DevEco Studio: NEXT Beta1-5.0.3.806, SDK: API12 Release(5.0.0.66)

Directory Structure

|---- DanmakuFlameMaster  
|     |---- entry    # Sample code
|     |---- library  # DanmakuFlameMaster library
|           |---- src\main\ets\components\common\master\flame\danmaku  # Source code
|           		|---- control   # Code for implementing bullet comment status control.
|           		|---- danmaku 	# Bullet comment base code.
|           		|---- ui 		# Custom bullet screen components
|           |---- index.ets  # External APIs
|     |---- README.md  # Readme                   
|     |---- README_zh.md  # Readme                   

How to Contribute

If you find any problem when using the project, submit an issue or a PR.

License

This project is licensed under Apache License 2.0.

Introduction

用户可快速在 OpenHarmony 应用中集成弹幕功能,支持纯文本与 GIF 弹幕发送,可设置显示区域、播放状态及点击交互,提供丰富的弹幕样式与管理接口,满足多样化弹幕展示需求。【此简介由AI生成】

Customize your domain