最近项目开发,老板说你放个视屏上去吧,给大家看,我说阿里云那边还没做啊,老板说你先放在本地,给人看看就中,我说好,,,好他么烦啊,还好,Android为我们提供了VideoView,不管是网络的还是本地的,傻瓜式开发,但是毕竟要做的像个样子吗,所以做了三个像样子的东东。。
- 视频刚放上去的时候不能全屏,你需要重写布局
- 老板说要加个title,好的,写了个textview,背景你就设成透明的就好。
- 虽然咱是本地的,不用下载,但好歹以后还是要从网络上访问视频video,加载是免不了的了
上代码
/**
* Created by TongXin134 on 2016-01-20.
*/
public class VideoTab extends Activity {
private VideoView video1;
MediaController mediaco;
ProgressDialog myDialog=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videotab);
video1=(VideoView)findViewById(R.id.video1);
mediaco=new MediaController(this);
video1.setMediaController(mediaco);
mediaco.setMediaPlayer(video1);
Uri uri=Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.projectvideo);
video1.setVideoURI(uri);
MediaMetadataRetriever media = new MediaMetadataRetriever();
media.setDataSource(this,uri);
Bitmap bitmap=media.getFrameAtTime();
Drawable drawable=new BitmapDrawable(bitmap);
//
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.BELOW, R.id.textviewvideo);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
video1.setLayoutParams(layoutParams);
video1.requestFocus();
myDialog = ProgressDialog.show(this, "TCQ El Centro Town Village",
"正在为您加载视频,请稍等", true);
new Thread() {
public void run() {
try {
/* 在这里写上要背景运行的程序片段 */
/* 为了明显看见效果,以暂停3秒作为示范 */
sleep(3000);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 卸载所创建的myDialog对象。
myDialog.dismiss();
video1.start();
try {
sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
video1.pause();
}
}
}.start(); /* 开始运行运行线程 */
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK){
Intent myIntent = new Intent();
myIntent = new Intent(this, mainFragment.class);
startActivity(myIntent);
this.finish();
}
return super.onKeyDown(keyCode, event);
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textviewvideo"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="El Centro Town Village Construction Site Aerial View, September 2015"
android:background="#000000"
android:textColor="#FFFFFF"
/>
<VideoView
android:id="@+id/video1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textviewvideo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>