HarmonyOS 音视频播放SDK,几句代码即可实现音视频播放功能~
使用简单,功能丰富,支持手机、车机系统、电视、电视盒子、手表等智能设备
ohpm i @ywl5320/libwlmedia
#module. json5
"requestPermissions" : [
{
"name" : "ohos.permission.INTERNET"
}
]
2.3 设置XComponentController
let wlPlayer : WlPlayer = new WlPlayer ( ) ;
let wlComponentController : WlComponentController = new WlComponentController ( this . wlPlayer ) ;
...
XComponent ( {
type : XComponentType . SURFACE ,
controller : this . wlComponentController
} )
. width ( '100%' )
. height ( '100%' )
. . .
@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%' )
}
}
}
- keep class com.ywl5320.wlmedia.* {* ; }