Using SurfaceView and MediaPlayer as the background, the surfaceview background is transparent.

Source: Internet
Author: User

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.

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.