Using SurfaceView and MediaPlayer as the background, the surfaceview background is transparent.
Scenario: As we saw a one-to-one video as the starting point in the Uber application, this scenario allows users to quickly get involved in application use. This application starts with a video, do we think it is very high? haha, we actually used SerfaceView to load a small video, then played it using mediaplayer, and then cyclically.
First.
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. 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 (SecurityException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IllegalStateException e) {// TODO Auto-generated 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 media resources in asynchronous mode. prepareAsync (); mediaPlayer. setOnPreparedListener (new OnPreparedListener () {@ Overridepublic void onPrepared (MediaPlayer mp) {// callback mediaPlayer after loading. start () ;}}) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_goto: startActivity (new Intent (this, OtherActivity. class); finish (); break; default: break ;}}}
For the complete demo, go to the github project homepage to download it.
Https://github.com/paulzeng/surfaceviewbackground
Click Open Link
We look forward to you follow and join us with cool results!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.