Unable to view videoview in realtime database using firebase realtime database - android

I want to view VideoView in RecyclerView. I am able to get the Uri of the uploaded videos but I am unable to play that video in the VideoView. I have checked that the I am getting the correct link of the video.
Here is my adapter code:
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 mediaController = MediaController(mContext)
mediaController.setAnchorView(holder.videoIv)
holder.videoIv.setMediaController(mediaController)
holder.videoIv.setVideoURI(videoid.getVideoUrl().toUri())
holder.videoIv.requestFocus()
holder.videoIv.start()
}
override fun getItemCount(): Int {
return mvideos.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var videoIv: VideoView = itemView.videos_ret
var vidId: TextView = itemView.videos_id_ret
}
}
I tried to use this adapter to get videos in VideoView but it is not playing any video.

Related

Pause previous video on Scroll of RecyclerView in Android Kotlin

`I am using Recycler View and inside the recycler view i am using the exo player
I want pause video on scroll on recycler view but the multiple videos are playing
Main Activity Code
RecyclerViewAdapter Code
class exoadapter(val myList: ArrayList, val context: Context) :
RecyclerView.Adapter<exoadapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val player = itemView.findViewById<StyledPlayerView>(R.id.exo_player)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.exo_item, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return myList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val curr = myList[position].url
// holder.player.
val player: ExoPlayer = ExoPlayer.Builder(context).build()
holder.player.player = player
player.repeatMode = Player.REPEAT_MODE_ALL
player.pauseAtEndOfMediaItems = true
// player.playWhenReady =true
//player.addListener(listner)
// Build the media item.
// Build the media item.
// val videoUri="https://html5demos.com/assets/dizzy.mp4"
// val videoUri = "https://bdiskv1.shunyaekai.com/posts/167395470138763c6845bb89273efdf60e0bf.mp4"
val mediaItem: MediaItem = MediaItem.fromUri(curr)
player.prepare()
player.play()
}
}
I want pause video on scroll on recycler view but the multiple videos are playing I am using Recyclerview

when Playing the video background, report android.view.WindowManager$BadTokenException:Unable to add window-token null is not valid;

I am trying to play videos in VideoView in the recyclerView using realTime database.
Here is my adapter code of the recyclerView. :-
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 videolink = Uri.parse(videoid.getVideoUrl())
val mediaController = MediaController(mContext)
mediaController.setAnchorView(holder.videoIv)
holder.videoIv.setMediaController(mediaController)
holder.videoIv.setVideoURI(videolink)
holder.videoIv.requestFocus()
holder.videoIv.start()
}
override fun getItemCount(): Int {
return mvideos.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var videoIv: VideoView = itemView.videos_ret
var vidId: TextView = itemView.videos_id_ret
}
}
After using this code. I am getting this error.
android.view.WindowManager$BadTokenException: Unable to add window --
token null is not valid; is your activity running? at
android.view.ViewRootImpl.setView(ViewRootImpl.java:1444) at
android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:469)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
at android.widget.MediaController.show(MediaController.java:384)
at android.widget.MediaController.show(MediaController.java:334)
at android.widget.VideoView$2.onPrepared(VideoView.java:521)
at
android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:4228)
at android.os.Handler.dispatchMessage(Handler.java:106) at
android.os.Looper.loop(Looper.java:246) at
android.app.ActivityThread.main(ActivityThread.java:8653) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
It plays the video for a few seconds in the background after crashing before closing the app.
someone, please help me.
The view added to the window must have a token, in your situation, it is the activity. but when your application is in the background, it may be destroyed by the system. you can add log info in the activity onDestory method
in most scenes, when the activity lifecycle is in onPause, the video play should pause. if you have to play it, you should change the way you use it.
it is PIP,
you can also use some third library.
PiP leverages the multi-window APIs made available in Android 7.0 to provide the pinned video overlay window. To add PiP to your app, you need to register your activities that support PiP, switch your activity to PiP mode as needed, and make sure UI elements are hidden and video playback continues when the activity is in PiP mode.
The PiP window appears in the topmost layer of the screen, in a corner chosen by the system.
Instead of VideoView use Exoplayer:-
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
}
}

How to put videos in recyclerview from firebase storage using android kotlin?

I have used the similar code that I use to get Images into the recyclerview from firebase storage. Here is the code of the adapter.:-
class VideoAdapter (private var mContext: Context,
private var mvideos: List<VID>,
private var isActivity: Boolean = false) : RecyclerView.Adapter<VideoAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoAdapter.ViewHolder {
val view = LayoutInflater.from(mContext).inflate(R.layout.videos_adapter, parent, false)
return VideoAdapter.ViewHolder(view)
}
override fun onBindViewHolder(holder: VideoAdapter.ViewHolder, position: Int) {
val videoid = mvideos[position]
holder.VidId.text = videoid.getilp()
val mediaController = MediaController(mContext)
mediaController.setAnchorView(holder.VideoIv)
VideoIv.setMediaController(mediaController)
VideoIv.setVideoURI(videoid.getVideoUrl())
VideoIv.requestFocus()
VideoIv.pause()
}
override fun getItemCount(): Int {
return mvideos.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var VideoIv: VideoView = itemView.videos_ret
var VidId: TextView = itemView.videos_id_ret
}
}
while using this code I am unable to VideoIv is unable to setmediacontroller and setvideouri. The data that I am getting is of URI type. Can someone please tell me how to get videos in my recyclerview?
Instead of VideoIv. Use holder.VideoIv
val mediaController = MediaController(mContext)
mediaController.setAnchorView(holder.VideoIv)
holder.VideoIv.setMediaController(mediaController)
holder.VideoIv.setVideoURI(videoid.getVideoUrl())
holder.VideoIv.requestFocus()
holder.VideoIv.pause()

How can I play mp3 from recyclerview using Kotlin?

so I am trying to play an audio file from the raw folder by using a button that found in recyclerview but I tried everything and nothing seems to work for me
can you please help me and thanks in advance
P.C. this is a sample just to Know how to add an audio file in a recyclerview hopefully I will be adding more so if you have any tutorials on to add progress bar, image buttons... etc in recyclerview please share it with me
here is my progress so far
my Recyclerview Adater
class shaikh( val context: Context, val names: ArrayList<String>, val images: ArrayList<Int>): RecyclerView.Adapter<ViewHolder>() {
private var mediaPlayer: MediaPlayer? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val callXml = layoutInflater.inflate(R.layout.quranvoice,parent, false)
return ViewHolder(callXml)
}
override fun getItemCount(): Int {
return names.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.ShaikhName.text = names.get(position)
holder.itemView.Shaikh.setImageResource(images.get(position))
holder.itemView.setOnClickListener {
when(position){
0->{
holder.itemView.voice.setOnClickListener {
if (mediaPlayer != null){
mediaPlayer = MediaPlayer.create(context, R.raw.abdelbasset)
mediaPlayer?.start()
}
}
}
1->{
Toast.makeText(context,"الشيخ ناصر",Toast.LENGTH_LONG).show()
}
}
}
}
}
public class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
fun ViewHolder( ){
}
}
my Activity
var texts: ArrayList<String> = ArrayList()
var images : ArrayList<Int> = ArrayList()
// nothing is in here except the onCreate method
val Adapter = shaikh(this, texts, images)
QuranVoice.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, true)
QuranVoice.setHasFixedSize(true)
QuranVoice.setAdapter( Adapter)
QuranVoice.addOnScrollListener(CenterScrollListener())
loadData()
private fun loadData() {
texts.add("الشيخ عبدالباسط عبدالصمد - رحمه الله-")
images.add(R.drawable.abdelbasset_abdessamad)
texts.add("الشيخ خالد مغمسي")
images.add(R.drawable.ic_esha_time)
texts.add("الشيخ خالد مغمسي")
images.add(R.drawable.ic_dhur_time)
}

RecyclerAdapter is creating multiple view back to back

I am using recyclerview to display images in cardview. When I load images from the url. It loads well from first time but when It loads for second time it is not displayed properly.
How to fix it? Here is the code
override fun getItemCount(): Int {
dataList = FlickrDBOperation.dataArray
if (dataList!!.isEmpty())
return 0
Log.e("count",dataList!!.size.toString())
return dataList!!.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
var itemView: View = LayoutInflater.from(parent.context).inflate(R.layout.imagelist_row,parent,false)
var viewHolder: FlickrAdapter.Holder = FlickrAdapter.Holder(itemView)
return viewHolder
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.image_name.text = dataList!!.get(position).name
Picasso.with(context).load(dataList!!.get(position).preUrl).into(holder.row_image)
}
class Holder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
val row_image: ImageView
val image_name: TextView
init {
row_image = itemView!!.findViewById<ImageView>(R.id.row_image)
image_name = itemView!!.findViewById<TextView>(R.id.image_name)
}
}

Categories

Resources