zoukankan      html  css  js  c++  java
  • 使用SurfaceView和MediaPlayer实现视频做为背景

    场景:像我们在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,增加更酷的效果!

  • 相关阅读:
    团队冲刺第九天
    团队冲刺第七天
    CSS 居中大全
    jquery 中fadeIn,fadeOut动画
    使用Fiddler提高前端工作效率 (实例篇)
    使用Fiddler提高前端工作效率 (介绍篇)
    python的一些学习资料(持续更新中)
    Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
    setuptools,pip,install,UnicodeDecodeError: 'ascii' codec can't decode byte.原因和解决方案
    express cookie-session解惑
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5376917.html
Copyright © 2011-2022 走看看