Search code examples
javaandroidbundleandroid-mediaplayerandroid-videoview

how to play a recorded video using video view in another activity


I have two activity Main Activity.java and VideoViewActivity.java I am Recording video and then creating bitmap and storing it in arraylist in Main Activity.java then i am passing the videofile path(i.e recorded video) to VideoViewActivity and there i am trying to play the video in videoview. when i click on the thumbnail it should play video in VideoViewActivity but the app is crashing when i click on thumbnail.Where and what i am doing wrong??Help me plsss..

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();
            }
        });

    }

}

Solution

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

    transfer these two lines inside onCreate() method in your VideoViewActivity