码迷,mamicode.com
首页 > 其他好文 > 详细

使用SurfaceView和MediaPlayer实现视频做为背景

时间:2015-06-29 13:25:06      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:surfaceview 视频动画

场景:像我们在Uber应用开场,看到一一段视频作为开始,这样子让用户很快投入应用使用的场景中去,这种以视频作为开场的应用,我们是不是觉得很高大上呢,哈哈,其实是使用了SerfaceView去加载一段小视频,然后使用mediaplayer进行播放,然后循环。

先上图吧。

技术分享

这里我贴出主要代码的实现

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private SurfaceView surfaceview;
	private Button btnGo;
	private MediaPlayer mediaPlayer;

	private int postion = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById();
		initView();
	}

	protected void findViewById() {
		// TODO Auto-generated method stub
		surfaceview = (SurfaceView) findViewById(R.id.surfaceView);
		btnGo = (Button) findViewById(R.id.btn_goto);

	}

	protected void initView() {
		// TODO Auto-generated method stub
		mediaPlayer = new MediaPlayer();
		surfaceview.getHolder().setKeepScreenOn(true);
		surfaceview.getHolder().addCallback(new SurfaceViewLis());
		btnGo.setOnClickListener(this);
	}

	private class SurfaceViewLis implements SurfaceHolder.Callback {

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {

		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			if (postion == 0) {
				try {
					play();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {

		}

	}

	public void play() throws IllegalArgumentException, SecurityException,
			IllegalStateException, IOException {
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		AssetFileDescriptor fd = this.getAssets().openFd("start.mp4");
		mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(),
				fd.getLength());
		mediaPlayer.setLooping(true);
		mediaPlayer.setDisplay(surfaceview.getHolder());
		// 通过异步的方式装载媒体资源
		mediaPlayer.prepareAsync();
		mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				// 装载完毕回调
				mediaPlayer.start();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_goto:
			startActivity(new Intent(this, OtherActivity.class));
			finish();
			break;
		default:
			break;
		}
	}
}

完整的demo请到我的github项目主页进行下载

https://github.com/paulzeng/surfaceviewbackground

点击打开链接

期待大家一起follow,加入更酷的效果!

使用SurfaceView和MediaPlayer实现视频做为背景

标签:surfaceview 视频动画

原文地址:http://blog.csdn.net/kaka89757/article/details/46680535

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!