您的位置:首页 > 移动开发 > Android开发

android实现登录页面视频播放背景

2018-03-22 13:58 756 查看
一,准备需要播放的mp4文件
二,在res文件夹下新建raw文件夹,添加准备好的mp4文件
三, 在layout文件夹下新建一个布局文件 video_background.xml,[b]用于显示VideoView视频[/b]
 背景mp4的布局文件 video_background.xml:<!--LoginActivity中include引用的样式-->  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center" />
</RelativeLayout>[b]四,在主页面添加对[b]video_background.xml的引用[/b][/b]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.LoginActivity">

<!-- 引入背景布局 -->
<include layout="@layout/video_background" />
.......
</RelativeLayout>
,在需要添加视频背景的activity中[b]OnCreate()方法中加入相关代码[/b]
  final VideoView videoview=(VideoView)findViewById(R.id.videoview);  
final String videoPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.文件名).toString();
myVideoView.setVideoPath(videoPath);
myVideoView.start();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setLooping(true);
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
myVideoView.setVideoPath(videoPath);
myVideoView.start();
}
});
效果图列举一下喽:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android