简体   繁体   English

VideoView 未在 RecyclerView 中播放视频或音频

[英]VideoView not playing video or audio in RecyclerView

Hi I am trying to create a video journal fragment that has a recyclerview which contains a title, a mood, and a video in each list item.嗨,我正在尝试创建一个视频日志片段,该片段具有一个 recyclerview,其中包含每个列表项中的标题、心情和视频。 For some reason the videoview is not even showing up.由于某种原因,视频视图甚至没有出现。 The videofilePath is saving correctly and I've checked with println statements. videofilePath 正确保存,我已经检查了 println 语句。 I'm not getting any errors, but its just completely collapsed.我没有收到任何错误,但它只是完全崩溃了。 The next list item starts before the video displays.下一个列表项在视频显示之前开始。

Here is my ViewHolder class这是我的 ViewHolder 类

public class JournalViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private TextView mood;
private VideoView mVideoView;
MediaController mMediaController;
Context mContext;

public JournalViewHolder(View view, Context context){
    super(view);
    mContext = context;
    title = (TextView)view.findViewById(R.id.JournalTitle);
    mood = (TextView)view.findViewById(R.id.JournalMood);
    mVideoView = (VideoView)view.findViewById(R.id.JournalVideo);
    mMediaController = new MediaController(context);
}

public void bind(JournalEntry journalEntry){
    title.setText(journalEntry.getTitle());
    mood.setText(journalEntry.getMood());
    if(journalEntry.getVideoFileName() != null){
        Uri uri  = Uri.parse(journalEntry.getVideoFileName());
        mVideoView.setVideoURI(uri);
        mVideoView.requestFocus();
        mVideoView.setMediaController(mMediaController);
        mVideoView.setZOrderOnTop(true);
        mVideoView.start();
    }
}

} }

Here is my adapter class这是我的适配器类

public class JournalRecyclerViewAdapter extends RecyclerView.Adapter<JournalViewHolder> {

private List mJournalEntries;私人列表 mJournalEntries; private Context mContext;私有上下文 mContext;

public JournalRecyclerViewAdapter(List<JournalEntry> entries, Context context){
    mJournalEntries = entries;
    mContext = context;
}

@Override
public JournalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.journal_list_items, parent, false);
    JournalViewHolder holder = new JournalViewHolder(view, mContext);

    return holder;
}

@Override
public void onBindViewHolder(JournalViewHolder holder, int position) {
    JournalEntry entry = mJournalEntries.get(position);
    holder.bind(entry);
}

@Override
public int getItemCount() {
    return mJournalEntries.size();
}

} }

Here is my class that initiates the list of items and the recyclerview这是我的课程,它启动项目列表和回收站视图

public class JournalView extends FragmentLoggingLifeCycler {
private RecyclerView mRecyclerView;
DataAccess mDataAccess;
List<JournalEntry> mEntryList;
public static final String USERNAME_KEY = "username";
public static final String PASSWORD_KEY = "password";
String password;
String username;
User currentUser;
private Context mContext;

public JournalView() {
    // Required empty public constructor
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = activity.getApplicationContext();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_journal_view, container, false);
    Bundle data = getArguments();
    username = data.getString(USERNAME_KEY);
    password = data.getString(PASSWORD_KEY);
    LinearLayoutManager manager = new LinearLayoutManager(getActivity());
    mDataAccess = DataAccess.getDataAccess(container.getContext());
    currentUser = mDataAccess.getLoginUser(username, password);


     mEntryList = mDataAccess.getAllJournals();
        //mDataAccess.mDatabase.delete(Scheme.Journal.NAME, null, null);
//        mEntryList = trimList(mEntryList, currentUser.getUsername());
//        for(int counter = 0; counter < mEntryList.size(); counter++){
//            System.out.println(mEntryList.get(counter).getTitle());
//        }
        mRecyclerView = (RecyclerView)view.findViewById(R.id.JournalList);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(new JournalRecyclerViewAdapter(mEntryList, mContext));
        return view;
    }

private static List<JournalEntry> trimList(List<JournalEntry> entries, String username){
    List<JournalEntry> returnedList = new ArrayList<>();

    for(int i = 0; i< returnedList.size(); i++){
        System.out.println(entries.get(i).getUsername()+ ", " +username);
        if(entries.get(i).getUsername().equals(username)){
            returnedList.add(entries.get(i));
        }
    }
    return returnedList;
}

} }

and now here are my XML files.现在这是我的 XML 文件。

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/JournalList">

List Item files列出项目文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="2">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:text="@string/Title"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/JournalTitle"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:text="@string/Mood"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/JournalMood"/>

</LinearLayout>
<VideoView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/JournalVideo"
    android:visibility="visible"/>

查看此示例代码,了解如何在 RecyclerView VideoInRecyclerViewExamples 中播放视频

I'm developing a library named OneItem that may help you.我正在开发一个名为 OneItem 的库,可以帮助您。
https://github.com/tbrand/OneItem https://github.com/tbrand/OneItem

In #selectItemAt(int position) , start playing video with your arbitrary VideoView(such as MediaPlayer or ExoPlayer).#selectItemAt(int position) ,开始使用任意 VideoView(例如 MediaPlayer 或 ExoPlayer)播放视频。
In #UnSelectItemAt(int position) , stop playing video and release it.#UnSelectItemAt(int position) ,停止播放视频并释放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM