android 应用的片段上的视频流

video streaming on Fragment of an android app

我想在我的 android 应用程序的片段中流式传输来自 firebase 存储的视频。但是即使我尝试我请求片段时我的视频也无法启动或应用程序崩溃。作为初学者,我遵循了 tuto,所以我使用了 exoplayer。 这是我的片段代码:

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;
}}

沉迷于 :

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());
    }

}}

xml 文件看起来像:

<?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>

欢迎在 java 或 kotlin 中提出任何建议。

如果这可能有帮助,我在运行面板中遇到了这些错误:

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.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)

您收到以下错误:

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

因为在你的“PubliciteFragment”中你声明了一个变量:

private RecyclerView recyclerView;

就是这样实例化的:

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

这是正确的,因为在您的“fragment_publicite”布局文件中,具有“recyclerview”id 的视图实际上是一个 CardView,不是 RecyclerView,因此错误:

要解决此问题,您需要更改 XML 文件中 CardView 的 ID 来自:

android:id="@+id/recyclerview"

收件人:

android:id="@+id/card_view"

添加一个新的变量声明:

private CardView cardView;

并像这样实例化它:

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

老实说,我在你的 XML 文件中看不到 RecyclerView。假设有一个,原来没有添加,要找到那个,请使用以下代码行:

recyclerView = cardView.findViewById(R.id.recycler_view);