正确的意图是启动图库并获取uri所选视频以在VideoView中播放

时间:2014-03-12 07:30:44

标签: android video android-intent android-videoview

我的UI有ImageButton,我用它来启动图库,我想从中选择要在videoview中播放的视频。我使用的代码如下,但它没有播放视频。

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

布局: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" />

任何帮助?

1 个答案:

答案 0 :(得分:2)

这样做:

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

    });
      }

}