Video blinks once on onCreate of previous fragment

2019-01-28 12:57发布

I have created an app for galaxy tab using fragments.I have a videoview on a fragment to paly video from external storage.

Here is the code for that fragment -

package com.example.hscroll.demo;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;


public class FragmentVideo1 extends Fragment implements OnTouchListener{

LayoutInflater inflater;
private VideoView video;
private MediaController ctlr;
int length = 0;
boolean isPlaying = false;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
    this.inflater = inflater;
    if(container == null)return null;

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            container = (LinearLayout)inflater.inflate(R.layout.video, container, false);
            video=(VideoView)container.findViewById(R.id.video);
            video.setVideoPath("/mnt/sdcard/Ideal Solar/video1.mp4");


            ctlr=new MediaController(inflater.getContext());
            ctlr.setMediaPlayer(video);
            video.setMediaController(ctlr);
            video.requestFocus();
        }else
        {
            container =         (RelativeLayout)inflater.inflate(R.layout.video_land, container, false);
            video=(VideoView)container.findViewById(R.id.myVideo_land);
            video.setVideoPath("/mnt/sdcard/Ideal Solar/video1.mp4");

            ctlr=new MediaController(inflater.getContext());
            ctlr.setMediaPlayer(video);
            video.setMediaController(ctlr);
            video.requestFocus();
            video.start();
        }

    return container;
}

    }

When i come to the fragment before this video fragment,it blinks once(Like the video fragment is visible below this fragment). It happens only for the first time.If i go back and come again to this fragment it wont blink.

Code for the fragment before video fragment -

   package com.example.hscroll.demo;

   import java.io.BufferedInputStream;
   import java.io.FileInputStream;

   import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.LinearLayout;

   import com.example.hscroll.customer.BitmapWeakReference;
   import com.example.hscroll.library.imagezoom.ImageViewTouch;

  public class Fragment2 extends Fragment{

ImageViewTouch imgview ;
LayoutInflater inflater;
FileInputStream in;
BufferedInputStream buf;
BitmapWeakReference bitmap;
ViewGroup con;
static Fragment2 frag2;

public static Fragment2 newInstance(int num)
{
    if(frag2 == null)
        frag2 = new Fragment2();
    return frag2;
}

private final String  PATH = "/mnt/sdcard/Ideal Solar/page_03.png";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
    this.inflater = inflater;
    if(container == null)return null;
    container = (LinearLayout)inflater.inflate(R.layout.fragment0_layout, container, false);
    con = container;
    imgview= (ImageViewTouch)container.findViewById(R.id.imageView1);
    bitmap = new BitmapWeakReference(imgview.selectImage(inflater.getContext(), PATH));
    if(bitmap!=null)
        imgview.setImageBitmapReset( bitmap.get(), true );

    return container;
}   


 @Override
public void onDestroyView() {
     super.onDestroyView();
        imgview.setImageBitmap(null);
        bitmap.clear();
        bitmap = null;
        Fragment1.unbindDrawables(con.findViewById(R.id.ll));
        System.gc();
    }

    }

I am just showing an image on this fragment.

XML for video fragment -

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

  <ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/header"
    android:background="@drawable/video_header"/>

   <VideoView 
    android:id="@+id/video" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   />
  </LinearLayout>

Thanks in advance for anyone who can help.

1条回答
家丑人穷心不美
2楼-- · 2019-01-28 13:28

VideoView inherits from SurfaceView, SurfaceView cause this problem. try adding a dummy SurfaceView to your mainView when your app loads (in onCreate...). fixed the problem for me.

查看更多
登录 后发表回答