打开播放视频片段时面临滞后

时间:2016-08-05 20:41:44

标签: android android-fragments android-videoview

使用AsyncTask在Fragment中加载视频时,我很难获得所需的性能。

我认为我正在占用UI线程,但我不明白在哪里,而且我没有看到内存泄漏。 我尝试从AyncTaskonAttach()OnCreate()onCreateView()

执行setUserVisibleHint()

我也尝试过设置和使用onPreparedListener()

在所有这些尝试中,当VideoFragment生命周期被调用时,我仍然看到性能滞后。延迟我不是指连接,因为我从raw文件夹加载视频。

enter image description here

因此,当它从IntroFragment过渡到DetailsFragment时,VideoFragment的生命周期(onAttach,onCreate,onCreateView等)会触发,这就是我看到的“滞后” “在DetailsFragment变得可见之前。

FragmentsHolder

public abstract class FragmentsHolder extends FragmentActivity{

   @Override
    final protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    mPagerAdapter = new PagerIntroAdapter(super.getSupportFragmentManager(), fragments);
    pager = (ViewPager) findViewById(R.id.view_pager);

    pager.setAdapter(this.mPagerAdapter);
 }
}

FirstSequence

public class FirstSequence extends FragmentsHolder {

@Override
public void init(Bundle savedInstanceState) {

    addSlide(IntroFragment.newInstance("First String","Second String",
            R.drawable.intro_fragment_drawable, 0, 0, 0, Color.parseColor("#4798af"), 1));
    addSlide(DetailsFragment.newInstance("First Details String","Second Details String",
            R.drawable.details_fragment_drawable, 0, 0, 0, Color.parseColor("#4798af"), 1));
    addSlide(FourImagesFragment.newInstance("Title String","Instruction String", R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, 
            R.drawable.image_4, Color.parseColor("#4798af"), 1));
    addSlide(VideoFragment.newInstance(R.raw.my_video,"Video title string","Video subtitle string",
            0, 0, 0, 0, Color.parseColor("#4798af"), 1));

VideoFragment

public class VideoFragment extends Fragment{
   View hackyView;
   private Handler subt_text;
   private static final String ARG_VIDEO_TO_LOAD = "videotoload";
   ...

 public static VideoFragment newInstance(int videoToLoad, String title, String subTitle,int imageDrawable1,int imageDrawable2,int imageDrawable3,int imageDrawable4,int bgColor, int progressCounter){

  VideoFragment sampleSlide = new VideoFragment();

  Bundle args = new Bundle();
  args.putInt(ARG_VIDEO_TO_LOAD,videoToLoad);
  ...

  return sampleSlide;

 }

private int videoToLoad, ....;
private String title, ...;

 @Override
 public void onAttach(Context context) {
    super.onAttach(context);
    System.out.println("VideoFragment OnAttach ");
 }

 @Override
 public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //get args
    if (getArguments() != null && getArguments().size() != 0) {
    videoToLoad = getArguments().getInt(ARG_VIDEO_TO_LOAD);
    ...
    }
    System.out.println("VideoFragment OnCreate ");
   }

 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  View v = inflater.inflate(R.layout.fragment_video, container, false);
  hackyView = v;
  //new loadVideoTask().execute();
  System.out.println("VideoFragment OnCreateView ");
  return vView;
 } 

 @Override
 public void setUserVisibleHint(boolean isVisibleToUser) {
 super.setUserVisibleHint(isVisibleToUser);
   if(isVisibleToUser){
   new loadVideoTask().execute(); 
   }else {

   }
 }

 private class loadVideoTask extends AsyncTask<Object, Object, Object> {

 ProgressDialog pDialog;

 @Override
 protected void onPreExecute() {

    System.out.println("AsyncTask load onPreExecute ");
    DialogMessage = "Loading Video";
    pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage(DialogMessage);
    pDialog.show();
 }

 @Override
 protected Object doInBackground(Object... params) {

    slideUpAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
    cv1 = (CardView) hackyView.findViewById(R.id.cv1);
    //Initialize the rest of the Views, animations, etc
    ...
    return hackyView;
 }  

 protected void onPostExecute(Object result) {
    //process result load video
    loadVideo();
    pDialog.dismiss();
 }
}  

private void loadVideo() {
  subt_text = new Handler();
  vid.setVideoURI(Uri.parse(urlpath));
  vid.requestFocus();
  vid.seekTo(seekto);
  //set all views
 }

}

我已经阅读了很多关于片段,生命周期等的问题,但我仍然无法弄清楚如何使用这些答案来帮助我理解我在实施中做错了什么。

1 个答案:

答案 0 :(得分:0)

结果显示&#34;滞后&#34;我看到片段之间的转换,这是一个黑屏闪烁,通过添加..

解决
<SurfaceView
    android:layout_width="0px"
    android:layout_height="0px" />

到片段的父活动的xml。就我而言,FragmentHolder的xml。 我找到了答案以及更多链接,解释了为什么这里有效VideoView inside fragment causes black screen flicking