Skip to content

Commit

Permalink
fix player issues add interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Reza Amuzadeh committed May 1, 2021
1 parent 0f906b5 commit f04941a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MainActivityKotlin : AppCompatActivity(), AndExoPlayerListener, View.OnCli
selectLocaleVideo()
}
R.id.stream_mp4 -> {
loadMP4Stream(PublicValues.TEST_URL_MP4_V5)
loadMP4Stream(PublicValues.TEST_URL_MP4_V6)
}
R.id.stream_hls -> {
loadHLSStream(PublicValues.TEST_URL_HLS)
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 19
versionName "2.0.3"
versionCode 21
versionName "2.0.5"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package com.potyvideo.library.kotlin

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatImageButton
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.ui.PlayerView
import com.potyvideo.library.R
import com.potyvideo.library.kotlin.globalEnums.*
import com.potyvideo.library.kotlin.utils.DoubleClick
import com.potyvideo.library.utils.PublicFunctions


abstract class AndExoPlayerRoot @JvmOverloads constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AndExoPlayerView(

private var player: SimpleExoPlayer = SimpleExoPlayer.Builder(context).build()
private var andExoPlayerListener: AndExoPlayerListener? = null
private var playWhenReady: Boolean = false
private var currPlayWhenReady: Boolean = false
private var playbackPosition: Long = 0
private var currentWindow: Int = 0
private var currVolume: Float = 0f
Expand Down Expand Up @@ -66,28 +66,32 @@ class AndExoPlayerView(

init {
player.addListener(this)

extractAttrs(attributeSet)
}

private fun extractAttrs(attributeSet: AttributeSet?) {

attributeSet.let {
val attributes: TypedArray = context.obtainStyledAttributes(it, R.styleable.AndExoPlayerView)
if (attributes.hasValue(R.styleable.AndExoPlayerView_andexo_aspect_ratio)) {
val aspectRatio = attributes.getInteger(R.styleable.AndExoPlayerView_andexo_aspect_ratio, EnumAspectRatio.ASPECT_16_9.value)

val typedArray: TypedArray = context.obtainStyledAttributes(it, R.styleable.AndExoPlayerView)

if (typedArray.hasValue(R.styleable.AndExoPlayerView_andexo_aspect_ratio)) {
val aspectRatio = typedArray.getInteger(R.styleable.AndExoPlayerView_andexo_aspect_ratio, EnumAspectRatio.ASPECT_16_9.value)
setAspectRatio(EnumAspectRatio[aspectRatio])
}

attributes.recycle()
if (typedArray.hasValue(R.styleable.AndExoPlayerView_andexo_resize_mode)) {
val resizeMode: Int = typedArray.getInteger(R.styleable.AndExoPlayerView_andexo_resize_mode, EnumResizeMode.FILL.value)
setResizeMode(EnumResizeMode[resizeMode])
}

if (typedArray.hasValue(R.styleable.AndExoPlayerView_andexo_play_when_ready)) {
setPlayWhenReady(typedArray.getBoolean(R.styleable.AndExoPlayerView_andexo_play_when_ready, false))
}

typedArray.recycle()
}

/*
val attributes = context.obtainStyledAttributes(attrs, R.styleable.BenefitView)
imageView.setImageDrawable(attributes.getDrawable(R.styleable.BenefitView_image))
textView.text = attributes.getString(R.styleable.BenefitView_text)
attributes.recycle()
*/
}

override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters) {
Expand All @@ -106,16 +110,16 @@ class AndExoPlayerView(
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
when (playbackState) {
ExoPlayer.STATE_BUFFERING -> {

andExoPlayerListener?.let { andExoPlayerListener!!.onExoBuffering() }
}
ExoPlayer.STATE_ENDED -> {

andExoPlayerListener?.let { andExoPlayerListener!!.onExoEnded() }
}
ExoPlayer.STATE_IDLE -> {

andExoPlayerListener?.let { andExoPlayerListener!!.onExoIdle() }
}
ExoPlayer.STATE_READY -> {

andExoPlayerListener?.let { andExoPlayerListener!!.onExoReady() }
}
else -> {

Expand All @@ -135,10 +139,11 @@ class AndExoPlayerView(
override fun onTimelineChanged(timeline: Timeline, reason: Int) {
}

private fun releasePlayer() {
playWhenReady = player.playWhenReady
fun releasePlayer() {
currPlayWhenReady = player.playWhenReady
playbackPosition = player.currentPosition
currentWindow = player.currentWindowIndex
player.stop()
player.release()
}

Expand Down Expand Up @@ -192,11 +197,6 @@ class AndExoPlayerView(

fun setSource(source: String) {

/*if (!PublicFunctions.isThisSourceSupported(source)) {
showRetryView("This source is not supported!")
return
}*/

currSource = source

val mediaItem = buildMediaItem(source)
Expand Down Expand Up @@ -241,8 +241,9 @@ class AndExoPlayerView(
showUnMuteButton()
}

fun setRepeatMode(repeatModeMode: EnumRepeatMode = EnumRepeatMode.REPEAT_OFF) {
when (repeatModeMode) {
fun setRepeatMode(repeatMode: EnumRepeatMode = EnumRepeatMode.REPEAT_OFF) {
this.currRepeatMode = repeatMode
when (repeatMode) {
EnumRepeatMode.REPEAT_OFF -> {
player.repeatMode = Player.REPEAT_MODE_OFF
}
Expand All @@ -252,6 +253,9 @@ class AndExoPlayerView(
EnumRepeatMode.REPEAT_ALWAYS -> {
player.repeatMode = Player.REPEAT_MODE_ALL
}
else -> {
player.repeatMode = Player.REPEAT_MODE_OFF
}
}
}

Expand Down Expand Up @@ -285,4 +289,23 @@ class AndExoPlayerView(
}
}

fun setPlayWhenReady(playWhenReady: Boolean = false) {
this.currPlayWhenReady = playWhenReady
player.playWhenReady = playWhenReady
}

fun pausePlayer() {
player.playWhenReady = false
player.playbackState
}

fun stopPlayer() {
player.stop()
}

fun startPlayer() {
player.playWhenReady = true
player.playbackState
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ interface AndExoPlayerListener {

fun onExoPlayerError(errorMessage: String?) {}

fun onExoBuffering() {}

fun onExoEnded() {}

fun onExoIdle() {}

fun onExoReady() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PublicValues {
const val TEST_URL_MP4_V3 = "https://www.rmp-streaming.com/media/big-buck-bunny-360p.mp4"
const val TEST_URL_MP4_V4 = "https://backyard.vaslapp.com/res/036e77f0-a94e-4165-9832-1a8e39c1e089/USER_FILES/6084024b90c61d0001471b0c/1619507956737/file.mp4"
const val TEST_URL_MP4_V5 = "https://backyard.vaslapp.com/res/036e77f0-a94e-4165-9832-1a8e39c1e089/USER_FILES/6084024b90c61d0001471b0c/1619507956737/file.mp4"
const val TEST_URL_MP4_V6 = "https://av.arvanvod.com/YKo7MPRwg0/0rGzR1oPkl/origin_t7pWbw6lZuzYGeEgHdiWOsngBpLnE6rb4pfKAU30.mp4"

const val TEST_URL_HLS = "https://content.jwplatform.com/manifests/yp34SRmf.m3u8"

Expand Down

0 comments on commit f04941a

Please sign in to comment.