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