Issue
My adapter code is:-
class VideoAdapter(
private var mContext: Context,
private var mvideos: List<VID>
) : RecyclerView.Adapter<VideoAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(mContext).inflate(R.layout.videos_adapter, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val videoid = mvideos[position]
holder.vidId.text = videoid.getilp()
val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
val videoLink = Uri.parse(videoid.getVideoUrl())
val mediaSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(videoLink))
holder.mPlayer = SimpleExoPlayer.Builder(mContext).build()
holder.playerView.player = holder.mPlayer
holder.mPlayer!!.playWhenReady = true
holder.mPlayer!!.setMediaSource(mediaSource)
holder.mPlayer!!.stop()
}
override fun getItemCount(): Int {
return mvideos.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var mPlayer: SimpleExoPlayer? = null
var playerView: PlayerView = itemView.videos_ret
var vidId: TextView = itemView.videos_id_ret
}
}
The activity code is :-
class videos : AppCompatActivity() {
var valueListener: ValueEventListener? = null
val wimagesRef = FirebaseDatabase.getInstance().getReference().child("videos")
private var recyclerView: RecyclerView? = null
private var videoAdapter: VideoAdapter? = null
private var vid1: MutableList<VID>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_videos)
recyclerView = videos
recyclerView?.setHasFixedSize(true)
recyclerView?.layoutManager = GridLayoutManager(this, 1)
vid1 = ArrayList()
videoAdapter = baseContext?.let { VideoAdapter(it, vid1 as ArrayList<VID>) }
recyclerView?.adapter = videoAdapter
wimagesRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (snapshot in dataSnapshot.children) {
videoAdapter?.notifyDataSetChanged()
val video = snapshot.getValue(VID::class.java)
if (video != null) {
videos.visibility = View.VISIBLE
(vid1 as ArrayList<VID>).add(video)
}
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
override fun onDestroy() {
super.onDestroy()
wimagesRef.removeEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
for (snapshot in snapshot.children) {
videoAdapter?.notifyDataSetChanged()
val videos = snapshot.getValue(VID::class.java)
if (videos != null)
(vid1 as ArrayList<VID>).clear()
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
}
Everthing is working fine but when I press back button, I came to my last activity but still the video continues to play in the background. The audio of the video still continues to play while I am in the other activity. I want to destroy that activity on backpress. Someone please help me.
Solution
Add this line to your onDestroy() function
StyledPlayerView.player.release()
Answered By - Vivian Alexander L. Nonglait
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.