Question

My UI has ImageButton which I am using to launch gallery from which I want to select a video to be played in videoview. I used code as follow but it doesn't play the video.

public class MainActivity extends Activity {

  VideoView videoView;

  private static final int PICK_FROM_GALLERY=1;

      @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      final VideoView videoView =(VideoView)findViewById(R.id.videoView1);
      MediaController mediaController= new MediaController(this);
          mediaController.setAnchorView(videoView);  

         ImageButton btn1 = (ImageButton)findViewById(R.id.galleryPicker);
             btn1.setOnClickListener(new OnClickListener() {

              public void onClick(View v) {
              Intent intent = new Intent();
              intent.setType("video/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
        }

        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (resultCode != RESULT_OK) return;

            if (requestCode == PICK_FROM_GALLERY) {
                Uri mVideoURI = data.getData();  
                videoView.setVideoURI(mVideoURI);
                videoView.start();   //edited
            }
        }
    });        
}

Layout: Activity_main.xml:

<RelativeLayout 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=".MainActivity" >

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true" />

<ImageButton
    android:id="@+id/galleryPicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="32dp"
    android:layout_marginLeft="16dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/gallery_picker"
    android:scaleType="center"

    android:src="@drawable/gallery_picker" />

Any help?

Was it helpful?

Solution

Do in this way:

 public class MainActivity extends Activity
   {
  .... //variables declarations here
  VideoView videoView;
  MediaController mc;
    protected void onCreate(Bundle savedInstanceState)
        {
         ........
          ......
         videoView = (VideoView)findViewById(R.id.videoView1);
         mc = new MediaController(this);
          mc.setAnchorView(videoView);
         ImageButton btn1 = (ImageButton)findViewById(R.id.galleryPicker);
         btn1.setOnClickListener(new OnClickListener()
         {

          public void onClick(View v) 
          {
          Intent intent = new Intent();
          intent.setType("video/*");
          intent.setAction(Intent.ACTION_GET_CONTENT);
          startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
           }

           } );
        }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode != RESULT_OK) return;

        if (requestCode == PICK_FROM_GALLERY) 
        {
            Uri mVideoURI = data.getData();  
             videoView.setMediaController(mc);

            videoView.setVideoURI(mVideoURI);  

             videoview.requestFocus();
            videoview.setOnPreparedListener(new OnPreparedListener() {
          // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {

            videoview.start();
        }

    });
      }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top