# Play video in background

**URL:** <https://discourse.processing.org/t/play-video-in-background/21092>\
**Category:** Processing for Android\
**Created:** [May 20, 2020, 1:43pm UTC](https://discourse.processing.org/t/play-video-in-background/21092 "2020-05-20T13:43:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mesalcode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mesalcode/32/9284_2.png) [@Mesalcode](https://discourse.processing.org/u/Mesalcode)\
**Post date:** [May 20, 2020, 1:43pm UTC](https://discourse.processing.org/t/play-video-in-background/21092/1 "2020-05-20T13:43:45Z")

</div>

Hi!

I am building an android game using processing and need to play videos in the background.  
Looking at two very long threads on the new and old forum, I found this code that is able to display video, but it is in the very foreground, so that like in the example below, the rectangle or any other structure is drawn behind it.

```auto
AssetFileDescriptor afd;
Context context;
Activity act;
SurfaceView mySurface;
SurfaceHolder mSurfaceHolder;
MediaMetadataRetriever metaRetriever;
MediaPlayer mMediaPlayer;
 
void setup(){
 fullScreen(P3D);
 setupVideo();
}
void draw(){
 fill(0);
 rect(12,12,300,300); //Should be drawn over the video
}
void setupVideo() {
  act = this.getActivity();
  context = act.getApplicationContext();
  Looper.prepare();
  mMediaPlayer = new MediaPlayer();
  try {
    afd = context.getAssets().openFd("background.mp4"); /// video is in the sketch Data folder
    MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
    metaRetriever.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); 
    if (int(height) < 2) {
      throw new IOException();
    }
  }
  catch (IllegalArgumentException e) {
    e.printStackTrace();
  }
  catch (IllegalStateException e) {
    e.printStackTrace();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }

  mySurface = new SurfaceView(act);
  mSurfaceHolder = mySurface.getHolder();
  mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
    //<a href="/two/profile/Override">@Override</a>
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
      mMediaPlayer.setDisplay(surfaceHolder);
    }
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
      mMediaPlayer.setDisplay(surfaceHolder);
    }
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    }
  }
  );
  startVideo();
}

void startVideo() {
  act.runOnUiThread(new Runnable() {
    public void run() {
      try {
        mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        mSurfaceHolder = mySurface.getHolder();
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mMediaPlayer.prepare();
        mMediaPlayer.setLooping(true);
        act.addContentView(mySurface, new ViewGroup.LayoutParams(width, height));
        if (mMediaPlayer.isPlaying() == false) {
          mMediaPlayer.start();
        }
      }
      catch (IllegalArgumentException e) {
        e.printStackTrace();
      }
      catch (IllegalStateException e) {
        e.printStackTrace();
      } 
      catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  );
};

void onPause() {
  if (mMediaPlayer!=null) {
    mMediaPlayer.release();
    mMediaPlayer = null;
  }
  super.onPause() ;
}

void onStop() {
  if (mMediaPlayer!=null) {
    mMediaPlayer.release();
    mMediaPlayer = null;
  }
  super.onStop() ;
}

void onDestroy() {
  if (mMediaPlayer!=null) {
    mMediaPlayer.release();
    mMediaPlayer = null;
  }
  super.onDestroy() ;
}

void onResume() {
  super.onResume() ;

```

Unfortunately I haven’t yet discovered a way to make the video the very background, so that I can draw over it.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [May 21, 2020, 3:58pm UTC](https://discourse.processing.org/t/play-video-in-background/21092/2 "2020-05-21T15:58:03Z")

</div>

Hi @Mesalcode  
I am afraid that is not possible.  
The sketch is drawn on a gray opaque layer.  
The video layer hovers above.  
But you can display a text or shapes on a transparent layer above the sketch and video layer. (However without processing’s functions)

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [May 21, 2020, 4:58pm UTC](https://discourse.processing.org/t/play-video-in-background/21092/3 "2020-05-21T16:58:57Z")

</div>

@Mesalcode === dont multiply your posts; i have answered on the other one…
