Play video using SurfaceView

Explanation:

Look at the introduction of a short video on the first startup of Bests. I originally planned to use VideoView to play the video. ! What’s nonsense is that I can’t control the size of the parent control…. There are also many pitfalls during playback with SurfaceView! ! ! ! Go directly to the code!

XML:

span><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/advideo_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background< /span>="@color/white" android:orientation="vertical"> <RelativeLayout android:id="@+id/VideoLayout" android:layout_width="fill_parent" android:layout_height span>="260.0dip" android:layout_alignParentTop=" true" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" android:layout_marginTop="110.0dip" android:background="@drawable/ad_video_bg" span>> <SurfaceView android:id ="@+id/surfaceView" android:layout_width= "fi ll_parent" android:layout_height="fill_parent" android:layout_marginBottom="10.0dip" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" < span class="hljs-attribute">android:layout_marginTop="60.0dip" /> RelativeLayout>
<Button < span class="hljs-attribute">android:id="@+id/jumpBtn" android: layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/VideoLayout" android:layout_centerHorizontal="true" android:layout_marginTop="88.0dip" android:background="@drawable/splash_jump_selected_bg" / >RelativeLayout>

JAVA:

package bsactivity;import android.app.Activity;import android.content.Intent;import android.content.res.AssetFileDescriptor;import android.media.MediaPlayer;import android.os.Bundle;import< /span> android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView ;import android.view.View;import android.widget.Button;import bsR;import bsutils.SharePrefUtil;/** * Created by Administrator on 2016/ 4/5. */public class VideoSplashActivity extends Activity implements View.OnClickListener { private SurfaceView mVideo; private MediaPlayer mMediaPlayer; private Button mJumpBtn; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate( savedInstanceState); setContentView(R.layout.activity_video_splash); mVideo = (SurfaceView) findViewById(R.id.surfaceView); mJumpBtn = (Button) findViewById(R.id.jumpBtn); mJumpBtn.setOnClickListener(this); initSurfaceView();} private void initSurfaceView() {//Keep the screen always on mVideo.getHolder().setKeepScreenOn(true); mVideo.getHolder().addCallback(new SurfaceHolder.Callback() {@Override public void surfaceCreated(SurfaceHolder holder) {initMediaPlayer();} @Override public void surfaceChanged(SurfaceHolder holder, int format , int width, int height) {} @Override< /span> public void surfaceDestroyed(SurfaceHolder holder) {mMediaPlayer.release(); Log.i(">>>>>>>>>>>>>>>>>>>>>" , "Destroyed") ;} });} private void initMediaPlayer() {try {mMediaPlayer = new MediaPlayer(); mMediaPlayer.reset();//initialization AssetFileDescriptor afd = getAssets().openFd("advideo.mp4");//Get video resources mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mMediaPlayer.setDisplay(mVideo.getHolder()); mMediaPlayer.prepareAsync (); mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@Override public void onPrepared(MediaPlayer mp) {mMediaPlayer.start();} });} catch (Exception e) {e.printStackTrace( );}} @Override protected void onDestroy() {super.onDestroy();} @ Override public void onClick (View v) {Intent intent = new Intent(VideoSplashActivity.this, SplashActivity.class); startActivity( intent); SharePrefUtil.saveBoolean(VideoSplashActivity.this, "firstLogin", true); finish(); }}

description:

look There was a short video introduction when Bests first started. I originally planned to use VideoView to play the video! What’s nonsense is that I can’t control the size of the parent control…. There are also many pitfalls during playback with SurfaceView! ! ! ! Go directly to the code!

XML:

span><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/advideo_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background< /span>="@color/white" android:orientation="vertical"> <RelativeLayout android:id="@+id/VideoLayout" android:layout_width="fill_parent" android:layout_height="260.0dip" android:layout_alignParentTop="true " android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" android:layout_marginTop="110.0dip" android:background="@drawable/ad_video_bg"> <SurfaceView android:id= "@+id/surfaceView" android:layout_width=" fill _parent" android:layout_height="fill_parent" android:layout_marginBottom="10.0dip" android:layout_marginLeft="10.0dip" android:layout_marginRight="10.0dip" < span class="hljs-attribute">android:layout_marginTop="60.0dip" />
RelativeLayout> <Button < span class="hljs-attribute">android:id="@+id/jumpBtn" android: layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/VideoLayout" android: layout_centerHorizontal="true" android:layout_marginTop="88.0dip" android:background="@drawable/splash_jump_selected_bg" />< /span>RelativeLayout>

JAVA:

package bsactivity;import android.app.Activity;import android.content.Intent;import android.content.res.AssetFileDescriptor;import  android.media.MediaPlayer;import android.os.Bundle;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView; import android.view.View;import android.widget.Button;import bsR;import bsutils.SharePrefUtil;/** * Created by Administrator on 2016/4 /5. */public class < span class="hljs-title">VideoSplashActivity extends Activity implements View.OnClickListener { private SurfaceView mVideo; private MediaPlayer mMediaPlayer; private Button mJumpBtn; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_splash); mVideo = (SurfaceView) findViewById(R.id.surfaceView); mJumpBtn = (Button) findViewById(R.id.jumpBtn); mJumpBtn.setOnClickListener(this); initSurfaceView();} private void initSurfaceView() {//Keep the screen always on mVideo.getHolder().setKeepScreenOn(true ); mVideo.getHolder().addCallback(new SurfaceHolder.Callback() {@Override public void surfaceCreated(SurfaceHolder holder) {initMediaPlayer();} @Override public void surfaceChanged(SurfaceHolder holder, int format , int width, int height) {} @Override< /span> public void surfaceDestroyed(SurfaceHolder holder) {mMediaPlayer.release(); Log.i(">>>>>>>>>>>>>>>>>>>>>" , "Destroyed"); } });} private void initMediaPlayer () {try {mMediaPlayer = new MediaPlayer(); mMediaPlayer.reset();//initialization AssetFileDescriptor afd = getAssets().openFd("advideo.mp4");//Get video resources mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mMediaPlayer.setDisplay(mVideo.getHolder()); mMediaPlayer.prepareAsync( ); mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@Override public void onPrepared(MediaPlayer mp) {mMediaPlayer.start();} });} catch (Exception e) {e.printStackTrace() ;}} @Override protected void < span class="hljs-title">onDestroy() {super.onDestroy();} @Override  public void onClick( View v) {Intent intent = new Intent(VideoSplashActivity.this, SplashActivity.class); startActivity(intent ); SharePrefUtil.saveBoolean(VideoSplashActivity.this, "firstLogin", true); finish(); }}

Leave a Comment

Your email address will not be published.