android使用开源库VLC播放网络摄像头内容
android-VLC
添加必要so库文件
将vlc源码文件拷贝到当前项目中
使用VLC播放rtsp流
package com.moyu.ipccamera
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.SurfaceView
import org.videolan.libvlc.LibVLC
import org.videolan.libvlc.Media
import org.videolan.libvlc.MediaPlayer
import java.util.ArrayList
class MainActivity : AppCompatActivity() {
private val TAG = "$\$MainActivity$$"
private lateinit var mSurfaceView: SurfaceView
private val mVideoWidth = 1280
private val mVideoHeight = 800
private var libVLC: LibVLC? = null
private var mMediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
private fun init() {
mSurfaceView = findViewById(R.id.main_sv)
createPlayer(500)
}
private fun createPlayer(nc: Int) {
val options = ArrayList<String>()
options.add("--network-caching=$nc")
libVLC = LibVLC(applicationContext, options)
mMediaPlayer = MediaPlayer(libVLC)
mMediaPlayer?.setEventListener(mEventListener)
// Set up video output
val vout = mMediaPlayer!!.vlcVout
vout.setVideoView(mSurfaceView)
vout.setWindowSize(mVideoWidth, mVideoHeight)
vout.attachViews()
val media = Media(
libVLC,
Uri.parse("rtsp://admin:admin12345@192.168.88.196/:554/h264/ch1/main/av_stream")
)
media.setHWDecoderEnabled(false, false)
mMediaPlayer!!.media = media
mMediaPlayer!!.play()
}
private fun releasePlayer() {
mMediaPlayer?.let {
it.stop()
it.vlcVout.detachViews()
mMediaPlayer = null
}
libVLC?.let {
it.release()
libVLC = null
}
}
private val mEventListener = MediaPlayer.EventListener {
when (it.type) {
MediaPlayer.Event.EndReached -> {
Log.i(TAG, "onEvent: EndReached")
}
MediaPlayer.Event.Buffering -> {
Log.i(TAG, "onEvent: Buffering")
}
MediaPlayer.Event.Opening -> {
Log.i(TAG, "onEvent: Opening")
}
MediaPlayer.Event.Playing -> {
Log.i(TAG, "onEvent: Playing")
}
MediaPlayer.Event.Stopped -> {
Log.i(TAG, "onEvent: Stopped")
}
}
}
override fun onDestroy() {
super.onDestroy()
releasePlayer()
}
}
附上rtsp流截图
VLC示例代码:https://code.videolan.org/videolan/libvlc-android-samples
本项目示例代码:https://gitee.com/cmy_mob_dllm/android/tree/master/IpcCamera