Android_关于VideoView

<pre name="code" class="java">package com.stephen.android_35_playvideoview;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {
 private static final String TAG = "MainActivity";
 private static final File FOLDER = getFolder();
 private List<String> lists_Path = null;
 MediaController mediaco;
 private int index = 0;
 private VideoView myVideoView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  myVideoView = (VideoView) this.findViewById(R.id.myVideoview_main);
  Log.i(TAG, "-------->>" + "文件路径为: " + FOLDER);
  lists_Path = getFilePath(FOLDER);
  myVideoView.requestFocus();
  mediaco = new MediaController(this);
  mediaco.setMediaPlayer(myVideoView);
  myVideoView.setMediaController(mediaco);
  initVideoView();

  /**
   * <span style="font-size:14px;color:#3333ff;">此监听器监听path路径设置好后调用的方法,一般做 "开始播放的操作"</span>
   */
  myVideoView.setOnPreparedListener(new OnPreparedListener() {

   @Override
   public void onPrepared(MediaPlayer mp) {
    // TODO Auto-generated method stub
    Log.i(TAG, "------->>onPrepared此方法被调用了!·");
    myVideoView.start();
   }
  });

  /**
   * <span style="font-size:14px;color:#3333ff;">此监听器监听视频无法播放时调用的方法,一般做 "错误处理的操作"</span>
   */
  myVideoView.setOnErrorListener(new OnErrorListener() {

   @Override
   public boolean onError(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub
    Log.i(TAG, "-------->>" + "onError: " + index);
    playVideo();
    return true;
   }

  });

  /**
   * <span style="font-size:14px;color:#3333ff;">此监听器监听视频播放完成调用的方法,一般做 "重新设置uri路径或者path路径</span>
   */
  myVideoView.setOnCompletionListener(new OnCompletionListener() {

   @Override
   public void onCompletion(MediaPlayer mp) {
    Log.i(TAG, "-------->>" + "index为: " + index);
    playVideo();
   }
  });
 }

 /**
  * 初始化VideoView
  */
 private void initVideoView() {
  // TODO Auto-generated method stub
  myVideoView.setVideoPath(lists_Path.get(index++ % lists_Path.size()));
 }

 /**
  * 获取视频文件夹
  * 
  * @return
  */
 private static File getFolder() {
  File rooFile = Environment.getExternalStorageDirectory();
  if (rooFile != null) {
   File new_file = new File(rooFile, "letv" + File.separator
     + "storage" + File.separator + "download" + File.separator
     + "info");
   return new_file;
  }
  return null;
 }

 /**
  * 得到文件夹里的所有视频路径(这里只支持mp4和3gp两种格式)
  * 
  * @param folder
  * @return
  */
 private List<String> getFilePath(File folder) {
  List<String> list_FilePaths = null;
  if (folder.exists()) {
   list_FilePaths = new ArrayList<String>();
   File[] files = folder.listFiles();
   for (int i = 0; i < files.length; i++) {
    String temp_Path = files[i].getAbsolutePath();
    if (temp_Path.endsWith(".mp4") || temp_Path.endsWith(".3gp")) {
     list_FilePaths.add(temp_Path);
    }
   }
   Log.i(TAG, "-------->>" + "所有文件路径为: " + list_FilePaths.size()
     + "\n" + list_FilePaths);

   return list_FilePaths;
  }
  return null;
 }

 /**
  * 开始播放视频
  */
 private void playVideo() {
  myVideoView.setVideoPath(lists_Path.get(index++ % lists_Path.size()));

 }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值