Skip to content
Advertisement

video streaming on Fragment of an android app

i want to stream a video from the firebase storage, in a fragment of my android app. but what even i try my video doesn’t launch or the app crash when i request the fragment. being a beginner, i have followed a tuto so i used exoplayer. this is my fragment code:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;



public class PubliciteFragment extends Fragment  {
private RecyclerView recyclerView;

@Override
public View onCreateView(
        LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState
){
    View view = inflater.inflate(R.layout.fragment_publicite,container,false);
    recyclerView = view.findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));

    return view;
}}

in addiction with :

import android.app.Application;
import android.net.Uri;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;

public class RecyclerViewHolder extends RecyclerView.ViewHolder {
   SimpleExoPlayer exoPlayer;
   PlayerView playerView;
public RecyclerViewHolder(@NonNull View itemView){
    super(itemView);
}

public void setExoPlayer(Application application){
    playerView = itemView.findViewById(R.id.exoplayer_item);
    try {
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(application).build();
        //TrackSelector trackSelector = new DefaultTrackSelector(new 
        AdaptiveTrackSelection.Factory(bandwidthMeter))
        exoPlayer = (SimpleExoPlayer) ExoPlayerFactory.newSimpleInstance(application);
        Uri video = Uri.parse("https://firebasestorage.googleapis.com/v0/b/gombo03- 
        450bb.appspot.com/o/Forza%20Horizon%204%202020-07-01%2018-16-35_Trim.mp4? 
        alt=media&token=443a28c2-0838-4957-a6cf-86093772a90d");
        DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("video");
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        MediaSource mediaSource = new 
        ExtractorMediaSource(video,dataSourceFactory,extractorsFactory,null,null);
        playerView.setPlayer(exoPlayer);
        exoPlayer.prepare(mediaSource);
        exoPlayer.setPlayWhenReady(false);
    }catch (Exception e){
        Log.e("PubliciteFragment", "exoplayer error"+e.toString());
    }

}}

And the xml file look like:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerview"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
app:cardCornerRadius="2dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<com.google.android.exoplayer2.ui.PlayerView
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:id="@+id/exoplayer_item"
    android:layout_margin="3dp"
    app:use_controller="true"
    app:resize_mode="fill"/>

</LinearLayout>

</androidx.cardview.widget.CardView>

any advice in java or kotlin is welcome.

if this may help, i got these errors in the runs panel :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.Gombo0_3, PID: 27945
java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be     
cast to androidx.recyclerview.widget.RecyclerView
    at com.Gombo0_3.PubliciteFragment.onCreateView(PubliciteFragment.java:23)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
    at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Advertisement

Answer

You are getting the following error:

java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be
cast to androidx.recyclerview.widget.RecyclerView

Because in your “PubliciteFragment” you have declared a variable:

private RecyclerView recyclerView;

That is instantiated like this:

recyclerView = view.findViewById(R.id.recyclerview);

And this is not correct because, in your “fragment_publicite” layout file, the View with the “recyclerview” id is actually a CardView and not a RecyclerView, hence the error:

enter image description here

To solve this, you need to change the ID of the CardView in the XML file from:

android:id="@+id/recyclerview"

To:

android:id="@+id/card_view"

Add a new variable declaration:

private CardView cardView;

And instantiate it like this:

cardView = view.findViewById(R.id.card_view);

Honestly, I cannot see in your XML a file a RecyclerView. Suppose there is one, which was not originally added, to find that, please use the following lines of code:

recyclerView = cardView.findViewById(R.id.recycler_view);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement