From f24bd1f8094762498153b8637d1a5ccd8dc30c02 Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Thu, 24 Mar 2022 11:49:51 +0800 Subject: [PATCH] Feature: set MediaPlayer listener --- .../controller/AudioRecordController.kt | 10 ++-- .../controller/MediaPlayController.kt | 57 ++++++++++++------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioRecordController.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioRecordController.kt index d3662aa..46f1b27 100644 --- a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioRecordController.kt +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioRecordController.kt @@ -35,11 +35,11 @@ open class AudioRecordController { val audioFormat = AudioFormat.ENCODING_PCM_16BIT val channelConfig = AudioFormat.CHANNEL_IN_STEREO minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat) - mAudioRecord = AudioRecord( MediaRecorder.AudioSource.MIC, - sampleRate, - channelConfig, - audioFormat, - minBufferSize) + mAudioRecord = AudioRecord( MediaRecorder.AudioSource.MIC, + sampleRate, + channelConfig, + audioFormat, + minBufferSize) if (enableAudioProcessor) { mAudioProcessController = AudioProcessController() diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/MediaPlayController.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/MediaPlayController.kt index 4c26db9..8dcef04 100644 --- a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/MediaPlayController.kt +++ b/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,34 +31,49 @@ open class MediaPlayController(playerCallback: PlayerCallback) { try { renderFirstFrame = false 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!!.setSurface(surface) + setListener() mediaPlayer!!.prepareAsync() } catch (e: IOException) { 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 { if (mediaPlayer == null) return 0