Use Surfaceview and MediaPlayer to implement video as a backdrop

Source: Internet
Author: User

Scenario: Like we started with the Uber app, we saw a video as the beginning. This lets the user very quickly into the application use of the scene, such as video as the opening of the application, we do not think very tall, haha, in fact, the use of Serfaceview to load a small video, and then use MediaPlayer to play. And then loop.

Go ahead.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva2frytg5nzu3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "alt=" >

Here I post the implementation of the main code

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; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findviewbyid (); Initview ();} protected void Findviewbyid () {//TODO auto-generated method Stubsurfaceview = (Surfaceview) Findviewbyid ( R.id.surfaceview); Btngo = (Button) Findviewbyid (R.id.btn_goto);} protected void Initview () {//TODO auto-generated mEthod Stubmediaplayer = new MediaPlayer (); Surfaceview.getholder (). Setkeepscreenon (True); Surfaceview.getholder (). Addcallback (New Surfaceviewlis ()); Btngo.setonclicklistener (this);} Private class Surfaceviewlis implements Surfaceholder.callback {@Overridepublic void surfacechanged (Surfaceholder Holder, int format, int width,int height) {} @Overridepublic void surfacecreated (Surfaceholder holder) {if (postion = = 0) { try {play ()} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Security Exception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IllegalStateException e) {//Todo Auto-genera Ted Catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} @Overridepublic 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 ( );//Load the Media Resource Mediaplayer.prepareasync () by asynchronous mode; Mediaplayer.setonpreparedlistener (new Onpreparedlistener () {@ overridepublic void onprepared (MediaPlayer MP) {//Load complete callback Mediaplayer.start ();});} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.btn_goto:startactivity (the new Intent (this, Otheractivity.class)); finish (); break;default:break;}}}

Full demo please download on my GitHub project homepage

Https://github.com/paulzeng/surfaceviewbackground

Click to open link

Look forward to everyone together follow, add more cool effect!

Use Surfaceview and MediaPlayer to implement video as a backdrop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.