'I want the player running above to stop when something is clicked (include) in the recyclerview below
I want the player running above to stop when something is clicked (include) in the recyclerview below. But I couldn't.
Author Detail Fragment :
@AndroidEntryPoint
class AuthorDetailFragment : BaseAksamFragment<FragmentAuthorDetailBinding, AuthorDetailViewModel>(), AuthorNavigator {
override val layoutResourceId: Int = R.layout.fragment_author_detail
//private var lastSelectedPosition: Int = -1
var selectedHolder: AllArticlesAdapter.AuthorsPageHolder? = null
//private var selectedBinding: ItemAuthorOtherArticlesBinding? = null
private var audioPlayer: SimpleExoPlayer? = null
private var countDownTimer: CountDownTimer? = null
private var flPlayPause: FrameLayout? = null
private var exoProgressBar: ProgressBar? = null
private var exoRemainingTime: TextView? = null
private var inc_other_articles: LinearLayout? = null
override val classTypeOfViewModel: Class<AuthorDetailViewModel> = AuthorDetailViewModel::class.java
private val columnId by lazy {
arguments?.getString(KeyConstants.columnIdKey)!!
}
private val authorOtherArticlesAdapter = AuthorOtherArticlesAdapter(this)
@RequiresApi(Build.VERSION_CODES.M)
override fun init() {
inc_other_articles?.setOnClickListener(){
audioPlayer?.stop()
//Player.STATE_BUFFERING
//stopMediaPlayer(true)
}
binding.vmAuthorDetail = viewModel
binding.authorNavigator = this
binding.incOtherArticles.recyclerNewsOthers.adapter = authorOtherArticlesAdapter
binding.webViewAuthorDetail.webViewClient = webViewClient
binding.btnListen.setOnClickListener {
val columnist = binding.columnData?.columnist
Log.d("olley", "${columnist?.title}-${columnist?.podcastsound ?: "-/-"}")
flPlayPause = binding.playerView.findViewById(R.id.exo_fl_play_pause)
exoProgressBar = binding.playerView.findViewById(R.id.exo_progress_bar)
exoRemainingTime = binding.playerView.findViewById(R.id.exo_tv_remaining_time)
stopMediaPlayer(true)
//if (lastSelectedPosition != -1)
if (columnist != null) {
binding.playerView.visibility = View.VISIBLE
initPlayer(columnist.podcastsound.orEmpty())
}
}
}
private fun initPlayer(audioFile: String) {
val remainingTimeAsSecond = audioFile
.substringAfter("?time=")
.filter { it.isDigit() }.toLongOrNull().orZero()
.times(1000)
exoRemainingTime?.text = UtilityCommon.timeFormatForGivenSecond(remainingTimeAsSecond)
binding?.btnListen?.isVisible = false
audioPlayer = context?.let {
SimpleExoPlayer.Builder(it)
.build().apply {
setMediaItem(MediaItem.fromUri(audioFile))
playWhenReady = true
addListener(playerListener)
prepare()
}
}
binding?.playerView?.run {
isVisible = true
showController()
player = audioPlayer
}
}
private val playerListener: Player.EventListener = object : Player.EventListener {
override fun onPlaybackStateChanged(state: Int) {
when (state) {
Player.STATE_BUFFERING -> {
flPlayPause?.visibility = View.INVISIBLE
exoProgressBar?.visibility = View.VISIBLE
}
Player.STATE_READY -> {
flPlayPause?.visibility = View.VISIBLE
exoProgressBar?.visibility = View.GONE
}
Player.STATE_IDLE -> {
exoProgressBar?.visibility = View.GONE
flPlayPause?.visibility = View.VISIBLE
Toast.makeText(context, "Podcast yüklenirken bir hata meydana geldiiiii test.", Toast.LENGTH_SHORT).show()
}
Player.STATE_ENDED -> return
}
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
if (isPlaying) {
audioPlayer?.run {
val remainingTime = (duration - currentPosition)
countDownTimer(remainingTime)
}
} else {
countDownTimer?.cancel()
}
}
}
private fun countDownTimer(remainingTime: Long) {
countDownTimer?.cancel()
countDownTimer = null
countDownTimer = object : CountDownTimer(remainingTime, 1000) {
override fun onTick(millisUntilFinished: Long) {
audioPlayer?.run {
val remainingTimeAsSeconds = UtilityCommon.timeFormatForGivenSecond(millisUntilFinished)
exoRemainingTime?.text = remainingTimeAsSeconds
}
}
override fun onFinish() {}
}.start()
}
private fun releasePlayer() {
if (audioPlayer != null) {
audioPlayer!!.removeListener(playerListener)
audioPlayer!!.release()
audioPlayer = null
binding?.playerView?.player = null
}
}
fun stopMediaPlayer(fromViewHolder: Boolean = false) {
exoRemainingTime?.text = context?.getString(R.string.default_remaining_time)
countDownTimer?.cancel()
releasePlayer()
}
override fun initStartRequest() {
viewModel.startRequest(columnId)
}
override fun setupObservers() {
observeColumnDetail()
}
private val webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
binding.progressBar.isVisible = true
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
binding.progressBar.isVisible = false
}
}
private fun observeColumnDetail() {
viewModel.authorDetail.observe(viewLifecycleOwner, {
when (it) {
Resource.Loading -> viewModel.updateServiceState(ApiStateEnum.LOADING)
is Resource.Error -> viewModel.updateServiceState(ApiStateEnum.ERROR)
is Resource.Success -> {
binding.columnData = it.data.data?.column
it.data.data?.itemList?.forEach { d->
Log.d("detaytest", "${d.itemId} ${d.title} ${d.podcastsound}")
}
authorOtherArticlesAdapter.submitList(it.data.data?.itemList)
viewModel.updateServiceState(ApiStateEnum.SUCCESS)
}
}
})
}
override fun onReadNewsRequest(columnId: String?) {
CommonNavigate.navigateToAuthorDetail(this, columnId)
}
override fun onAuthorsPage(columnId: String?) {
CommonNavigate.navigateToAuthorsPage(this, columnId)
}
override fun onPause() {
super.onPause()
authorOtherArticlesAdapter.selectedHolder?.pauseMediaPlayer()
}
override fun onDestroyView() {
super.onDestroyView()
authorOtherArticlesAdapter.selectedHolder?.stopMediaPlayer()
}
}
XML :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="vmAuthorDetail"
type="com.aksam.android.ui.authors.detail.AuthorDetailViewModel" />
<variable
name="columnData"
type="com.common.library.data.model.aksam.AuthorDetailModel.Data.Column" />
<variable
name="authorNavigator"
type="com.aksam.android.ui.authors.AuthorNavigator" />
<import type="com.common.library.data.enums.ApiStateEnum" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollAuthorDetail"
visibleGone="@{vmAuthorDetail.serviceState == ApiStateEnum.SUCCESS}"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="@dimen/dp10"
android:layout_marginTop="@dimen/dp10">
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/author_image"
imageUrl="@{columnData.columnist.imageUrl}"
android:layout_width="@dimen/author_detail_image_size"
android:layout_height="@dimen/author_detail_image_size"
android:scaleType="fitXY"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/img_galery_test" />
<TextView
android:id="@+id/author_name"
setTextGone="@{columnData.columnist.columnistName}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/dp10"
android:layout_marginTop="@dimen/dp5"
android:fontFamily="@font/roboto"
android:textColor="@color/color_33"
android:textSize="@dimen/sp20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/author_image"
app:layout_constraintTop_toTopOf="@id/author_image"
tools:text="Turgay Güler" />
<TextView
android:id="@+id/author_title"
setTextGone="@{columnData.columnist.title}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/dp10"
android:layout_marginTop="@dimen/dp5"
android:fontFamily="@font/roboto_black"
android:textColor="@color/color_33"
android:textSize="@dimen/sp24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/author_image"
app:layout_constraintTop_toBottomOf="@id/author_name"
app:layout_constraintVertical_bias=".0"
tools:text="Çoğunuz unutmuştur yahut hiç bilmiyordur" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="author_image,author_title" />
<Button
android:id="@+id/author_all_articles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="@dimen/dp10"
android:fontFamily="@font/roboto_bold"
android:onClick="@{()->authorNavigator.onAuthorsPage(columnData.columnist.itemId)}"
android:paddingHorizontal="@dimen/dp10"
android:text="@string/author_bt_all_article"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier1" />
<Button
android:id="@+id/btn_listen"
style="?android:attr/buttonBarStyle"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp45"
android:fontFamily="@font/roboto_black"
android:text="@string/authors_listen_article"
android:textSize="@dimen/sp14"
app:icon="@drawable/ic_listen_btn"
app:layout_constraintEnd_toEndOf="@+id/author_title"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toEndOf="@+id/author_image"
app:layout_constraintTop_toBottomOf="@+id/author_title" />
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:controller_layout_id="@layout/exo_author_player_view"
app:hide_on_touch="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.916"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_listen"
app:show_timeout="0"
app:shutter_background_color="@color/white" />
<include
android:id="@+id/inc_share"
layout="@layout/common_inc_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/author_all_articles"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/author_all_articles"
bind:fullPath="@{columnData.fullPath}" />
<TextView
android:id="@+id/author_date"
setTextGone="@{columnData.publishDate}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp5"
android:fontFamily="@font/roboto_bold"
android:paddingHorizontal="@dimen/dp2"
android:textColor="@color/color_33"
android:textSize="@dimen/sp14"
app:layout_constraintEnd_toEndOf="@id/inc_share"
app:layout_constraintTop_toBottomOf="@id/author_all_articles"
tools:text="11 Mart 2021 Perşembe" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="author_all_articles,author_date" />
<WebView
android:id="@+id/webView_author_detail"
setWebViewGone="@{columnData.bodyText}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier2" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp10"
android:indeterminateTint="@color/red_d0001b"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/barrier2" />
<include
android:id="@+id/inc_other_articles"
layout="@layout/common_author_other_articles"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp20"
android:paddingBottom="@dimen/dp12"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/webView_author_detail" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<include
android:id="@+id/inc_error_page"
layout="@layout/common_error_loading_page"
bind:baseVm="@{vmAuthorDetail}"
tools:visibility="gone" />
</FrameLayout>
</layout>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
