Feature: set MediaPlayer listener

pull/209/head
xufuji456 3 years ago
parent b294d3d11f
commit f24bd1f809
  1. 41
      AndroidMedia/src/main/java/com/frank/androidmedia/controller/MediaPlayController.kt

@ -2,6 +2,8 @@ package com.frank.androidmedia.controller
import android.media.MediaPlayer
import android.media.PlaybackParams
import android.media.TimedText
import android.util.Log
import android.view.Surface
import com.frank.androidmedia.listener.PlayerCallback
import java.io.IOException
@ -29,17 +31,22 @@ open class MediaPlayController(playerCallback: PlayerCallback) {
try {
renderFirstFrame = false
mediaPlayer = MediaPlayer()
mediaPlayer!!.setDataSource(filePath)
mediaPlayer!!.setSurface(surface)
setListener()
mediaPlayer!!.prepareAsync()
} catch (e: IOException) {
e.printStackTrace()
}
}
private fun setListener() {
mediaPlayer!!.setOnPreparedListener {
mediaPlayer!!.start()
playerCallback?.onPrepare()
}
mediaPlayer!!.setOnErrorListener { mp: MediaPlayer?, what: Int, extra: Int ->
return@setOnErrorListener playerCallback?.onError(what, extra)!!
}
mediaPlayer!!.setOnCompletionListener {
playerCallback?.onCompleteListener()
}
mediaPlayer!!.setOnInfoListener { mp, what, extra ->
mediaPlayer!!.setOnInfoListener { mp: MediaPlayer?, what: Int, extra: Int ->
(
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
if (!renderFirstFrame) {
@ -49,11 +56,21 @@ open class MediaPlayController(playerCallback: PlayerCallback) {
})
return@setOnInfoListener true
}
mediaPlayer!!.setDataSource(filePath)
mediaPlayer!!.setSurface(surface)
mediaPlayer!!.prepareAsync()
} catch (e: IOException) {
e.printStackTrace()
mediaPlayer!!.setOnBufferingUpdateListener { mp, percent ->
Log.i("MediaPlayer", "buffer percent=$percent")
}
mediaPlayer!!.setOnTimedTextListener { mp: MediaPlayer?, text: TimedText? ->
Log.i("MediaPlayer", "subtitle=" + text?.text)
}
mediaPlayer!!.setOnErrorListener { mp: MediaPlayer?, what: Int, extra: Int ->
return@setOnErrorListener playerCallback?.onError(what, extra)!!
}
mediaPlayer!!.setOnCompletionListener {
playerCallback?.onCompleteListener()
}
}

Loading…
Cancel
Save