麻烦的视频在播放Recyclerview [英] Trouble getting video to play on Recyclerview

查看:552
本文介绍了麻烦的视频在播放Recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView内VideoView。我想最终有视频列表上一个Recyclerview玩。我决定用一个视频开始的,然后移动到有多个视频。我似乎无法得到一个视频在Recyclerview玩。当我开始我的手机上的应用程序,我得到的是进度条。上prepared功能不会被调用的某些原因。这里是我的code。

I have a VideoView inside a RecyclerView. I want to eventually have a list of videos to play on a Recyclerview. I decided to start out with one video, then move on to having multiple videos. I can't seem to get one video to play in the Recyclerview. When I start the app on my phone, all I get is the progress bar. The onPrepared function is never called for some reason. Here's my code.

RecyclerActivity.java

RecyclerActivity.java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.RecyclerView;


public class RecyclerActivity extends ActionBarActivity {
  private RecyclerView mRecyclerView;
  private RecyclerView.Adapter mAdapter;
  private RecyclerView.LayoutManager mLayoutManager;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler);
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyAdapter(this);
    mRecyclerView.setAdapter(mAdapter);
  }


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_recycler, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
  }
}

MyAdapter.java

MyAdapter.java

import android.app.ProgressDialog;
import android.content.Context; 
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.TextView; 
import android.widget.VideoView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  private Context context;
  private ProgressDialog progressDialog;
  private MediaController mediaControls;
  public static class ViewHolder extends RecyclerView.ViewHolder{
    public VideoView mVideoView;
    public ViewHolder(View v){
        super(v);
        mVideoView = (VideoView) v.findViewById(R.id.video_view);
    }
  }

  public MyAdapter(Context mContext) {
    context = mContext;
    mediaControls = new MediaController(context);
    progressDialog = new ProgressDialog(context);
    progressDialog.setTitle("Random Video");
    progressDialog.setMessage("Loading...");
    progressDialog.setCancelable(false);
    progressDialog.show();
  }

  @Override
  public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_video_view, parent,false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
    try{
        holder.mVideoView.setMediaController(mediaControls);
        holder.mVideoView.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.kitkat));
    } catch (Exception e){
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    holder.mVideoView.requestFocus();
    holder.mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            progressDialog.dismiss();
            holder.mVideoView.start();
        }
    });
  }

  @Override
  public int getItemCount() {
    return 1;
  }

my_video_view.xml

my_video_view.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    tools:context=".RecyclerActivity">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
</FrameLayout>

activity_recycler.xml

activity_recycler.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".RecyclerActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

任何想法?视频文件是由的方式名为.3gp文件。谢谢!

Any ideas? The video file is a .3gp file by the way. Thanks!

推荐答案

我被困在这个时间太长了为好。搜索无处不在,还在挣扎后,我看着完全相同的code酒吧的一个区别我的大code基地。在 VideoView 是有它的高度设置编程。 的android:layout_height =WRAP_CONTENT似乎并没有做出高度调整

I was stuck on this for far too long as well. After searching everywhere and still struggling I looked at my older code base with the exact same code bar one difference. The VideoView was having its height programatically set. android:layout_height="wrap_content" doesn't appear to make a height adjustment.

所以我用一个自制的工具方法来获取屏幕宽度和设置高度为16:9的比例,像这样。 (也许只是你的高度和宽度设置为某个任意数目第一,看它是否是个问题)

So I used a self made utility method to get the screen width and set the height to be in a 16:9 ratio like so. (Maybe just set your height and width to some arbitrary numbers first to see if it's the issue)

public static int getScreenWidth(Context c) {
    int screenWidth = 0; // this is part of the class not the method 
    if (screenWidth == 0) {
        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        screenWidth = size.x;
    }

    return screenWidth;
}

然后,所有它需要的是设置VideoViews高度,这样

Then all it takes is to set the VideoViews height as such

holder.mVideoView.getLayoutParams().height = getScreenWidth(mContext) * 9 /16;

注:我的宽度设置为 match_parent ,所以 WRAP_CONTENT 也可能失败有太多

NOTE: My width is set to match_parent, so wrap_content may also fail there too.

希望这有助于。

这篇关于麻烦的视频在播放Recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆