VideoView播放SD卡上视频的例子
2014-04-13 13:09
127 查看
先上代码:
Java代码
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文件
Java代码
<?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文件
Java代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="darkgray">#808080</drawable>
<drawable name="white">#FFFFFF</drawable>
<drawable name="blue">#0000FF</drawable>
</resources>
以上只是简单播放本地视频,稍后会贴上播放网络视频的例子
Java代码
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文件
Java代码
<?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文件
Java代码
<?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视频播放让VideoView充满整个父控件的解决方案(附例子)
- VideoView播放SD卡上指定视频文件
- 安卓使用VideoView播放raw目录的视频和sd卡的视频
- Android VideoView播放视频
- [置顶] 《android多媒体api》之VideoView 视频播放控件
- 多媒体之播放视频——VideoView和MediaPlayer(MediaPlayer和SurfaceView结合)
- Android模仿VideoView实现视频播放
- 用VideoView和SurfaceView播放视频
- VideoView用于播放视频03
- ijvideoview(播放视频,可播放https)
- 自定义VideoView播放网络视频
- 使用VideoView播放视频
- Android-ScalableVideoView ListView列表每一个播放多个视频
- Android VideoView播放视频(1)
- Android使用VideoView播放当前工程中的视频文件
- 使用VideoView全屏播放视频文件
- 控制webview使用html5的video播放视频不全屏(inline)的方法
- Android VideoView播放视频
- Android使用VideoView播放视频的方法
- anroid VideoView开始播放前设置视频缩略图