首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在RecyclerView中无法播放视频或音频的VideoView

在RecyclerView中无法播放视频或音频的VideoView
EN

Stack Overflow用户
提问于 2015-12-12 03:17:21
回答 2查看 9.3K关注 0票数 9

嗨,我正在尝试创建一个视频日志片段,它有一个回收视图,其中包含标题,情绪和每个列表项目中的视频。由于某些原因,videoview甚至没有出现。videofilePath保存正确,我已经使用println语句进行了检查。我没有收到任何错误,但它只是完全崩溃。下一个列表项在视频显示之前开始。

这是我的ViewHolder类

代码语言:javascript
运行
复制
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();
    }
}

}

下面是我的适配器类

代码语言:javascript
运行
复制
public class JournalRecyclerViewAdapter extends RecyclerView.Adapter<JournalViewHolder> {

专用列表mContext;专用上下文列表;

代码语言:javascript
运行
复制
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();
}

}

下面是我的类,它初始化项目列表和回收器视图

代码语言:javascript
运行
复制
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;
}

}

下面是我的XML文件。

代码语言:javascript
运行
复制
<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">

列表项文件

代码语言:javascript
运行
复制
<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"/>

EN

回答 2

Stack Overflow用户

发布于 2015-12-19 23:56:54

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

票数 1
EN

Stack Overflow用户

发布于 2017-02-26 13:09:21

我正在开发一个名为OneItem的库,它可能会对您有所帮助。

https://github.com/tbrand/OneItem

#selectItemAt(int position)中,开始使用任意VideoView(如MediaPlayer或ExoPlayer)播放视频。

#UnSelectItemAt(int position)中,停止播放视频并释放它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34230944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档