【问题标题】:Trouble getting video to play on Recyclerview无法在 Recyclerview 上播放视频
【发布时间】:2015-02-20 01:18:28
【问题描述】:

我在 RecyclerView 中有一个 VideoView。我希望最终有一个要在 Recyclerview 上播放的视频列表。我决定从一个视频开始,然后继续制作多个视频。我似乎无法在 Recyclerview 中播放一个视频。当我在手机上启动应用程序时,我得到的只是进度条。由于某种原因,永远不会调用 onPrepared 函数。这是我的代码。

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

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

<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

<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 文件。谢谢!

【问题讨论】:

    标签: java android mobile android-videoview android-recyclerview


    【解决方案1】:

    我也被困在这个问题上太久了。在到处搜索并仍在苦苦挣扎之后,我查看了具有完全相同代码的旧代码库,其中一个差异。 VideoView 以编程方式设置其高度。 android:layout_height="wrap_content" 似乎没有进行高度调整。

    所以我使用自制的实用方法来获取屏幕宽度并将高度设置为 16:9 的比例,就像这样。 (也许只是先将您的高度和宽度设置为一些任意数字,看看是否是问题所在)

    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 的高度就可以了

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

    注意:我的宽度设置为match_parent,所以wrap_content 也可能在那里失败。

    希望这会有所帮助。

    【讨论】:

    • 谢谢,首先将高度设置为固定值帮助我解决了我在回收站视图中的视频视图问题!
    【解决方案2】:

    问题出在

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    

    在加载视频之前,它们的值为 0 来解决这个问题可以使用将 minHeight 设置为 1dp 并将 layout_width 设置为 ma​​tch_parent 这样就可以解决问题,您不需要固定的宽度和高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 2012-06-07
      • 2015-04-13
      • 2014-11-13
      • 2015-11-19
      • 2020-03-04
      • 2018-10-27
      相关资源
      最近更新 更多