|
|
@ -1,16 +1,18 @@ |
|
|
|
package com.frank.androidmedia.controller |
|
|
|
package com.frank.androidmedia.controller |
|
|
|
|
|
|
|
|
|
|
|
import android.media.MediaPlayer |
|
|
|
import android.media.MediaPlayer |
|
|
|
|
|
|
|
import android.media.PlaybackParams |
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author xufulong |
|
|
|
* The controller of MediaPlayer |
|
|
|
* @date 3/18/22 1:53 PM |
|
|
|
* |
|
|
|
* @desc The controller of MediaPlayer |
|
|
|
* @author frank |
|
|
|
|
|
|
|
* @date 2022/3/18 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
open class MediaPlayerController(playerCallback: PlayerCallback) { |
|
|
|
open class MediaPlayController(playerCallback: PlayerCallback) { |
|
|
|
|
|
|
|
|
|
|
|
private var mediaPlayer: MediaPlayer? = null |
|
|
|
private var mediaPlayer: MediaPlayer? = null |
|
|
|
private var renderFirstFrame: Boolean = false |
|
|
|
private var renderFirstFrame: Boolean = false |
|
|
@ -20,7 +22,7 @@ open class MediaPlayerController(playerCallback: PlayerCallback) { |
|
|
|
this.playerCallback = playerCallback |
|
|
|
this.playerCallback = playerCallback |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun initPlayer(filePath :String, surface :Surface) { |
|
|
|
fun initPlayer(filePath: String, surface: Surface) { |
|
|
|
if (mediaPlayer != null) { |
|
|
|
if (mediaPlayer != null) { |
|
|
|
releasePlayer() |
|
|
|
releasePlayer() |
|
|
|
} |
|
|
|
} |
|
|
@ -31,21 +33,20 @@ open class MediaPlayerController(playerCallback: PlayerCallback) { |
|
|
|
mediaPlayer!!.start() |
|
|
|
mediaPlayer!!.start() |
|
|
|
playerCallback?.onPrepare() |
|
|
|
playerCallback?.onPrepare() |
|
|
|
} |
|
|
|
} |
|
|
|
mediaPlayer!!.setOnErrorListener { |
|
|
|
mediaPlayer!!.setOnErrorListener { mp: MediaPlayer?, what: Int, extra: Int -> |
|
|
|
mp: MediaPlayer?, what: Int, extra: Int -> |
|
|
|
|
|
|
|
return@setOnErrorListener playerCallback?.onError(what, extra)!! |
|
|
|
return@setOnErrorListener playerCallback?.onError(what, extra)!! |
|
|
|
} |
|
|
|
} |
|
|
|
mediaPlayer!!.setOnCompletionListener { |
|
|
|
mediaPlayer!!.setOnCompletionListener { |
|
|
|
playerCallback?.onCompleteListener() |
|
|
|
playerCallback?.onCompleteListener() |
|
|
|
} |
|
|
|
} |
|
|
|
mediaPlayer!!.setOnInfoListener { |
|
|
|
mediaPlayer!!.setOnInfoListener { mp, what, extra -> |
|
|
|
mp, what, extra -> ( |
|
|
|
( |
|
|
|
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) { |
|
|
|
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) { |
|
|
|
if (!renderFirstFrame) { |
|
|
|
if (!renderFirstFrame) { |
|
|
|
renderFirstFrame = true |
|
|
|
renderFirstFrame = true |
|
|
|
playerCallback?.onRenderFirstFrame() |
|
|
|
playerCallback?.onRenderFirstFrame() |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
return@setOnInfoListener true |
|
|
|
return@setOnInfoListener true |
|
|
|
} |
|
|
|
} |
|
|
|
mediaPlayer!!.setDataSource(filePath) |
|
|
|
mediaPlayer!!.setDataSource(filePath) |
|
|
@ -56,19 +57,19 @@ open class MediaPlayerController(playerCallback: PlayerCallback) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun currentPosition() : Int { |
|
|
|
fun currentPosition(): Int { |
|
|
|
if (mediaPlayer == null) |
|
|
|
if (mediaPlayer == null) |
|
|
|
return 0 |
|
|
|
return 0 |
|
|
|
return mediaPlayer!!.currentPosition |
|
|
|
return mediaPlayer!!.currentPosition |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun duration() : Int { |
|
|
|
fun duration(): Int { |
|
|
|
if (mediaPlayer == null) |
|
|
|
if (mediaPlayer == null) |
|
|
|
return 0 |
|
|
|
return 0 |
|
|
|
return mediaPlayer!!.duration |
|
|
|
return mediaPlayer!!.duration |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun seekTo(position :Int) { |
|
|
|
fun seekTo(position: Int) { |
|
|
|
mediaPlayer?.seekTo(position) |
|
|
|
mediaPlayer?.seekTo(position) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -83,6 +84,44 @@ open class MediaPlayerController(playerCallback: PlayerCallback) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getVideoWidth(): Int { |
|
|
|
|
|
|
|
return mediaPlayer!!.videoWidth |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getVideoHeight(): Int { |
|
|
|
|
|
|
|
return mediaPlayer!!.videoHeight |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun mute() { |
|
|
|
|
|
|
|
mediaPlayer?.setVolume(0.0f, 0.0f) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun setVolume(volume: Float) { |
|
|
|
|
|
|
|
if (volume < 0 || volume > 1) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
mediaPlayer?.setVolume(volume, volume) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set playback rate |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun setSpeed(speed: Float) { |
|
|
|
|
|
|
|
if (speed <= 0 || speed > 8) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { |
|
|
|
|
|
|
|
val params = PlaybackParams() |
|
|
|
|
|
|
|
params.speed = speed |
|
|
|
|
|
|
|
mediaPlayer?.playbackParams = params |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Select audio or subtitle track, when there are multi tracks |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun selectTrack(trackId: Int) { |
|
|
|
|
|
|
|
mediaPlayer?.selectTrack(trackId) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun releasePlayer() { |
|
|
|
fun releasePlayer() { |
|
|
|
if (mediaPlayer != null) { |
|
|
|
if (mediaPlayer != null) { |
|
|
|
mediaPlayer!!.stop() |
|
|
|
mediaPlayer!!.stop() |