使用SurfaceView播放视频
2016-04-05 16:40
369 查看
说明:
看百思上面第一次启动时有个视频短片介绍,本来打算用VideoView播放视频呢!可扯淡的是怎么也不能控制与父控件的大小…..索性用SurfaceView进行播放期间也有可多坑!!!!直接上代码吧!XML:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/advideo_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" android:orientation="vertical"> <RelativeLayout android:id="@+id/VideoLayout" android:layout_width="fill_parent" android:layout_height="260.0dip" android:layout_alignParentTop="true" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" android:layout_marginTop="110.0dip" android:background="@drawable/ad_video_bg"> <SurfaceView android:id="@+id/surfaceView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="10.0dip" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" android:layout_marginTop="60.0dip" /> </RelativeLayout> <Button android:id="@+id/jumpBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/VideoLayout" android:layout_centerHorizontal="true" android:layout_marginTop="88.0dip" android:background="@drawable/splash_jump_selected_bg" /> </RelativeLayout>
JAVA:
package b.s.activity; import android.app.Activity; import android.content.Intent; import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import b.s.R; import b.s.utils.SharePrefUtil; /** * Created by Administrator on 2016/4/5. */ public class VideoSplashActivity extends Activity implements View.OnClickListener { private SurfaceView mVideo; private MediaPlayer mMediaPlayer; private Button mJumpBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_splash); mVideo = (SurfaceView) findViewById(R.id.surfaceView); mJumpBtn = (Button) findViewById(R.id.jumpBtn); mJumpBtn.setOnClickListener(this); initSurfaceView(); } private void initSurfaceView() { //保持屏幕常亮 mVideo.getHolder().setKeepScreenOn(true); mVideo.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { initMediaPlayer(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { mMediaPlayer.release(); Log.i(">>>>>>>>>>>>>>>>>>>>>", "销毁了"); } }); } private void initMediaPlayer() { try { mMediaPlayer = new MediaPlayer(); mMediaPlayer.reset();//初始化 AssetFileDescriptor afd = getAssets().openFd("advideo.mp4");//获取视频资源 mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mMediaPlayer.setDisplay(mVideo.getHolder()); mMediaPlayer.prepareAsync(); mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mMediaPlayer.start(); } }); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); } @Override public void onClick(View v) { Intent intent = new Intent(VideoSplashActivity.this, SplashActivity.class); startActivity(intent); SharePrefUtil.saveBoolean(VideoSplashActivity.this, "firstLogin", true); finish(); } }
相关文章推荐
- 通过get或post请求URL获取返回数据
- #pragma和#ifndef的区别
- java多线程之守护线程(Daemon)
- mysql 之mvcc多版本控制
- LOOPS(HDU3853)
- 第一天学习,错误汇总
- Spring MVC 全局异常处理&文件上传
- 数据结构实现循环队列的两种方法
- 一个popen异步的例子
- idea tomcat 热部署
- 游戏开发策划工作(6)
- 阿里云主机windows系统Apache启用浏览器缓存的方法
- Linux内核分析第三章读书笔记
- django之创建第8-3个项目-数据库数据提取之高级操作
- the usage of vi on mac
- PS制作超酷炫的碎裂火焰美女
- c++封装之对象成员基础
- 干活 spring依赖注入
- SQL自定义函数--计算两个坐标点间距离
- 软件工程导论第五周作业:关于上周自我回忆录(html,div+css,javascript,jsp,jdbc)