如何在另一项活动中使用视频观看播放录制的视频

时间:2017-01-19 07:40:38

标签: java android bundle android-mediaplayer android-videoview

我有两个活动Main Activity.java和VideoViewActivity.java我正在录制视频,然后创建位图并将其存储在Main Activity.java中的arraylist中然后我将视频文件路径(即录制的视频)传递给VideoViewActivity并在那里我我正试图在videoview中播放视频。当我点击缩略图时,它应该在VideoViewActivity中播放视频,但是当我点击缩略图时,应用程序崩溃了。我在做什么,我做错了什么?帮帮我... ...

MainActivity

 private List<Bitmap> bitMapsAvailable = new ArrayList<>();//Bitmaps of video files 
  private List<String> bitMapsFilePath = new ArrayList<>();//video files path



private void showThumbnails() {

    LinearLayout layout = (LinearLayout) findViewById(R.id.thumbnails);

    bitMapIndex = 0;
    if (layout.getChildCount() > 0) {
        layout.removeAllViews();
    }
    for (Bitmap eachBitMap : bitMapsAvailable) {

        bitMapIndex++;
        ImageView thumb = new ImageView(this);
        thumb.setId(new Integer(bitMapIndex+ 17));
        thumb.setLayoutParams(new android.view.ViewGroup.LayoutParams(100, 80));
        thumb.setImageBitmap(eachBitMap);

        thumb.setOnClickListener(previewThumb(thumb));
        layout.addView(thumb);

        CheckBox deleteCheckBox = new CheckBox(this);
        deleteCheckBox.setId(new Integer(bitMapIndex));

        deleteCheckBox.bringToFront();
        deleteCheckBox.setOnClickListener(deleteRelatedThumbnail(deleteCheckBox));

        layout.addView(deleteCheckBox);

        CheckBox saveCheckBox = new CheckBox(this);
        saveCheckBox.setId(new Integer(bitMapIndex + 31));
        saveCheckBox.bringToFront();

        saveCheckBox.setOnClickListener(saveRelatedThumbnail(saveCheckBox));
        layout.addView(saveCheckBox);

        String("check").concat(new Integer(checkTagNumber++).toString()));

    }
}
View.OnClickListener previewThumb(final ImageView imageview) {

    return new View.OnClickListener() {
        public void onClick(View arg0) {

            int index = imageview.getId()-18;


            Intent myIntent = new Intent(MainActivity.this,
                    VideoViewActivity.class);
              findViewById(R.id.VideoView);
            Bundle bundle = new Bundle();

            //Add your data to bundle
            bundle.putString("url", bitMapsFilePath.get(index));
            myIntent.putExtras(bundle);
            startActivity(myIntent);


      }
    };
}

VideoViewActivity

public class VideoViewActivity extends Activity {

    public VideoView videoview;

    Bundle bundle = getIntent().getExtras();
    String videoURL = bundle.getString("url");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.videoview_main);

        videoview = (VideoView) findViewById(R.id.VideoView);

        Toast.makeText(this, videoURL, Toast.LENGTH_SHORT).show();

        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            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() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {

                videoview.start();
            }
        });

    }

}

3 个答案:

答案 0 :(得分:1)

    Bundle bundle = getIntent().getExtras();
    String videoURL = bundle.getString("url");

onCreate()

中的VideoViewActivity方法内转移这两行

答案 1 :(得分:0)

您只需在另一项活动中使用以下代码即可播放视频。

Uri uri = Uri.parse(URL); //Declare your url here.

VideoView mVideoView  = (VideoView)findViewById(R.id.videoview)
mVideoView.setMediaController(new MediaController(this));       
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();

答案 2 :(得分:0)

在onCreate()之前你不能得到getIntent() - 那时根本没有可用的Intent。 将您的代码更改为以下内容:

public class VideoViewActivity extends Activity {

public VideoView videoview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videoview_main);
    videoview = (VideoView) findViewById(R.id.VideoView);
    Toast.makeText(this, videoURL, Toast.LENGTH_SHORT).show();

    Bundle bundle = getIntent().getExtras();
    String videoURL = bundle.getString("url");

    try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(
                VideoViewActivity.this);
        mediacontroller.setAnchorView(videoview);
        // Get the URL from String VideoURL
        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() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {

            videoview.start();
        }
    });

}