1

У меня мало карт в RecyclerView. Каждая карта имеет пользовательский просмотр видео. При щелчке видеоизображения видео должно начинаться. Однако в моем случае это просто показывает голубые границы, и видео не запускается. Код приведен ниже,Воспроизведение видео в CardView

custom_card_view.xml:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_gravity="center" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_margin="5dip" 
    card_view:cardCornerRadius="4dp"> 


    <com.test.components.CustomVideoView 
     android:id="@+id/videoView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     /> 

    <TextView 
     android:id="@+id/info_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dip" 

     /> 


</android.support.v7.widget.CardView> 

CustomVideoView:

import android.content.Context; 
import android.net.Uri; 
import android.util.AttributeSet; 
import android.widget.MediaController; 
import android.widget.VideoView; 

import com.test.src.R; 

    /** 
    * Created by Psych on 12/20/14. 
    */ 
    public class CustomVideoView extends VideoView { 

     MediaController mMediaController = null; 
     private String mVideoUrl = null; 

     public CustomVideoView(Context context) { 
      super(context); 
     } 

     public CustomVideoView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
     } 

     /** 
     * Initialize all the basic elements here. 
     */ 
     private void initialize() { 
      mMediaController = new MediaController(getContext()); 
     } 

     /** 
     * Starts up the video. 
     * Throws a null pointer exception if the URL is not set 
     */ 
     public void startVideo() { 

      // Return as is if there is no URL 
      if (mVideoUrl == null) { 
       throw new NullPointerException(getContext().getString(R.string.video_null_pointer_message)); 
      } 

      mMediaController.setAnchorView(this); 
      setMediaController(mMediaController); 
      start(); 
     } 

     public void setmVideoUrl(String mVideoUrl) { 
      this.mVideoUrl = mVideoUrl; 
      Uri vidUri = Uri.parse(mVideoUrl); 
      setVideoURI(vidUri); 
     } 

     public String getmVideoUrl() { 
      return mVideoUrl; 
     } 
    } 

MainActivity:

public class MainActivity extends ActionBarActivity { 

    RecyclerView.LayoutManager mLayoutManager = null; 
    RecyclerView mCardsRecyclerView = null; 
    RecyclerView.Adapter mAdapter = null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initComponents(); 

     // use this setting to improve performance if you know that changes 
     // in content do not change the layout size of the RecyclerView 
     mCardsRecyclerView.setHasFixedSize(true); 
     mCardsRecyclerView.setLayoutManager(mLayoutManager); 
     mCardsRecyclerView.setAdapter(mAdapter); 

    } 

    /** 
    * Initialize all ui components/widgets/elements 
    */ 
    private void initComponents() { 
     mCardsRecyclerView = (RecyclerView) findViewById(R.id.cardsListView); 
     mLayoutManager = new LinearLayoutManager(this); 
     mAdapter = new NewsFeedAdapter(); 
    } 
} 

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    style="@style/AppTheme" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/cardsListView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/alertButton" /> 


</RelativeLayout> 

Я хочу, чтобы каждая карта отображала только миниатюру. При щелчке видео, соответствующая карта должна запустить видео (не все из них). VideoView должен отображать весь экран при запуске видео. Как я могу это достичь?

Спасибо!

+0

Прогресс в этой области? – sirvon

+0

Нет. Пришлось положить его на трюм. – Ahmed

ответ

0

У меня была та же проблема. This ответ от другого stackOverflow вопрос, решена это для меня. То есть, указав явное значение высоты (или ширины или обоих) в VideoView. Например,

<VideoView 
    android:id="@+id/mVideoView" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp"/> 
Смежные вопросы