带文件浏览的视频播放器
普通的视频播放器没什么特别的 所以今天加料 其带有文件浏览功能 即 windowXP 播放器带有的浏览功能 然后选择要播放的文件 再返回播放器中播放目标
windowsXP 行为 参考如下截图:
[功能]
1. 文件浏览器
2. 视频播放器
[代码 步骤]
1. 构建界面
写道
1 VideoView 用于视频播放
2 Button 一个用于播放控制(暂停/继续) 另一个用于文件浏览
1 TextView 用于显示指定目标文件和目录
2 Button 一个用于播放控制(暂停/继续) 另一个用于文件浏览
1 TextView 用于显示指定目标文件和目录
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <VideoView
- android:id="@+id/video"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <Button
- android:id="@+id/cmd"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:text="播放"
- />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:text="浏览"
- />
- </LinearLayout>
- </LinearLayout>
2. VideoView 的一些方法
- //1. 定义
- VideoView vp;
- //2.初始化
- vp = (VideoView) findViewById(R.id.video);
- //3. 暂停
- vp.pause();
- //4. 继续播放
- vp.start();
- //5. 判断是否正在播放
- vp.isPlaying()
3. 让一个Button 既有播放功能 又又暂停功能
- findViewById(R.id.cmd).setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(vp.isPlaying()){
- vp.pause();
- cmd.setText("播放");
- }
- else {
- vp.start();
- cmd.setText("暂停");
- }
- }
- });
4. 按下"Browse" Button 去浏览文件 并返回目标的文件与目录 且传入要检索文件的类型 本例为:*.3gp 并标记其为:FILE_QUERY_ACTIVITY 便于返回供确定所用
- public void sendGo(){
- Intent i = new Intent(this, FileQuery.class);
- Bundle b = new Bundle();
- b.putString("format", ".3gp");
- i.putExtras(b);
- startActivityForResult(i,FILE_QUERY_ACTIVITY);
- }
- findViewById(R.id.button).setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //vp.pause();
- sendGo();
- }
- });
5. 在onActivityResult()捕捉返回的目标 然后播放之
- public void playVideo(String s){
- Uri u = Uri.parse(s);
- vp.setVideoURI(u);
- vp.start();
- cmd.setText("暂停");
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode,
- Intent data){
- switch (resultCode){
- case FILE_QUERY_ACTIVITY:
- Bundle b = data.getExtras();
- String string = b.getString(FILE_QUERY);
- playVideo(string);
- }
- }
以上是与播放有关的功能 下面说文件浏览有关的功能
1. 定义布局: file.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="bottom"
- >
- <Button
- android:id="@+id/previous"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="后退 "
- android:gravity="right"
- />
- <TextView
- android:id="@+id/path"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:singleLine="true"
- />
- </LinearLayout>
- <ListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
2. 定义变量fileType 用于保存VideoView 传来的文件类型 并定义2个String 用于存放目标目录及其上级目录
- //定义
- String fileType = "";
- //匹配检测
- public void getType(Intent i){
- Bundle b = i.getExtras();
- fileType = b.getString("format");;
- }
- //调用 刚进入文件浏览时调用
- getType(this.getIntent());
- //存放目标目录
- String target="";
- //存放目标目录的上级目录
- String parent="";
3. 列出目标目录下的所有文件/目录
- //检测目标是否应该列出 条件:
- 1. 子目录
- 2. 文件 且 类型匹配
- public boolean isType(String s){
- if(s.contains(fileType)){
- return true;
- }
- else {
- return false;
- }
- }
- public List<Map<String,String>> list(String s){
- List<Map<String,String>> result = new ArrayList<Map<String, String>>();
- File file = new File(s);
- File[] list= file.listFiles();
- for( File f : list ){
- if(isType(f.getName())||f.isDirectory()){
- Map<String,String> map =new HashMap<String, String>();
- map.put(FILE_NAME, f.getName().toString());
- if(f.isFile()){
- map.put(FILE_TYPE, FILE_TYPE_FILE);
- }
- else if(f.isDirectory()){
- map.put(FILE_TYPE, FILE_TYPE_DIRECTORY);
- }
- result.add(map);
- }
- }
- path.setText(target);
- return result;
- }
4. 得到目标目录的子目录 当该子目录也是目录时 列出该子目录下的所有文件/目录 当该子目录为文件时 表示找到了目标 返回之 如何注册:点击ListView中的具体item 就像windowXP 的一样
- list.setOnItemClickListener(new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- // TODO Auto-generated method stub
- String s = value.get((int)arg3).get("name");
- value = getNextList(s);
- adapter.notifyDataSetChanged();
- }
- });
- public List<Map<String,String>> getNextList(String s){
- String string = updateNext(s);
- if(isFile(string)){
- replyQuery(string);
- string = updatePrevious();
- }
- return list(string);
- }
- public String updateNext(String s){
- parent = target;
- target += "/" + s;
- return target;
- }
5. 返回目标目录的上级目录 并显示其所有文件/目录 如何注册: Button id=R.id.previous
- previous.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- value = getPreviousList();
- adapter.notifyDataSetChanged();
- }
- });
- public List<Map<String,String>> getPreviousList(){
- String string = updatePrevious();
- return list(string);
- }
- public String updatePrevious(){
- target = parent;
- File file = new File(parent);
- parent = file.getParent();
- return target;
- }
6. emulator 的运行截图:
* 整体布局:
* 文件选择:
* 播放界面