3gp影片:android使用VideoView设计的扫描全局.mp4和.3gp的视频播放器

在android的中提供了一个VideoView组件,用于播放视频文件,要想使用VideoView播放视频,首先要在布局文件中创建改组件,然后在activity中获取该组件,调用VideoView中的start方法来播放视频。首先通过扫描SD卡找到SD卡中MP4和3gp个是的视频文件,放在一个listView列表中,点击listview的单元格实现对当前视频文件的播放。

下面放上主要的代码

1、xml文件

list.xml文件为视频列表布局文件,主要包括一个listview显示视频列表和一个更新视频按钮

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/updatevedio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:text="视频目录:"/> <Button android:id="@+id/updatevedioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="更新视频"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/updatevedio" android:id="@+id/lv"> </ListView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:textSize="20sp" android:text="C"/> </RelativeLayout>list_sd.xml文件为listview单元格布局文件,只是显示了一个视频名称,显示缩略图以后再做。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:id="@+id/mp4"/> </LinearLayout>video.xml一个videoview组件,用于播放视频
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <VideoView android:id="@+id/video" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> </RelativeLayout>MovieListActivity.java 扫描全局得到MP4和3gp视频文件并保存在listview中,程序中都注释,就不多解释了。。。
package cn.edu.cqu.movie;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import com.example.project.R;import android.app.Activity;import android.app.ProgressDialog;import android.content.Intent;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleAdapter;public class MovieListActivity extends Activity{ private static final String LOG = "main";private ListView lv; private Button updatevedioButton; private ArrayList<HashMap<String, String>> name; private ArrayList<String> moviepathlist; String path = Environment.getExternalStorageDirectory().getPath(); String file; String moviepath; SimpleAdapter adapter; ProgressDialog progressDialog ; ScaningHandler sHandler; ScaningThread st; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); lv = (ListView) findViewById(R.id.lv); name = new ArrayList<HashMap<String, String>>(); moviepathlist=new ArrayList<String>(); updatevedioButton = (Button)findViewById(R.id.updatevedioButton); updatevedioButton.setOnClickListener(new UpdatevedioButtonListener()); //获得视频列表 vedio(); //progressDialog = ProgressDialog.show(MovieListActivity.this, "请稍等", "正在扫描...", true); // 开始动态加载线程 //mThreadLoadApps.start(); } class UpdatevedioButtonListener implements OnClickListener{@Overridepublic void onClick(View arg0) {updataView();} } //获得视频列表 private void vedio(){ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // File path = Environment.getExternalStorageDirectory();// 获得SD卡路径 System.out.println("path-------》" + path); //File[] files = path.listFiles();// 读取 getFileName(path); //视频列表 // sHandler = new ScaningHandler(); //st = new ScaningThread(); //st.start(); } SimpleAdapter adapter = new SimpleAdapter(this, name, R.layout.sd_list, new String[] { "视频名称" }, new int[] { R.id.mp4 }); lv.setAdapter(adapter); for (int i = 0; i < name.size(); i++) { Log.i(LOG, "list. name: " + name.get(i)); } lv.setOnItemClickListener(new ListView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View view, int position,long id) { //String str = lv.getItemAtPosition(position).toString(); //file = str.substring("{视频名称=".length(), str.lastIndexOf("}")); //String moviename = moviepath ; //System.out.println("视频文件地址-----》" + moviepath); String moviename = moviepathlist.get(position); Intent intent = new Intent(MovieListActivity.this,MovieActivity.class); //把文件地址传给下一个activity intent.putExtra("moviename", moviename); startActivity(intent); //关闭当前Activity,不放到栈里面 //finish(); } }); } //仅搜索当前目录下的文件 private void getFileName(String url) { File files = new File(url); File[] file = files.listFiles(); for(File f : file){ if(f.isDirectory()){ getFileName(f.getAbsolutePath()); } else{ String fileName = f.getName(); if (fileName.endsWith(".3gp")||fileName.endsWith(".mp4")) { HashMap<String, String> map = new HashMap<String, String>(); //获得视频名,去掉扩展名 String s = fileName.substring(0,fileName.lastIndexOf(".")).toString(); Log.i(LOG, "文件名mp4或3gp:: " + s); //获取视频文件地址 moviepath = f.getPath(); System.out.println("filename-----" + fileName); map.put("视频名称", fileName); moviepathlist.add(moviepath); name.add(map); } } } } //先判断目录是否为空,否则会报空指针 /*if (files != null) { for (File file : files) { String fileName = file.getName(); if (fileName.endsWith(".3gp")||fileName.endsWith(".mp4")) { HashMap<String, String> map = new HashMap<String, String>(); String s = fileName.substring(0,fileName.lastIndexOf(".")).toString(); Log.i(LOG, "文件名mp4或3gp:: " + s); map.put("视频名称", fileName); name.add(map); } } } */ //更新视频 private void updataView(){ name.clear(); //File path = Environment.getExternalStorageDirectory();// 获得SD卡路径 // File[] files = path.listFiles();// 读取 getFileName(path); //视频列表 SimpleAdapter adapter = new SimpleAdapter(this, name, R.layout.sd_list, new String[] { "视频名称" }, new int[] { R.id.mp4 }); //lv.removeAllViews(); lv.setAdapter(adapter); } }
MovieActivity.java 视频播放的模块,在android中提供了一个与VideoView组件组合使用的mediacontroller组件,mediacontroller可以通过图形界面来控制视频的播放。

package cn.edu.cqu.movie;import java.io.File;import com.example.project.R;import android.app.Activity;import android.content.Intent;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.os.Bundle;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.MediaController;import android.widget.TextView;import android.widget.Toast;import android.widget.VideoView;public class MovieActivity extends Activity{public static String tpath;private VideoView video;private MediaController mController;@Overrideprotected void onCreate(Bundle savedInstanceState) {this.requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.other);video = (VideoView)findViewById(R.id.video); //实例化MediaController mController = new MediaController(this); //把要播放的文件地址通过intent传过来 Intent intent = getIntent(); tpath = intent.getStringExtra("moviename"); File file = new File(tpath); if(file.exists()){ //设置播放视频源的位置 video.setVideoPath(file.getAbsolutePath()); //为Video指定Mediacontroller video.setMediaController(mController); //为Mediacontroller指定video mController.setMediaPlayer(video); //自动播放,不需要按下开始键 video.start(); 视频播放完之后返回播放列表 video.setOnCompletionListener(new OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer arg0) {Intent intent2list = new Intent(); intent2list.setClass(MovieActivity.this, MovieListActivity.class); startActivity(intent2list); //关闭当前Activity,不放到栈里面 finish();}}); //增加监听上一个和下一个的切换事件,默认这两个按钮是不显示 mController.setPrevNextListeners(new OnClickListener() {@Overridepublic void onClick(View v) { Toast.makeText(MovieActivity.this, "下一个",0).show();}}, new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MovieActivity.this, "上一个",0).show();}}); }}}到这里一个能够扫描全局并播放MP4和3gp文件的视频播放器就完成了,如下图所示



相关推荐

相关文章