Advertisement
codeido

ScreenView.java

Dec 30th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. /**
  2.  * Display digital visual media.
  3.  */
  4. public class ScreenView extends FrameLayout implements SurfaceHolder.Callback {
  5.     private static final String TAG = "ScreenView";
  6.     private Context context;
  7.     private MediaPlayer mediaPlayer;
  8.     private SurfaceHolder surfaceHolder;
  9.  
  10.     public ScreenView(Context context) {
  11.         super(context);
  12.         initialize(context, null, 0);
  13.     }
  14.  
  15.     public ScreenView(Context context, AttributeSet attributeSet) {
  16.         super(context, attributeSet);
  17.         initialize(context, attributeSet, 0);
  18.     }
  19.  
  20.     public ScreenView(Context context, AttributeSet attributeSet, int defStyleAttr) {
  21.         super(context, attributeSet, defStyleAttr);
  22.         initialize(context, attributeSet, defStyleAttr);
  23.     }
  24.  
  25.     private void initialize(Context context, AttributeSet attributeSet, int defStyleAttr) {
  26.         if (this.isInEditMode()) {
  27.             return;
  28.         }
  29.  
  30.         View view = LayoutInflater.from(context).inflate(R.layout.view_screen, this, true);
  31.  
  32.         SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.surface_screen);
  33.  
  34.         this.mediaPlayer = new MediaPlayer();
  35.         this.surfaceHolder = surfaceView.getHolder();
  36.         this.surfaceHolder.addCallback(this);
  37.     }
  38.  
  39.     @Override
  40.     public void surfaceCreated(SurfaceHolder holder) {
  41.         Log.i(TAG, "surfaceCreated");
  42.         try {
  43.             this.mediaPlayer.setDisplay(this.surfaceHolder);
  44.             Uri uri = Uri.parse("android.resource://com.example.Examples/" + R.raw.sample_video);
  45.  
  46.             this.mediaPlayer.setDataSource(this.getContext(), uri);
  47.             this.mediaPlayer.prepare();
  48.  
  49.             this.mediaPlayer.start();
  50.             this.mediaPlayer.setVolume(0, 0);
  51.         } catch (Exception exception) {
  52.             exception.printStackTrace();
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  58.         Log.i(TAG, "surfaceChanged");
  59.     }
  60.  
  61.     @Override
  62.     public void surfaceDestroyed(SurfaceHolder holder) {
  63.         Log.i(TAG, "surfaceDestroyed");
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement