鸿蒙音视频播放器:libwlmedia

libwlmedia

跨平台播放器wlmedia现在已经支持了鸿蒙(Harmony)平台了,SDK插件地址:libwlmedia

一、接入SDK

1.1 导入SDK

bash 复制代码
ohpm i @ywl5320/libwlmedia

1.2 添加权限(可选)

如果需要播放网络视频,需要添加网络权限

bash 复制代码
#module.json5
"requestPermissions": [
    {
    "name": "ohos.permission.INTERNET"
    }
]

1.3 设置XComponentController

bash 复制代码
let wlPlayer: WlPlayer = new WlPlayer();
let wlComponentController: WlComponentController = new WlComponentController(this.wlPlayer);
...
XComponent({
        type: XComponentType.SURFACE,
        controller: this.wlComponentController
    })
    .width('100%')
    .height('100%')
...

1.4 基础调用代码

bash 复制代码
@Entry
@Component
struct Index{
    private wlPlayer: WlPlayer | null = null;
    private wlComponentController: WlComponentController | null = null;

    aboutToAppear(): void {
        this.wlPlayer = new WlPlayer();
        this.wlPlayer.setOnMediaInfoListener({
        onPrepared: (): void => {
            // 异步准备好后回调,这里调用 wlplayer.start() 开始播放
            this.wlPlayer?.start();
        },
        onTimeInfo: (currentTime: number, bufferTime: number): void => {
            // 时间进度回调 
        },
        onComplete: (wlCompleteType: WlCompleteType, msg: string): void => {
            if (wlCompleteType == WlCompleteType.WL_COMPLETE_EOF) {
                // 正常播放完成
            } else if (wlCompleteType == WlCompleteType.WL_COMPLETE_ERROR) {
                // 播放出错,原因为:msg 字段
            } else if (wlCompleteType == WlCompleteType.WL_COMPLETE_HANDLE) {
                // 主动调用 wlPlayer.stop() 会回调此类型
            } else if (wlCompleteType == WlCompleteType.WL_COMPLETE_NEXT) {
                // 正在播放中,切换了新的数据源,会回调此类型
            } else if (wlCompleteType == WlCompleteType.WL_COMPLETE_TIMEOUT) {
                // 播放超时,会回调此接口
            } else if (wlCompleteType == WlCompleteType.WL_COMPLETE_LOOP) {
                // 循环播放中,每开始新的一次循环,会回调此接口
            }
        },
        onLoadInfo: (loadStatus: WlLoadStatus, progress: number, speed: number): void => {
            // 加载状态回调
            if (wlLoadStatus == WlLoadStatus.WL_LOADING_STATUS_START) {
            // 开始加载
            } else if (wlLoadStatus == WlLoadStatus.WL_LOADING_STATUS_PROGRESS) {
            // 加载进度
            } else if (wlLoadStatus == WlLoadStatus.WL_LOADING_STATUS_FINISH) {
            // 加载完成
            }
        },
        onFirstFrameRendered: (): void => {
            // seek 完成回调
        },
        onSeekFinish: (): void => {
            // seek 完成回调
        },
        onAudioInterrupt: (type: WlAudioInterruptType, hint: WlAudioInterruptHint) => {
            // 音频打断,和系统一致
        }
        this.wlComponentController = new WlComponentController(this.wlPlayer);
        this.wlPlayer.setClearLastVideoFrame(this.wlComponentController.getUniqueNum(), false);
    }

    // 退出 销毁资源
    onBackPress(): boolean | void {
        this.wlPlayer?.release()
    }

    build() {
        Column() {
            XComponent({
          type: XComponentType.SURFACE,
          controller: this.wlComponentController
        })
          .onLoad((event) => {
            // 加载完成后就开始播放
            this.wlPlayer?.setSource(this.filesDir + "/huoying_cut.mkv");
            this.wlPlayer?.prepare();
          })
          .width('100%')
          .height('100%')
        }
    }
}

二、效果展示

HarmonyOS libwlmedia 演示视频

三、注意事项

  • 模拟器播放需要设置解码类型为soft,因为模拟器测试不支持硬解码。
bash 复制代码
this.wlPlayer.setCodecType(WlCodecType.WL_CODEC_SOFT)

四、更多功能

参考:harmonyos_api