• 使用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,增加更酷的效果!

  • 相关阅读:
    springmvc 拦截器
    springmvc 文件上传
    springmvc 数据验证 hibernate-validator --->对象验证
    springmvc 类型转换器 数据回显及提示信息
    springmvc框架自带的异常处理器SimpleMappingExceptionResolver的使用
    如何解决JSP页面顶端报错 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    eval函数的使用之一
    【模板】负环判定
    【洛谷P1072】Hankson 的趣味题
    【洛谷P1463】反素数
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5376917.html
Copyright © 2020-2023  润新知