Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error layout #41

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,30 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast

import androidx.activity.viewModels
import androidx.appcompat.widget.AlertDialogLayout
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.NavDirections
import androidx.navigation.fragment.navArgs
import androidx.navigation.navArgs
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import com.google.android.exoplayer2.Player.*
import com.google.android.exoplayer2.source.ExtractorMediaSource
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.hls.HlsDataSourceFactory
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory
import com.google.android.exoplayer2.upstream.HttpDataSource
import dagger.hilt.android.AndroidEntryPoint
import divyansh.tech.animeclassroom.R
import divyansh.tech.animeclassroom.ResultWrapper
import divyansh.tech.animeclassroom.databinding.FragmentPlayerBinding
import divyansh.tech.animeclassroom.home.HomeFragmentDirections
import divyansh.tech.animeclassroom.models.home.PlayerScreenModel
import divyansh.tech.animeclassroom.player.callbacks.PlayerControlListener
import kotlinx.android.synthetic.main.exo_player_custom_controls.*
Expand Down Expand Up @@ -77,18 +72,103 @@ class PlayerFragment: Fragment(), PlayerControlListener {
exo_track_selection_view.setOnClickListener { onButtonClicked(PlayerViewModel.PlayerClick.QUALITY_CONTROL) }
exo_speed_selection_view.setOnClickListener { onButtonClicked(PlayerViewModel.PlayerClick.SPEED_CONTROL) }
exo_full_Screen.setOnClickListener { onButtonClicked(PlayerViewModel.PlayerClick.FULLSCREEN_TOGGLE) }

binding.retry.setOnClickListener {
if (exoPlayer.playbackError != null)
exoPlayer.retry()
}
}

private fun initializePlayer(data: PlayerScreenModel) {
val trackSelector = DefaultTrackSelector()
val loadControl = DefaultLoadControl()
val renderersFactory = DefaultRenderersFactory(requireContext())

exoPlayer = ExoPlayerFactory.newSimpleInstance(requireContext(), renderersFactory, trackSelector, loadControl)
exoPlayer = ExoPlayerFactory.newSimpleInstance(
requireContext(),
renderersFactory,
trackSelector,
loadControl
)

exoPlayer.addListener(object : EventListener {
override fun onPlayerError(error: ExoPlaybackException?) {
super.onPlayerError(error)
when (error?.type) {
ExoPlaybackException.TYPE_OUT_OF_MEMORY -> {
exoplayerErrorLayoutChange(
visible = true,
image = R.drawable.ic_general_error,
errorText = R.string.status_code_default
)
}
ExoPlaybackException.TYPE_REMOTE -> {
exoplayerErrorLayoutChange(
visible = true,
image = R.drawable.ic_general_error,
errorText = R.string.server_error
)
}
ExoPlaybackException.TYPE_RENDERER -> {
exoplayerErrorLayoutChange(
visible = true,
image = R.drawable.ic_general_error,
errorText = R.string.status_code_default
)
}
ExoPlaybackException.TYPE_SOURCE -> {
exoplayerErrorLayoutChange(
visible = true,
image = R.drawable.ic_no_internet,
errorText = R.string.no_internet
)

}
ExoPlaybackException.TYPE_UNEXPECTED -> {
exoplayerErrorLayoutChange(
visible = true,
image = R.drawable.ic_general_error,
errorText = R.string.status_code_default
)
}
}
}

override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
super.onPlayerStateChanged(playWhenReady, playbackState)
when (playbackState) {
STATE_IDLE -> {

}
STATE_BUFFERING -> {
exoplayerErrorLayoutChange(visible = false)

}
STATE_READY -> {
exoplayerErrorLayoutChange(visible = false)
}
STATE_ENDED -> {
exoplayerErrorLayoutChange(visible = false)

}
}
}

})
binding.exoPlayerView.player = exoPlayer
play(data)
}

private fun exoplayerErrorLayoutChange(
visible: Boolean,
image: Int = R.drawable.ic_no_internet,
errorText: Int = R.string.no_internet
) {
binding.errorLayout.isVisible = visible
binding.errorImage.setImageResource(image)
binding.errorText.text = getString(errorText)
}

private fun play(data: PlayerScreenModel) {
exoPlayer.prepare(buildMediaSource(data.streamingUrl.toUri()))
exoPlayer.playWhenReady = true
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_general_error.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="32dp" android:tint="?attr/colorControlNormal"
android:viewportHeight="24" android:viewportWidth="24"
android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-2h2v2zM13,13h-2L11,7h2v6z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_no_internet.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="32dp" android:tint="?attr/colorControlNormal"
android:viewportHeight="24" android:viewportWidth="24"
android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M23.64,7c-0.45,-0.34 -4.93,-4 -11.64,-4 -1.5,0 -2.89,0.19 -4.15,0.48L18.18,13.8 23.64,7zM17.04,15.22L3.27,1.44 2,2.72l2.05,2.06C1.91,5.76 0.59,6.82 0.36,7l11.63,14.49 0.01,0.01 0.01,-0.01 3.9,-4.86 3.32,3.32 1.27,-1.27 -3.46,-3.46z"/>
</vector>
42 changes: 38 additions & 4 deletions app/src/main/res/layout/fragment_player.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/tools">
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/tools">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/videoPlayerContainer"
android:background="@android:color/black"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/error_layout"
android:visibility="gone"
android:elevation="20dp"
android:layout_height="match_parent">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/error_image"
android:src="@drawable/ic_no_internet"
app:tint="@color/white">
</ImageView>

<TextView
android:layout_width="wrap_content"
android:text="@string/no_internet"
android:textColor="@color/white"
android:id="@+id/error_text"
android:layout_height="wrap_content">
</TextView>
<TextView
android:layout_width="wrap_content"
android:id="@+id/retry"
android:textColor="@color/white"
android:text="@string/retry"
android:layout_height="wrap_content">

</TextView>
</LinearLayout>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<color name="progress">#e5e5e5</color>
<color name="secondary_background">#EAEAEB</color>
<color name="black">#191818</color>
<color name="white">#ffffff</color>

//Home Fragment
<color name="home_header">@color/header</color>
Expand Down