【问题标题】:video streaming on Fragment of an android appandroid应用程序片段上的视频流
【发布时间】:2021-06-28 00:55:45
【问题描述】:

我想在我的 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$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)

【问题讨论】:

  • 请添加您在应用程序崩溃时遇到的错误。另外,我没有看到您将视图设置为播放器。 exoPlayer.setView(playerView);.在准备播放器之前试试这个。
  • 如果应用程序崩溃,会有一个堆栈跟踪。请在 logcat 上查找,并将其添加到您的问题中。请回复@AlexMamo
  • @AkshayShenoy exoPlayer.setView(playerView) 我认为不存在,但 playerView.setPlayer(exoPlayer) 存在。
  • @AlexMamo,当应用程序在 logcat 中崩溃时,我没有任何特定的错误行,所以我添加了运行错误。如果这还不够,我会尝试复制粘贴更多的 logcat 行。
  • 感谢您的帮助

标签: java android firebase kotlin video-streaming


【解决方案1】:

您收到以下错误:

java.lang.ClassCastException: androidx.cardview.widget.CardView 不能
转换为 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多