Video library for processing doesn’t work in APDE. But i need load/export videos feature in my app. Is there any way to load/export video in apde?
Bit old post but whatever.
This works on APDE.
But not in the preview! You have to make an app.
This example requires a “test.mp4” in the data folder to load into the mediaplayer.
Exporting i don’t know.
import android.media.MediaMetadataRetriever;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.app.Activity;
import android.view.ViewGroup;
import android.view.View;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.content.res.Resources;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.Context;
import android.os.Environment;
AssetFileDescriptor afd;
Context context;
Activity act;
SurfaceView mySurface;
SurfaceHolder mSurfaceHolder;
MediaMetadataRetriever metaRetriever;
MediaPlayer mMediaPlayer;
//String sdcard = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
String fileName = "test.mp4";
// this one is in the data folder.
// for a other path/file adjust accordingly.
void setup() {
size(displayWidth, displayHeight, P2D);
act = this.getActivity();
context = act.getApplicationContext();
Looper.prepare();
mMediaPlayer = new MediaPlayer();
try {
// here the file gets prepared/opened
afd = context.getAssets().openFd(fileName);
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() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mMediaPlayer.setDisplay(surfaceHolder);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
mMediaPlayer.setDisplay(surfaceHolder);
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
}
);
startVideo();
}
void startVideo() {
act.runOnUiThread(new Runnable() {
public void run() {
try {
// here the file gets actually loaded into the mediaplayer
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mSurfaceHolder = mySurface.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mMediaPlayer.prepare();
// act.addContentView(mySurface, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
act.addContentView(mySurface, new ViewGroup.LayoutParams(400,400));
if (mMediaPlayer.isPlaying() == false) {
mMediaPlayer.start();
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
);
};
void draw() {
}
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() ;
}
2 Likes
great! it works!!!
Now I need to know how can I extract pictures from videos?
sorry for the late reply but I don’t always have internet.
and unfortunately I can’t help you with exporting images from the MediaPlayer…
but try a search on stackexchange for something like: ‘android save image from MediaPlayer’ and you should get some helpful results
2 Likes