现在的位置: 首页 > 综合 > 正文

文件浏览 & 视频播放器

2013年09月18日 ⁄ 综合 ⁄ 共 11222字 ⁄ 字号 评论关闭

带文件浏览的视频播放器

 

普通的视频播放器没什么特别的 所以今天加料 其带有文件浏览功能 即 windowXP 播放器带有的浏览功能 然后选择要播放的文件 再返回播放器中播放目标

 

windowsXP 行为 参考如下截图:

 

 

[功能]

1. 文件浏览器

2. 视频播放器

 

 

 

[代码 步骤]

1. 构建界面

写道
1 VideoView 用于视频播放
2 Button 一个用于播放控制(暂停/继续)  另一个用于文件浏览
1 TextView 用于显示指定目标文件和目录

  

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7. <VideoView     
  8.     android:id="@+id/video"  
  9.     android:layout_width="fill_parent"    
  10.     android:layout_height="wrap_content"    
  11.     />   
  12. <LinearLayout   
  13.     android:orientation="horizontal"  
  14.     android:layout_width="fill_parent"  
  15.     android:layout_height="wrap_content" >   
  16. <Button     
  17.     android:id="@+id/cmd"  
  18.     android:layout_width="wrap_content"    
  19.     android:layout_height="fill_parent"    
  20.     android:text="播放"  
  21.     />   
  22. <Button     
  23.     android:id="@+id/button"  
  24.     android:layout_width="wrap_content"    
  25.     android:layout_height="fill_parent"    
  26.     android:text="浏览"  
  27.     />   
  28. </LinearLayout>   
  29. </LinearLayout>  

 

 

 

2. VideoView 的一些方法

Java代码 复制代码
  1. //1. 定义   
  2. VideoView vp;   
  3.   
  4. //2.初始化   
  5. vp = (VideoView) findViewById(R.id.video);   
  6.   
  7. //3. 暂停   
  8. vp.pause();   
  9.   
  10. //4. 继续播放   
  11. vp.start();   
  12.   
  13. //5. 判断是否正在播放   
  14. vp.isPlaying()  

 

 

3. 让一个Button 既有播放功能 又又暂停功能

Java代码 复制代码
  1. findViewById(R.id.cmd).setOnClickListener(new OnClickListener(){   
  2.    public void onClick(View v) {   
  3.     // TODO Auto-generated method stub   
  4.     if(vp.isPlaying()){   
  5.      vp.pause();   
  6.         
  7.      cmd.setText("播放");   
  8.     }   
  9.     else {   
  10.      vp.start();   
  11.         
  12.      cmd.setText("暂停");   
  13.     }   
  14.    }   
  15.         });  

 

 

 

4. 按下"Browse" Button 去浏览文件 并返回目标的文件与目录 且传入要检索文件的类型 本例为:*.3gp 并标记其为:FILE_QUERY_ACTIVITY 便于返回供确定所用

Java代码 复制代码
  1. public void sendGo(){   
  2.         Intent i = new Intent(this, FileQuery.class);   
  3.            
  4.         Bundle b = new Bundle();   
  5.         b.putString("format"".3gp");   
  6.            
  7.         i.putExtras(b);   
  8.            
  9.         startActivityForResult(i,FILE_QUERY_ACTIVITY);   
  10.     }   
  11.   
  12.   
  13. findViewById(R.id.button).setOnClickListener(new OnClickListener(){   
  14.             public void onClick(View v) {   
  15.                 // TODO Auto-generated method stub   
  16.                 //vp.pause();   
  17.   
  18.                 sendGo();   
  19.             }   
  20.         });  

 

 

5. 在onActivityResult()捕捉返回的目标 然后播放之

Java代码 复制代码
  1. public void playVideo(String s){   
  2.         Uri u = Uri.parse(s);   
  3.         vp.setVideoURI(u);   
  4.         vp.start();   
  5.            
  6.         cmd.setText("暂停");   
  7.     }   
  8.   
  9.   
  10. @Override  
  11.     protected void onActivityResult(int requestCode, int resultCode,   
  12.                                    Intent data){   
  13.         switch (resultCode){   
  14.         case FILE_QUERY_ACTIVITY:   
  15.             Bundle b = data.getExtras();   
  16.                
  17.             String string = b.getString(FILE_QUERY);   
  18.                
  19.             playVideo(string);   
  20.                
  21.         }   
  22.            
  23.     }  

 

 

 

以上是与播放有关的功能 下面说文件浏览有关的功能

 

1.  定义布局: file.xml

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <LinearLayout    
  8.     android:orientation="horizontal"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:gravity="bottom"  
  12.     >  
  13. <Button  
  14.     android:id="@+id/previous"     
  15.     android:layout_width="wrap_content"    
  16.     android:layout_height="wrap_content"    
  17.     android:text="后退 "  
  18.     android:gravity="right"  
  19.     />  
  20.  <TextView  
  21.     android:id="@+id/path"     
  22.     android:layout_width="wrap_content"    
  23.     android:layout_height="wrap_content"    
  24.     android:singleLine="true"  
  25.     />  
  26. </LinearLayout>  
  27. <ListView  
  28.     android:id="@+id/list"     
  29.     android:layout_width="fill_parent"    
  30.     android:layout_height="wrap_content"    
  31.     />  
  32. </LinearLayout>  

 

 

2. 定义变量fileType 用于保存VideoView 传来的文件类型 并定义2个String 用于存放目标目录及其上级目录

Java代码 复制代码
  1. //定义   
  2. String fileType = "";   
  3.   
  4.   
  5. //匹配检测   
  6. public void getType(Intent i){   
  7.         Bundle b = i.getExtras();   
  8.            
  9.         fileType = b.getString("format");;   
  10.            
  11.     }   
  12.   
  13.   
  14. //调用 刚进入文件浏览时调用   
  15. getType(this.getIntent());  
Java代码 复制代码
  1. //存放目标目录   
  2. String target="";   
  3.   
  4. //存放目标目录的上级目录   
  5. String parent="";  

  

 

 

3. 列出目标目录下的所有文件/目录

Java代码 复制代码
  1. //检测目标是否应该列出 条件:   
  2. 1. 子目录   
  3. 2. 文件 且 类型匹配   
  4. public boolean isType(String s){   
  5.         if(s.contains(fileType)){   
  6.             return true;   
  7.         }   
  8.         else {   
  9.             return false;   
  10.         }   
  11.     }   
  12.   
  13. public List<Map<String,String>> list(String s){   
  14.         List<Map<String,String>> result = new ArrayList<Map<String, String>>();   
  15.            
  16.         File file = new File(s);   
  17.            
  18.         File[] list= file.listFiles();   
  19.            
  20.         for( File f : list ){   
  21.                
  22.             if(isType(f.getName())||f.isDirectory()){   
  23.                 Map<String,String> map =new HashMap<String, String>();   
  24.                    
  25.                 map.put(FILE_NAME, f.getName().toString());   
  26.                    
  27.                 if(f.isFile()){   
  28.                     map.put(FILE_TYPE, FILE_TYPE_FILE);   
  29.                 }   
  30.                 else if(f.isDirectory()){   
  31.                     map.put(FILE_TYPE, FILE_TYPE_DIRECTORY);   
  32.                 }   
  33.                
  34.                 result.add(map);   
  35.             }   
  36.         }   
  37.            
  38.         path.setText(target);   
  39.         return result;   
  40.            
  41.     }  

 

 

4. 得到目标目录的子目录 当该子目录也是目录时 列出该子目录下的所有文件/目录 当该子目录为文件时 表示找到了目标 返回之 如何注册:点击ListView中的具体item 就像windowXP 的一样

Java代码 复制代码
  1. list.setOnItemClickListener(new OnItemClickListener(){   
  2.             @Override  
  3.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,   
  4.                     long arg3) {   
  5.                 // TODO Auto-generated method stub   
  6.                 String s = value.get((int)arg3).get("name");   
  7.                    
  8.                 value = getNextList(s);   
  9.                    
  10.                 adapter.notifyDataSetChanged();   
  11.                    
  12.             }   
  13.             });   
  14.   
  15.   
  16. public List<Map<String,String>> getNextList(String s){   
  17.         String string = updateNext(s);   
  18.            
  19.         if(isFile(string)){   
  20.                
  21.             replyQuery(string);   
  22.                
  23.             string = updatePrevious();   
  24.         }   
  25.            
  26.         return list(string);   
  27.     }   
  28.   
  29.   
  30. public String updateNext(String s){   
  31.         parent = target;   
  32.         target += "/" + s;   
  33.            
  34.         return target;   
  35.     }  

 

 

5. 返回目标目录的上级目录 并显示其所有文件/目录 如何注册: Button id=R.id.previous

Java代码 复制代码
  1. previous.setOnClickListener(new OnClickListener(){   
  2.             public void onClick(View v) {   
  3.                 // TODO Auto-generated method stub   
  4.                 value = getPreviousList();   
  5.                    
  6.                 adapter.notifyDataSetChanged();   
  7.             }   
  8.         });   
  9.   
  10.   
  11. public List<Map<String,String>> getPreviousList(){   
  12.         String string = updatePrevious();   
  13.            
  14.         return list(string);   
  15.     }   
  16.   
  17.   
  18. public String updatePrevious(){   
  19.         target = parent;   
  20.            
  21.         File file = new File(parent);   
  22.         parent = file.getParent();   
  23.            
  24.         return target;   
  25.     }  

 

 

 

6. emulator 的运行截图:

* 整体布局:

 

* 文件选择:

 

* 播放界面

抱歉!评论已关闭.