2016-03-04 32 views
0

首先,我已经看过网站上的类似问题,他们没有帮助。我使用FrameLayout,并尝试点击自定义ImageView,使图像视图不可见和视频开始播放,但是当我点击ImageView时,只有一个空白屏幕出现,没有视频/音频播放。Android VideoView没有显示在不同的布局

feedImageView.setOnClickListener(new OnClickListener() { 
      String substrr=item.getURL(); 
      @Override 
      public void onClick(View v) { 

      feedImageView.setVisibility(View.INVISIBLE); 
          vid.setVisibility(View.VISIBLE); 
          Uri vidUri = Uri.parse(substrr); 
          vid.setVideoURI(vidUri); 

          MediaController vidControl = new MediaController(getActivity()); 
          vidControl.setAnchorView(vid); 
          vid.setMediaController(vidControl); 
         vid.start(); 

     }); 

完整的布局,他们走的是下面 -

<FrameLayout 
    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" 
    tools:context="com.example.android.videoplayer.MainActivity"> 
<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/myPic" 
    android:src="@drawable/icon" 

    /> 
    <VideoView 
     android:id="@+id/myVideo" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerInParent="true" 
     android:visibility="invisible"/> 
</FrameLayout> 

请帮助 -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/feed_bg" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="@dimen/feed_item_margin" 
     android:layout_marginRight="@dimen/feed_item_margin" 
     android:layout_marginTop="@dimen/feed_item_margin" 
     android:background="@drawable/bg_parent_rounded_corner" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/feed_item_padding_top_bottom" 
     android:paddingTop="@dimen/feed_item_padding_top_bottom" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 

      <com.android.volley.toolbox.NetworkImageView 
       android:id="@+id/profilePic" 
       android:layout_width="@dimen/feed_item_profile_pic" 
       android:layout_height="@dimen/feed_item_profile_pic" 
       android:scaleType="fitCenter" > 
      </com.android.volley.toolbox.NetworkImageView> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 

       <TextView 
        android:id="@+id/name" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textSize="@dimen/feed_item_profile_name" 
        android:textColor="#0f84cc" 
        android:textStyle="bold" /> 


      </LinearLayout> 

     </LinearLayout> 


     <-- THIS IS THE FRAME LAYOUT CONCERNED WITH THE QUESTION--> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      > 

      <com.example.android.FeedImageView 
      android:id="@+id/feedImage1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/white" 
      android:layout_marginTop="10dp" 
      android:scaleType="fitXY" 
      android:visibility="visible" /> 

      <VideoView 
       android:id="@+id/vid12" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:visibility="invisible" 
       /> 

      </FrameLayout> 
     <TextView 
      android:id="@+id/txtStatusMsg" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      android:paddingLeft="@dimen/feed_item_status_pad_left_right" 
      android:paddingRight="@dimen/feed_item_status_pad_left_right" 
      android:paddingTop="@dimen/feed_item_status_pad_top" /> 

     <Button 
      android:id="@+id/downloadbtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Download" 
      android:textColor="#ffffff" 
      android:gravity="center" 
      android:padding="10dp" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="10dp" 
      android:background="#0f84cc" 
       /> 

    </LinearLayout> 

</LinearLayout> 

现在如果我用Java代码与以下布局的视频作品。

+0

你有什么错误? –

+0

@Shoeb Siddique没有error.I只是无法让VideoView在第一个布局中出现,我也没有任何声音。 – zek54

回答

0

ü需要删除的android:可视性belowXML =“隐形” ..

<VideoView 
    android:id="@+id/myVideo" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" 
    android:visibility="invisible"/> 
+0

我需要将视频的可见性设置为不可见,并且只有在单击图像视图时才可见。使用id myVideo的videoView可以正常工作,并且是代码中不同布局的一部分。我在问什么是更改我应该使用videoview(拥有id vid12)来解决我的问题。 – zek54

0

请尝试在单独的线程来播放视频 -

设置在runOnUiThread视频播放器和网址。

feedImageView.setOnClickListener(new OnClickListener() { 
    String substrr=item.getURL(); 
    @Override 
    public void onClick(View v) { 
     feedImageView.setVisibility(View.INVISIBLE); 
     vid.setVisibility(View.VISIBLE); 
     getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Uri vidUri = Uri.parse(substrr); 
       vid.setVideoURI(vidUri); 

       MediaController vidControl = new MediaController(getActivity()); 
       vidControl.setAnchorView(vid); 
       vid.setMediaController(vidControl); 
       vid.start(); 
      } 
     }); 



    }); 
+0

对不起,我试过但它给出了同样的问题。 – zek54