The player developed based on Kotlin supports MediaPlayer by default, and can be extended with VLC player, IJK player, EXO player, Alibaba Cloud player, and any player that uses TextureView
Introduction to the book (to be completed):https://www.jianshu.com/nb/50294642
implementation 'com.gitee.zhangmengxiong:MXVideo:1.9.4'
dependencies {
implementation 'com.gitee.zhangmengxiong:MXVideo:x.x.x'
}
<com.mx.video.MXVideoStd
android:id="@+id/mxVideoStd"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
// The lifecycle of an activity or fragment is changed, and the pause/resume function is handled when entering the background/foreground
override fun onStart() {
mxVideoStd.onStart()
super.onStart()
}
override fun onStop() {
mxVideoStd.onStop()
super.onStop()
}
// Set up a playback placeholder
Glide.with(this).load("http://www.xxx.com/xxx.png").into(mxVideoStd.getPosterImageView())
// By default, it is played from the last progress
mxVideoStd.setSource(MXPlaySource(Uri.parse("https://aaa.bbb.com/xxx.mp4"), "标题1"))
mxVideoStd.startPlay()
// Play from the beginning
mxVideoStd.setSource(MXPlaySource(Uri.parse("https://aaa.bbb.com/xxx.mp4"), "标题1"), seekTo = 0)
mxVideoStd.startPlay()
// Play from the 10th second
mxVideoStd.setSource(MXPlaySource(Uri.parse("https://aaa.bbb.com/xxx.mp4"), "标题1"), seekTo = 10)
mxVideoStd.startPlay()
MXPlaySource 可选参数说明:
Parameter | Description | Default value |
---|---|---|
title | Title | "" |
headerMap | Network request header | null |
isLooping | Whether to loop | false |
enableSaveProgress | Whether to store and read playback progress | true |
isLiveSource | Whether it is a live source, when it is live at that time, the progress is not displayed, and it cannot be fast-forwarded and fast-forwarded and paused | false |
mxVideoStd.addOnVideoListener(object : MXVideoListener() {
// Playback status changes
override fun onStateChange(state: MXState) {
}
// Playback time changes
override fun onPlayTicket(position: Int, duration: Int) {
}
})
Here, MXVideo holds the currently playing MXVideoStd by default, and can use static methods to operate functions such as exiting the full screen and releasing resources.
You can also use the methods of Vivid: Max Verder, Max Fals, Max Verder, Max Verder, Max Velis, etc.
override fun onBackPressed() {
if (MXVideo.isFullScreen()) {
MXVideo.gotoNormalScreen()
return
}
super.onBackPressed()
}
override fun onDestroy() {
MXVideo.releaseAll()
super.onDestroy()
}
// The default Mediaplayer player is built-in by default
com.mx.video.player.MXSystemPlayer
// Google's Exo Player
com.mx.mxvideo_demo.player.exo.MXExoPlayer
// VLC Player
com.mx.mxvideo_demo.player.vlc.MXVLCPlayer
// IJK Player
com.mx.mxvideo_demo.player.MXIJKPlayer
// Alibaba Cloud Player supports only armeabi-v7a and arm64-v8a CPUs
com.mx.mxvideo_demo.player.MXAliPlayer
// Setting the playback source is possible to set the kernel, default = MXSystemPlayer
mxVideoStd.setPlayer(MXSystemPlayer::class.java)
mxVideoStd.setSource(MXPlaySource(Uri.parse("xxx"), title = "xxx"), seekTo = 0)
// default angle = MXOrientation.DEGREE_0
mxVideoStd.setTextureOrientation(MXOrientation.DEGREE_90)
// default value = false
mxVideoStd.setAudioMute(true)
// Default=1f, when setting=0f, the video is muted
// Valid values: 0f - > 1f
mxVideoStd.setVolumePercent(0.5f)
// Force the fill of the width and height MXScale.FILL_PARENT
// Depending on the size of the video, the adaptive width and height MXScale.CENTER_CROP
// Default Fill Rule = MXScale.CENTER_CROP
mxVideoStd.setScaleType(MXScale.CENTER_CROP)
在页面xml中添加,layout_width一般设置match_parent,高度wrap_content
<com.mx.video.MXVideoStd
android:id="@+id/mxVideoStd"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
可以设置任意宽高比,如果设置宽高比,则控件高度需要设置android:layout_height="wrap_content",否则不生效。
当取消约束、MXVideo高度自适应、填充规则=MXScale.CENTER_CROP时,控件高度会自动根据视频宽高自动填充高度
// MXVideoStd控件设置宽高比= 16:9
mxVideoStd.setDimensionRatio(16.0 / 9.0)
// MXVideoStd控件设置宽高比= 4:3
mxVideoStd.setDimensionRatio(4.0 / 3.0)
// 取消约束
mxVideoStd.setDimensionRatio(0.0)
// Progress Units: Seconds can be invoked after playback is initiated, after an error or before playback ends
mxVideoStd.seekTo(55)
mxVideoStd.getConfig().canSeekByUser.set(false)
mxVideoStd.getConfig().canFullScreen.set(false)
mxVideoStd.getConfig().canShowNetSpeed.set(false)
mxVideoStd.getConfig().hidePlayBtnWhenNoSource.set(false)
// default setting =true
// The full-screen button is only displayed when canFullScreen=true & showFullScreenButton=true
mxVideoStd.getConfig().showFullScreenButton.set(false)
mxVideoStd.getConfig().canShowSystemTime.set(false)
mxVideoStd.getConfig().canShowBottomSeekBar.set(false)
mxVideoStd.getConfig().canShowBatteryImg.set(false)
mxVideoStd.getConfig().showTipIfNotWifi.set(false)
mxVideoStd.getConfig().mirrorMode.set(true)
mxVideoStd.getConfig().gotoNormalScreenWhenComplete.set(false)
mxVideoStd.getConfig().gotoNormalScreenWhenError.set(false)
mxVideoStd.getConfig().canPauseByUser.set(false)
mxVideoStd.getConfig().autoFullScreenBySensor.set(true)
// Pre-playback settings: Default=MXSensorMode.SENSOR_FIT_VIDEO
// MXSensorMode.SENSOR_AUTO = Follow the direction of gravity
// MXSensorMode.SENSOR_FIT_VIDEO = Automatically rotates 0 or 180 degrees to follow the width and height of the video
// MXSensorMode.SENSOR_NO = Fixed landscape or portrait based on the video aspect ratio, landscape = (video width > = height) -- Portrait = (video width < height)
mxVideoStd.getConfig().fullScreenSensorMode.set(MXSensorMode.SENSOR_AUTO)
mxVideoStd.getConfig().replayLiveSourceWhenError.set(true)
mxVideoStd.getConfig().animatorDuration.set(200L)
if (mxVideoStd.isPlaying()) {
val bitmap: Bitmap? = mxVideoStd.getTextureView()?.bitmap
screenCapImg.setImageBitmap(bitmap)
}
config.enableTouchWhenNormalScreen.set(true)
// default = 1.0
config.playSpeed.set(1.0)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。