edison_cool911 发表于 2013-1-15 02:26:30

VideoView播放SD卡上视频的例子

先上代码:
package cn.com;import android.app.Activity;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.MediaController;import android.widget.TextView;import android.widget.Toast;import android.widget.VideoView;public class Play3G extends Activity {private TextView videoPath;private VideoView videoview;private String strVideoPath = "";private Button video1, video2;private String TAG = "HIPPO_VIDEOVIEW";/* 默认判别是否安装存储卡flag为false */private boolean bIfSDExist = false;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);                //检查是否存在SD卡checkTheSdCard();videoPath = (TextView) findViewById(R.id.videoPath);videoview = (VideoView) findViewById(R.id.videoview);video1 = (Button) findViewById(R.id.video1);video2 = (Button) findViewById(R.id.video2);video1.setOnClickListener(new Button.OnClickListener() {public void onClick(View arg0) {// TODO Auto-generated method stubif (bIfSDExist) {/* 播放影片路径1 */strVideoPath = "file:///sdcard/test.mp4";playVideo(strVideoPath);}}});video2.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif (bIfSDExist) {/* 播放影片路径2 */strVideoPath = "file:///sdcard/test.3gp";playVideo(strVideoPath);}}});}/* 自定义以VideoView播放影片 */private void playVideo(String strPath) {if (strPath != "") {/* 调用VideoURI方法,指定解析路径 */videoview.setVideoURI(Uri.parse(strPath));/* 设置控制Bar显示于此Context中 */videoview.setMediaController(new MediaController(Play3G.this));videoview.requestFocus();/* 调用VideoView.start()自动播放 */videoview.start();if (videoview.isPlaying()) {/* 下程序不会被运行,因start()后尚需要preparing() */videoPath.setText("Now Playing:" + strPath);Log.i(TAG, strPath);}}}// 检查是否存在SD卡public void checkTheSdCard() {/* 判断存储卡是否存在 */if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {bIfSDExist = true;} else {bIfSDExist = false;mMakeTextToast(getResources().getText(R.string.str_err_nosd).toString(), true);}}// 弹出提示对话框public void mMakeTextToast(String str, boolean isLong) {if (isLong == true) {Toast.makeText(Play3G.this, str, Toast.LENGTH_LONG).show();} else {Toast.makeText(Play3G.this, str, Toast.LENGTH_SHORT).show();}}}
2.main.xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:background="@drawable/white" android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:id="@+id/videoPath" android:layout_width="fill_parent"android:layout_height="wrap_content" android:textColor="@drawable/blue"android:text="@string/hello" /><VideoView android:id="@+id/videoview" android:layout_width="320px"android:layout_height="240px" /><LinearLayout android:orientation="horizontal"android:layout_width="wrap_content" android:layout_height="wrap_content"><Button android:id="@+id/video1" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="@string/str_button1" /><Button android:id="@+id/video2" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="@string/str_button2" /></LinearLayout></LinearLayout>
3.color.xml文件
<?xml version="1.0" encoding="utf-8"?><resources><drawable name="darkgray">#808080</drawable><drawable name="white">#FFFFFF</drawable><drawable name="blue">#0000FF</drawable></resources>
以上只是简单播放本地视频,稍后会贴上播放网络视频的例子
页: [1]
查看完整版本: VideoView播放SD卡上视频的例子