Ryan Kimber
Ryan Kimber

Reputation: 19

How to get a video to play in a recyclerview?

I have spent all day trying to figure this out using libraries and everything but I can't seem to implement a feature that would seem easy.

My end goal here is to get videos to display in a recyclerview as this part of my app will have a 'videos' section.

I will attach my current code and if anyone could help i'd appreciate it all I am really using here is intent extras to get and pull data.

Adapter.java

public class WerecommendAdapter extends RecyclerView.Adapter<WerecommendAdapter.MyHolder> {

private final List<Businesses> mData;
private Context mcontext;
private CategoryCardActivity cca;


public WerecommendAdapter(Context mcontext, List<Businesses> mData) {
    this.mData = mData;
    this.mcontext = mcontext;
}

public WerecommendAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    mcontext = parent.getContext ();
    View view = LayoutInflater.from ( mcontext ).inflate ( R.layout.recommend_card_design,
            parent, false );


    return new WerecommendAdapter.MyHolder ( view );
}

@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {

    Businesses temp = mData.get ( position );
    // Dymamically Generating card background colours
    Random rnd = new Random ();
    int currentColour = Color.argb ( 255, rnd.nextInt ( 256 ), rnd.nextInt ( 256 ), rnd.nextInt ( 256 ) );
    holder.cardview.setBackgroundColor ( currentColour );

    holder.businessname.setText ( mData.get ( position ).getBusiness_name () );
    Glide.with ( mcontext )
            .asBitmap ()
            .load ( mData.get ( position ).getBusiness_img () )
            .diskCacheStrategy ( DiskCacheStrategy.ALL )
            .into ( holder.businesslogo );
    holder.cardview.setOnClickListener ( v -> {
        Intent intent = new Intent ( mcontext, Solocompany.class );
        intent.putExtra ( "bus_name", temp.getBusiness_name () );
        intent.putExtra ( "bdesc", temp.getBdesc () );
        intent.putExtra ( "Image_URL", temp.getBusiness_img () );
        intent.putExtra ( "contact", temp.getContact () );
        intent.putExtra ( "website", temp.getBusiness_website () );
        intent.putExtra ( "website_social", temp.getBusiness_website_social () );
        intent.putExtra ( "careers", temp.getCareers () );
        intent.putExtra ( "videos", temp.getVideos () );
        mcontext.startActivity ( intent );

        } );


}

@Override
public int getItemCount() {
    cca = new CategoryCardActivity ();
    return mData.size ();
}


public class MyHolder extends RecyclerView.ViewHolder {

    TextView category;
    RelativeLayout werecommend;
    TextView businessname;
    ImageView businesslogo;
    LinearLayout cardview;

    VideoView videoView;



    public MyHolder(@NonNull View itemView) {
        super ( itemView );
        category = itemView.findViewById ( R.id.category );
        werecommend = itemView.findViewById ( R.id.werecommendrecycler );
        businessname = itemView.findViewById ( R.id.businessname );
        businesslogo = itemView.findViewById ( R.id.business_image );
        cardview = itemView.findViewById ( R.id.cardview );
        videoView = itemView.findViewById ( R.id.videorecycler );



    }
}

}

Where Intent goes ---->

public class Solocompany extends AppCompatActivity {
private static final String TAG = "SOLO company";
ImageView business_logo;
List<Businesses> businessesList;
Button website, social, careers, contact;
TextView business_name;
TextView bdesc;
VideoView videoView;



private Context mContext;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.company_solo_page );
    businessesList = new ArrayList<> ();
    business_name = findViewById ( R.id.bus_name );
    business_logo = findViewById ( R.id.profile_image );
    bdesc = findViewById ( R.id.bdesc );
    website = findViewById ( R.id.website );
    social = findViewById ( R.id.socialmedia );
    careers = findViewById ( R.id.careers );
    contact = findViewById ( R.id.contact );
    videoView = findViewById ( R.id.videorecycler );
    videoView.setVideoURI ( Uri.parse ( getIntent ().getStringExtra ( "videos" ) ) );

    business_name.setText ( getIntent ().getStringExtra ( "bus_name" ) );
    bdesc.setText ( getIntent ().getStringExtra ( "bdesc" ) );
    Glide.with ( this )
            .load ( getIntent ().getStringExtra ( "Image_URL" ) )
            .into ( business_logo );
    contact.setOnClickListener ( this::contact );

Any help would be appreciated

----------------- UPDATE 22:34 ---------------

I am getting this error: Caused by: java.lang.ClassCastException: androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.VideoView

Upvotes: 0

Views: 1564

Answers (1)

Ole Pannier
Ole Pannier

Reputation: 3673

I see a possibility to get a workaround. You can set up the RecyclerView like this project reffers. Then implement "glide" (usefull widget to get picture and GIFs from Internet site or servers) I guess what it can do is playing thumbnail that you access from GIFs. So you implement the link from your GIF (as preview video) and Glide will play it. It wont play videos but GIFs.

I know this isn't exactly what you wanted, but better then telling you there is nothing around. Hope that helps. Cheers!

Upvotes: 1

Related Questions