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. */
@Override
public 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 stub
if (bIfSDExist) {
/* 播放影片路径1 */
strVideoPath = "file:///sdcard/test.mp4";
playVideo(strVideoPath);
}
}

});

video2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (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>

以上只是简单播放本地视频,稍后会贴上播放网络视频的例子
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值