为VideoView for Android应用程序提取视频。

时间:2013-03-04 10:28:35

标签: android android-videoview mediacontroller

所以我正在尝试设置一个Activity,用户可以从其各自的存储中浏览视频,然后使用VideoView播放。我怎样才能做到这一点?我在下面写了一些代码。

此外,我使用的是Nexus 7,因此它没有SD卡。如何制作通用代码从内部/外部存储器中获取视频?

public class VideoCapture extends Activity {

  private static final int INSERT_ID = Menu.FIRST;

  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.gc();
        String filename = "/sdcard/Video/sample.mp4";
        //String fileName = "android.resource://" + getPackageName() + "/" + R.raw.sample;
        VideoView videoView1 = (VideoView)findViewById(R.id.videoView1);
        setContentView(videoView1);
        videoView1.setVideoPath(filename);
        videoView1.setMediaController(new MediaController(this));
        videoView1.requestFocus();
        videoView1.start();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      super.onCreateOptionsMenu(menu);
      menu.add(0, INSERT_ID, 0,"FullScreen");

      return true;
  }

  @Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
      switch(item.getItemId()) {
      case INSERT_ID:
          createNote();
      }
      return true;
  }

  private void createNote() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  }
}

2 个答案:

答案 0 :(得分:0)

为您的ListView创建ListView也创建适配器类
然后将以下代码添加到活动

private List <String> getSD()
{
    List <String> it = new ArrayList <String>();
    File f = new File (Environment.getExternalStorageDirectory()+File.separator+"Vidoes");
        // to get Internal Storage use File m_applicationDir = new File(this.getFilesDir() + "");
        //File f = new File(m_applicationDir + "/Vidoes");
    File[] files = f.listFiles ();

    for (int i = 0; i <files.length; i++)
    {
        File  file = files[i];
        Log.d("Count",file.getPath());
        it.add (file.getPath());
    }
    return it;
}

将此列表设置为列表适配器。点击listitem

播放您的视频

答案 1 :(得分:0)

没关系,我想我明白了。我只需将VideoURI设置为我从画廊中选择的视频并播放它。如果有人需要帮助设置,请告诉我。