MediaCodec: Video freeze after a time in playing UDP in android
I try to play UDP in Android Tv Box by vlclib. Sometimes video freeze but not sound and I still hear the sound. Some another time video and sound stop, both. How could I make it right or how could I detect that when it stops, try to reload UDP address and continue the playing Tv.
this is my VideoController class:
` class VideoController(activity: AppCompatActivity) : IVLCVout.Callback, MediaPlayer.EventListener {
// create TAG for logging
companion object {
private var TAG = "VideoController"
}
// declare media player object
private var mediaPlayer: MediaPlayer? = null
// declare surface view object
var mSurface: SurfaceView? = null
// declare surface holder object
var holder: SurfaceHolder? = null
// declare libvlc object
private var libvlc: LibVLC? = null
// declare/initialize activity
private var activity: Activity? = null
init {
this.activity = activity
}
/////////////////////
/////////////////////
fun createPlayer(media: String) {
if (mediaPlayer != null && libvlc != null) {
releasePlayer()
}
Log.i(TAG, "Creating vlc player")
try {
// create arraylist to assign option to create libvlc object
val options = ArrayList<String>()
options.add("--http-reconnect")
options.add("-vvv") // verbosity
options.add("--aout=opensles")
options.add("--android-display-chroma=RV32")
options.add("--audio-time-stretch") // time stretching
options.add("--deinterlace=-1")
options.add("--deinterlace-mode=yadif")
options.add("--sout-deinterlace-mode=yadif")
options.add("--video-filter=deinterlace")
options.add("--no-hdtv-fix")
options.add("--network-caching=8192")
options.add("--udp-buffer=8192")
options.add("--clock-jitter=0")
options.add("--clock-synchro=0")
// create libvlc object
libvlc = LibVLC(activity, options)
// get surface view holder to display video
this.holder = mSurface!!.holder
holder!!.setKeepScreenOn(true)
// Creating media player
mediaPlayer = MediaPlayer(libvlc)
// Setting up video output
val vout = mediaPlayer!!.vlcVout
vout.setVideoView(mSurface)
vout.addCallback(this)
vout.attachViews()
val m = Media(libvlc, Uri.parse(media))
mediaPlayer!!.setMedia(m)
mediaPlayer!!.play()
} catch (e: Exception) {
Toast.makeText(
activity, "Error in creating player!", Toast
.LENGTH_LONG
).show()
}
}
fun stopAll() {
mediaPlayer?.stop()
mediaPlayer = null
holder = null
libvlc = null
}
/*
* release player
* */
fun releasePlayer() {
Log.i(TAG, "releasing player started")
if (libvlc == null)
return
mediaPlayer!!.stop()
var vout: IVLCVout = mediaPlayer!!.vlcVout
vout.removeCallback(this)
vout.detachViews()
mediaPlayer!!.release()
mediaPlayer = null
holder = null
libvlc!!.release()
libvlc = null
Log.i(TAG, "released player")
}
override fun onEvent(event: MediaPlayer.Event) {
when (event.type) {
MediaPlayer.Event.EndReached -> {
this.releasePlayer()
}
MediaPlayer.Event.Playing -> Log.i("playing", "playing")
MediaPlayer.Event.Paused -> {
Log.i("paused", "paused")
mediaPlayer?.play()
}
MediaPlayer.Event.Stopped -> {
Log.i("stopped", "stopped")
mediaPlayer?.play()
}
else -> Log.i("nothing", "nothing")
}
}
override fun onSurfacesCreated(vlcVout: IVLCVout?) {
val sw = mSurface!!.width
val sh = mSurface!!.height
if (sw * sh == 0) {
Log.e(TAG, "Invalid surface size")
return
}
mediaPlayer!!.vlcVout.setWindowSize(sw, sh)
mediaPlayer!!.aspectRatio = "Wide-Screen"
mediaPlayer!!.setScale(0f)
}
override fun onSurfacesDestroyed(vlcVout: IVLCVout?) {
releasePlayer()
}
} `
It's better that I could detect its stop and continue video after lag.
Edited by Thomas Guillem