Feature: set MediaPlayer listener

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

@ -35,11 +35,11 @@ open class AudioRecordController {
val audioFormat = AudioFormat.ENCODING_PCM_16BIT val audioFormat = AudioFormat.ENCODING_PCM_16BIT
val channelConfig = AudioFormat.CHANNEL_IN_STEREO val channelConfig = AudioFormat.CHANNEL_IN_STEREO
minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat) minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat)
mAudioRecord = AudioRecord( MediaRecorder.AudioSource.MIC, mAudioRecord = AudioRecord( MediaRecorder.AudioSource.MIC,
sampleRate, sampleRate,
channelConfig, channelConfig,
audioFormat, audioFormat,
minBufferSize) minBufferSize)
if (enableAudioProcessor) { if (enableAudioProcessor) {
mAudioProcessController = AudioProcessController() mAudioProcessController = AudioProcessController()

@ -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,34 +31,49 @@ open class MediaPlayController(playerCallback: PlayerCallback) {
try { try {
renderFirstFrame = false renderFirstFrame = false
mediaPlayer = MediaPlayer() mediaPlayer = MediaPlayer()
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 ->
(
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
if (!renderFirstFrame) {
renderFirstFrame = true
playerCallback?.onRenderFirstFrame()
}
})
return@setOnInfoListener true
}
mediaPlayer!!.setDataSource(filePath) mediaPlayer!!.setDataSource(filePath)
mediaPlayer!!.setSurface(surface) mediaPlayer!!.setSurface(surface)
setListener()
mediaPlayer!!.prepareAsync() mediaPlayer!!.prepareAsync()
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
} }
} }
private fun setListener() {
mediaPlayer!!.setOnPreparedListener {
mediaPlayer!!.start()
playerCallback?.onPrepare()
}
mediaPlayer!!.setOnInfoListener { mp: MediaPlayer?, what: Int, extra: Int ->
(
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
if (!renderFirstFrame) {
renderFirstFrame = true
playerCallback?.onRenderFirstFrame()
}
})
return@setOnInfoListener true
}
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()
}
}
fun currentPosition(): Int { fun currentPosition(): Int {
if (mediaPlayer == null) if (mediaPlayer == null)
return 0 return 0

Loading…
Cancel
Save