VideoView播放SD卡上视频的例子 – Envisage123 – ITeye技术网站

先上代码:

Java代码  收藏代码
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.net.Uri;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.MediaController;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12. import android.widget.VideoView;  
  13.   
  14. public class Play3G extends Activity {  
  15.   
  16.     private TextView videoPath;  
  17.     private VideoView videoview;  
  18.     private String strVideoPath = “”;  
  19.     private Button video1, video2;  
  20.     private String TAG = “HIPPO_VIDEOVIEW”;  
  21.   
  22.     /* 默认判别是否安装存储卡flag为false */  
  23.     private boolean bIfSDExist = false;  
  24.   
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.                 //检查是否存在SD卡  
  31.         checkTheSdCard();  
  32.   
  33.         videoPath = (TextView) findViewById(R.id.videoPath);  
  34.         videoview = (VideoView) findViewById(R.id.videoview);  
  35.   
  36.         video1 = (Button) findViewById(R.id.video1);  
  37.         video2 = (Button) findViewById(R.id.video2);  
  38.   
  39.         video1.setOnClickListener(new Button.OnClickListener() {  
  40.             public void onClick(View arg0) {  
  41.                 // TODO Auto-generated method stub  
  42.                 if (bIfSDExist) {  
  43.                     /* 播放影片路径1 */  
  44.                     strVideoPath = “file:///sdcard/test.mp4”;  
  45.                     playVideo(strVideoPath);  
  46.                 }  
  47.             }  
  48.   
  49.         });  
  50.   
  51.         video2.setOnClickListener(new Button.OnClickListener() {  
  52.             @Override  
  53.             public void onClick(View arg0) {  
  54.                 // TODO Auto-generated method stub  
  55.                 if (bIfSDExist) {  
  56.                     /* 播放影片路径2 */  
  57.                     strVideoPath = “file:///sdcard/test.3gp”;  
  58.                     playVideo(strVideoPath);  
  59.                 }  
  60.             }  
  61.         });  
  62.   
  63.     }  
  64.   
  65.     /* 自定义以VideoView播放影片 */  
  66.     private void playVideo(String strPath) {  
  67.         if (strPath != “”) {  
  68.             /* 调用VideoURI方法,指定解析路径 */  
  69.             videoview.setVideoURI(Uri.parse(strPath));  
  70.   
  71.             /* 设置控制Bar显示于此Context中 */  
  72.             videoview.setMediaController(new MediaController(Play3G.this));  
  73.   
  74.             videoview.requestFocus();  
  75.   
  76.             /* 调用VideoView.start()自动播放 */  
  77.             videoview.start();  
  78.             if (videoview.isPlaying()) {  
  79.                 /* 下程序不会被运行,因start()后尚需要preparing() */  
  80.                 videoPath.setText(“Now Playing:” + strPath);  
  81.                 Log.i(TAG, strPath);  
  82.             }  
  83.         }  
  84.     }  
  85.   
  86.     // 检查是否存在SD卡  
  87.     public void checkTheSdCard() {  
  88.         /* 判断存储卡是否存在 */  
  89.         if (android.os.Environment.getExternalStorageState().equals(  
  90.                 android.os.Environment.MEDIA_MOUNTED)) {  
  91.             bIfSDExist = true;  
  92.         } else {  
  93.             bIfSDExist = false;  
  94.             mMakeTextToast(getResources().getText(R.string.str_err_nosd)  
  95.                     .toString(), true);  
  96.         }  
  97.     }  
  98.   
  99.     // 弹出提示对话框  
  100.     public void mMakeTextToast(String str, boolean isLong) {  
  101.         if (isLong == true) {  
  102.             Toast.makeText(Play3G.this, str, Toast.LENGTH_LONG).show();  
  103.         } else {  
  104.             Toast.makeText(Play3G.this, str, Toast.LENGTH_SHORT).show();  
  105.         }  
  106.     }  
  107. }  

2.main.xml文件

Java代码  收藏代码
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:background=“@drawable/white” android:orientation=“vertical”  
  4.     android:layout_width=“fill_parent” android:layout_height=“fill_parent”>  
  5.     <TextView android:id=“@+id/videoPath” android:layout_width=“fill_parent”  
  6.         android:layout_height=“wrap_content” android:textColor=“@drawable/blue”  
  7.         android:text=“@string/hello” />  
  8.     <VideoView android:id=“@+id/videoview” android:layout_width=“320px”  
  9.         android:layout_height=“240px” />  
  10.     <LinearLayout android:orientation=“horizontal”  
  11.         android:layout_width=“wrap_content” android:layout_height=“wrap_content”>  
  12.         <Button android:id=“@+id/video1” android:layout_width=“wrap_content”  
  13.             android:layout_height=“wrap_content” android:text=“@string/str_button1” />  
  14.         <Button android:id=“@+id/video2” android:layout_width=“wrap_content”  
  15.             android:layout_height=“wrap_content” android:text=“@string/str_button2” />  
  16.     </LinearLayout>  
  17. </LinearLayout>  

3.color.xml文件

Java代码  收藏代码
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <resources>  
  3.   <drawable name=“darkgray”>#808080</drawable>  
  4.   <drawable name=“white”>#FFFFFF</drawable>  
  5.   <drawable name=“blue”>#0000FF</drawable>  
  6. </resources>  

以上只是简单播放本地视频,稍后会贴上播放网络视频的例子

来源URL:http://edison-cool911.iteye.com/blog/696832