先上代码:
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>
以上只是简单播放本地视频,稍后会贴上播放网络视频的例子
分享到:
相关推荐
Android Media-使用VideoView播放SD卡中的视频
本示例使用 VideoView 播放视频,功能上与 "使用MediaPlayer 配合 SurfaceView" 来播放视频 很相似
android视频播放,利用VideoView播放视频,编译环境为AndroidStudio,如果没有该编译器可以查找单独代码文件进行查看
android videoview视频播放
Android 使用videoview播放Rtsp视频 demo http://blog.csdn.net/huanzhongying/article/details/53032909
为了在Android应用中播放视频,Android提供了VideoView控件,它就是一个位于android.widget包下的控件,它的作用与ImageView类似,只是ImageView用于显示图片,而VideoView用于播放视频。 使用VideoView播放视频的...
Android之VideoView播放视频
本文将讲解使用VideoView播放本地视频,使用起来相对简单,在其中添加了MediaController类来实现更多的视频控制功能。
VideoView+MediaController播放网络视频 预加载视频
简单的android videoView视频播放,希望对大家会有帮助!
Android studio 的videoView播放网络视频 demo,直接把项目导入 工程中运行可用。
本Demo实现了自定义样式的MediaController以及结合VideoView的半屏视频播放和全屏视频播放
VideoView播放Rtsp的例子,直接导入eclipse,url换成需要打开的rtsp流地址即刻。
例子中使用VideoView控件播放视频。
通过VideoView播放视频的步骤: 1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件 2、调用VideoView的如下两个方法来加载指定的视频 (1)setVidePath(String path):加载path文件代表的视频 (2...
调用摄像头录像保存到SD卡中用GridView展示出,VideoView播放
Androidstudio 环境下的源码。 编译不通过请修改 project的build.gradle 的 ... app的build.gradle的 compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { ... minSdkVersion 15 targetSdkVersion 23 ... }
基于VideoView的本地文件及流媒体播放器,可播放RTSP视频流,也可以播放本地文件。 源码及开发方法,参考: http://blog.csdn.net/fm0517/article/details/46426275
该项目资源是用AS开发,用git 管理,可以使用git 命令查看具体的实现流程,使用VideoView + MediaPlayer实现播放网络rtsp流视频
实现功能:1 采用videoview 播放网络获取的视频 2 seekbar进度调显示当前播放进度 3 显示总时间和当前播放时间 4 暂停和播放按钮