Fragment里面的VideoView

时间:2015-04-01 14:06:54

标签: android android-viewpager fragment android-videoview

编辑:我认为我的片段实现存在问题,导致videoView无法正常工作,但事实证明实施是正确的,我只是遇到布局问题视频的xml。 我使用viewpager来保持代码作为在片段内正确实现videoView的一个例子:)

这是我的片段:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;

import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


public class ScreenSlidePageFragment extends Fragment {

    private Context context = null; //TODO MAKE SETTER
    private String fileName;
    private final String FILE_NAME = "FILE_name";

    /**
     * Factory method for this fragment class. Constructs a new fragment.
     */
    public static ScreenSlidePageFragment create(String fileName, Context context) {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
        fragment.setFileName(fileName);
        fragment.setContext(context);
        return fragment;
    }

    public ScreenSlidePageFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        fileName = getArguments().getString(FILE_NAME);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout containing a title and body text.
        ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_screen_slide_page, container, false);

        // Set the title view to show the page number.
        String name = this.fileName;
        if (name != null) {
            if (name.endsWith(".jpg")) {
                Bitmap myBitmap = BitmapFactory.decodeFile(name);
                ImageView view = (ImageView) rootView.findViewById(R.id.imageView);
                view.setImageBitmap(myBitmap);
                PhotoViewAttacher mAttacher = new PhotoViewAttacher(view);
            }
            if (name.endsWith(".mp4")) {
                final VideoView video = (VideoView) rootView.findViewById(R.id.videoView);

                video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        video.requestFocus();
                        video.start();
                    }
                });
                video.setVideoURI(Uri.parse(name));
//                Bitmap bitmap = ThumbnailUtils.createVideoThumbnail("/storage/sdcard0/Android/data/huji.ac.il.test/files/savedFiles/150401015447.mp4", MediaStore.Video.Thumbnails.MICRO_KIND);
//                ImageView view = (ImageView) rootView.findViewById(R.id.imageView);
//                view.setImageBitmap(bitmap);
            }

        }
        return rootView;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setContext(Context context) {
        this.context = context;
    }
}

这是布局" fragment_screen_slide_page.xml":

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

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="0dp"

        />

    <VideoView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/videoView"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

这是使用片段的活动:

public class ScreenSlideActivity extends FragmentActivity {


    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    private ArrayList<String> fileNameList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View decorView = getWindow().getDecorView();
        // Hide the status bar.
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        setContentView(R.layout.activity_screen_slide);

        fileNameList=getImagesFromStorage();
        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), fileNameList,this);
        mPager.setAdapter(mPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public ArrayList<String> getImagesFromStorage()
    {
        ArrayList<String> fileNameList= new ArrayList<String>();
        File file= new File(getApplicationContext().getExternalFilesDir(null).getAbsolutePath()+ File.separator + "savedFiles");

        if (file.isDirectory())
        {
            File[] listOfFiles= file.listFiles();
            for (int i = 0; i <listOfFiles.length; i++)
            {

                fileNameList.add(listOfFiles[i].getAbsolutePath());
            }
        }
        return fileNameList;
    }

}

我尝试打开GPU加速,因为有些帖子建议,但它没有用。 我尝试在主要活动上播放视频,而不是在片段上播放它,所以我在猜测问题出在我的片段上。 有什么建议吗?

修改

适配器:

public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    private ArrayList<String> fileNames;
    private Context context=null;


    public ScreenSlidePagerAdapter(FragmentManager fragmentManager, ArrayList<String> fileNameList,Context context) {
        super(fragmentManager);
        this.fileNames=fileNameList;
        this.context=context;
    }


    @Override
    public Fragment getItem(int position) {

        return ScreenSlidePageFragment.create(fileNames.get(position),this.context);
    }

    @Override
    public int getCount() {
        return fileNames.size();
    }
}

1 个答案:

答案 0 :(得分:5)

您需要确保已设置:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

在你的AndroidManifest.xml

另外,我认为你根本没有开始播放视频

在您的VideoView初始化中,您可以使用以下方式访问VideoView中的MediaPlayer:

VideoView video = (VideoView) rootView.findViewById(R.id.videoView);
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    videoView.requestFocus();
                    videoView.start();
                }
  });
video.setVideoURI(Uri.parse(pathForTheFile));

修改 由于这是您尝试播放的本地视频,因此您无需等待MediaPlayer准备好,因此这应该有效:

VideoView video = (VideoView) rootView.findViewById(R.id.videoView);               
video.setVideoURI(Uri.parse(pathForTheFile));
video.start();
video.requestFocus();

所以,你不需要这些行:

MediaController mc=  new MediaController(getActivity());
                mc.setAnchorView(video);
                video.setMediaController(mc);
                video.requestFocus();

让我知道它是否有效