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.
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.