在片段内创建ViewVideo

时间:2017-10-21 19:03:52

标签: android android-fragments video fragment video-streaming

我想在片段内创建视频流,但是我遇到的问题导致我无法运行我的应用程序,应用程序突然停止并且不会出现。 有人可以帮帮我,哪些代码导致应用程序无法运行?

这是 fragment_kamera_atas.xml

<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.indrswr.cece.KameraAtasFragment">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="match_parent"
    android:layout_height="223dp"/> 
</FrameLayout>

这是 KameraAtasFragment.java

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.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

public class KameraAtasFragment extends Fragment {
    ProgressDialog progressDialog;
    VideoView videoView;

    String VideoURL = "archive.org/download/Pbtestfilemp4videotestmp4/video_test.mp4";

    public KameraAtasFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_kamera_atas, container, false);
        final VideoView videoView = (VideoView) rootView.findViewById(R.id.videoView);
        progressDialog.setMessage("Buffering");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.show();

        try {
            MediaController mediaController = new MediaController(getActivity());
            mediaController.setAnchorView(videoView);
            Uri video = Uri.parse(VideoURL);
            videoView.setMediaController(mediaController);
            videoView.setVideoURI(video);
        }

        catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoView.requestFocus();
        videoView.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer) {
                progressDialog.dismiss();
                videoView.start();
            }
        });

        return rootView;
    }
}

0 个答案:

没有答案