Android实现的视频背景
16lz
2021-01-26
Android APP实现视频背景
步骤
1.导入Class
2.在需要使用的Activity中重写方法,加载本地资源
3.使用帧,布局中使用自定义Video控件并FULL全屏
第一步 导入重写的VideoView class
package com.scxx.wangli_qq.UserControl;import android.content.Context;import android.media.MediaPlayer;import android.util.AttributeSet;import android.view.KeyEvent;import android.widget.VideoView;public class UserVideoView extends VideoView { public UserVideoView(Context context) { super(context); } public UserVideoView(Context context, AttributeSet attrs) { super(context, attrs); } public UserVideoView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //我们重新计算高度 int width = getDefaultSize(0, widthMeasureSpec); int height = getDefaultSize(0, heightMeasureSpec); setMeasuredDimension(width, height); } @Override public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) { super.setOnPreparedListener(l); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); }}
在需要使用的Activity中重写方法,加载本地资源
/** * 视频加载 */ //返回重启加载 @Override protected void onRestart() { super.onRestart(); InitView(); } //防止锁屏或者切出的时候,音乐在播放 @Override protected void onStop() { super.onStop(); userVideoView.stopPlayback(); } public void InitView() { userVideoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video)); userVideoView.start(); userVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { userVideoView.start(); } }); }
InitView函数需要在onCreate方法中调用,才能生效
R.raw.video为视频资源
使用帧
使用帧布局,并把自定义控件放在第一位
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.scxx.wangli_qq.UserControl.UserVideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/Video_player"/></FrameLayout>
更多相关文章
- android ListView向上滑动隐藏标题,下拉显示标题栏
- Android获取assets目录下的文件和图片
- EventBus源码解析
- 菜单栏上没有android机器人的图标(AVD)的解决方法
- Android(安卓)root的两种方法 -- udev漏洞和setuid漏洞
- Android(安卓)listview多类型item问题
- android 铃声设置流程
- android 中如何获取camera当前状态
- android获取时间差的方法