use of android.media.videoeditor.MediaItem in project XobotOS by xamarin.
the class VideoEditorImpl method removeTransitionAfter.
/**
* Remove the transition after this media item
*
* @param mediaItem The media item
*/
private void removeTransitionAfter(int index) {
final MediaItem mediaItem = mMediaItems.get(index);
final Iterator<Transition> it = mTransitions.iterator();
while (it.hasNext()) {
Transition t = it.next();
if (t.getAfterMediaItem() == mediaItem) {
mMANativeHelper.setGeneratePreview(true);
it.remove();
t.invalidate();
mediaItem.setEndTransition(null);
/**
* Invalidate the reference in the next media item
*/
if (index < mMediaItems.size() - 1) {
mMediaItems.get(index + 1).setBeginTransition(null);
}
break;
}
}
}
use of android.media.videoeditor.MediaItem in project XobotOS by xamarin.
the class VideoEditorImpl method load.
/**
* the project form XML
*/
private void load() throws FileNotFoundException, XmlPullParserException, IOException {
final File file = new File(mProjectPath, PROJECT_FILENAME);
/**
* Load the metadata
*/
final FileInputStream fis = new FileInputStream(file);
try {
final List<String> ignoredMediaItems = new ArrayList<String>();
final XmlPullParser parser = Xml.newPullParser();
parser.setInput(fis, "UTF-8");
int eventType = parser.getEventType();
String name;
MediaItem currentMediaItem = null;
Overlay currentOverlay = null;
boolean regenerateProjectThumbnail = false;
while (eventType != XmlPullParser.END_DOCUMENT) {
switch(eventType) {
case XmlPullParser.START_TAG:
{
name = parser.getName();
if (TAG_PROJECT.equals(name)) {
mAspectRatio = Integer.parseInt(parser.getAttributeValue("", ATTR_ASPECT_RATIO));
final boolean mRegenPCM = Boolean.parseBoolean(parser.getAttributeValue("", ATTR_REGENERATE_PCM));
mMANativeHelper.setAudioflag(mRegenPCM);
} else if (TAG_MEDIA_ITEM.equals(name)) {
final String mediaItemId = parser.getAttributeValue("", ATTR_ID);
try {
currentMediaItem = parseMediaItem(parser);
mMediaItems.add(currentMediaItem);
} catch (Exception ex) {
Log.w(TAG, "Cannot load media item: " + mediaItemId, ex);
currentMediaItem = null;
// First media item is invalid, mark for project thumbnail removal
if (mMediaItems.size() == 0) {
regenerateProjectThumbnail = true;
}
// Ignore the media item
ignoredMediaItems.add(mediaItemId);
}
} else if (TAG_TRANSITION.equals(name)) {
try {
final Transition transition = parseTransition(parser, ignoredMediaItems);
// media items are ignored
if (transition != null) {
mTransitions.add(transition);
}
} catch (Exception ex) {
Log.w(TAG, "Cannot load transition", ex);
}
} else if (TAG_OVERLAY.equals(name)) {
if (currentMediaItem != null) {
try {
currentOverlay = parseOverlay(parser, currentMediaItem);
currentMediaItem.addOverlay(currentOverlay);
} catch (Exception ex) {
Log.w(TAG, "Cannot load overlay", ex);
}
}
} else if (TAG_OVERLAY_USER_ATTRIBUTES.equals(name)) {
if (currentOverlay != null) {
final int attributesCount = parser.getAttributeCount();
for (int i = 0; i < attributesCount; i++) {
currentOverlay.setUserAttribute(parser.getAttributeName(i), parser.getAttributeValue(i));
}
}
} else if (TAG_EFFECT.equals(name)) {
if (currentMediaItem != null) {
try {
final Effect effect = parseEffect(parser, currentMediaItem);
currentMediaItem.addEffect(effect);
if (effect instanceof EffectKenBurns) {
final boolean isImageClipGenerated = Boolean.parseBoolean(parser.getAttributeValue("", ATTR_IS_IMAGE_CLIP_GENERATED));
if (isImageClipGenerated) {
final String filename = parser.getAttributeValue("", ATTR_GENERATED_IMAGE_CLIP);
if (new File(filename).exists() == true) {
((MediaImageItem) currentMediaItem).setGeneratedImageClip(filename);
((MediaImageItem) currentMediaItem).setRegenerateClip(false);
} else {
((MediaImageItem) currentMediaItem).setGeneratedImageClip(null);
((MediaImageItem) currentMediaItem).setRegenerateClip(true);
}
} else {
((MediaImageItem) currentMediaItem).setGeneratedImageClip(null);
((MediaImageItem) currentMediaItem).setRegenerateClip(true);
}
}
} catch (Exception ex) {
Log.w(TAG, "Cannot load effect", ex);
}
}
} else if (TAG_AUDIO_TRACK.equals(name)) {
try {
final AudioTrack audioTrack = parseAudioTrack(parser);
addAudioTrack(audioTrack);
} catch (Exception ex) {
Log.w(TAG, "Cannot load audio track", ex);
}
}
break;
}
case XmlPullParser.END_TAG:
{
name = parser.getName();
if (TAG_MEDIA_ITEM.equals(name)) {
currentMediaItem = null;
} else if (TAG_OVERLAY.equals(name)) {
currentOverlay = null;
}
break;
}
default:
{
break;
}
}
eventType = parser.next();
}
computeTimelineDuration();
// Regenerate project thumbnail
if (regenerateProjectThumbnail) {
generateProjectThumbnail();
regenerateProjectThumbnail = false;
}
} finally {
if (fis != null) {
fis.close();
}
}
}
use of android.media.videoeditor.MediaItem in project XobotOS by xamarin.
the class VideoEditorImpl method generateProjectThumbnail.
/*
* Generate the project thumbnail
*/
private void generateProjectThumbnail() {
/*
* If a thumbnail already exists, then delete it first
*/
if ((new File(mProjectPath + "/" + THUMBNAIL_FILENAME)).exists()) {
(new File(mProjectPath + "/" + THUMBNAIL_FILENAME)).delete();
}
/*
* Generate a new thumbnail for the project from first media Item
*/
if (mMediaItems.size() > 0) {
MediaItem mI = mMediaItems.get(0);
/*
* Keep aspect ratio of the image
*/
int height = 480;
int width = mI.getWidth() * height / mI.getHeight();
Bitmap projectBitmap = null;
String filename = mI.getFilename();
if (mI instanceof MediaVideoItem) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filename);
Bitmap bitmap = retriever.getFrameAtTime();
retriever.release();
retriever = null;
if (bitmap == null) {
String msg = "Thumbnail extraction from " + filename + " failed";
throw new IllegalArgumentException(msg);
}
// Resize the thumbnail to the target size
projectBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
} else {
try {
projectBitmap = mI.getThumbnail(width, height, 500);
} catch (IllegalArgumentException e) {
String msg = "Project thumbnail extraction from " + filename + " failed";
throw new IllegalArgumentException(msg);
} catch (IOException e) {
String msg = "IO Error creating project thumbnail";
throw new IllegalArgumentException(msg);
}
}
try {
FileOutputStream stream = new FileOutputStream(mProjectPath + "/" + THUMBNAIL_FILENAME);
projectBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
} catch (IOException e) {
throw new IllegalArgumentException("Error creating project thumbnail");
} finally {
projectBitmap.recycle();
}
}
}
use of android.media.videoeditor.MediaItem in project XobotOS by xamarin.
the class VideoEditorImpl method save.
/*
* {@inheritDoc}
*/
public void save() throws IOException {
final XmlSerializer serializer = Xml.newSerializer();
final StringWriter writer = new StringWriter();
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag("", TAG_PROJECT);
serializer.attribute("", ATTR_ASPECT_RATIO, Integer.toString(mAspectRatio));
serializer.attribute("", ATTR_REGENERATE_PCM, Boolean.toString(mMANativeHelper.getAudioflag()));
serializer.startTag("", TAG_MEDIA_ITEMS);
for (MediaItem mediaItem : mMediaItems) {
serializer.startTag("", TAG_MEDIA_ITEM);
serializer.attribute("", ATTR_ID, mediaItem.getId());
serializer.attribute("", ATTR_TYPE, mediaItem.getClass().getSimpleName());
serializer.attribute("", ATTR_FILENAME, mediaItem.getFilename());
serializer.attribute("", ATTR_RENDERING_MODE, Integer.toString(mediaItem.getRenderingMode()));
if (mediaItem instanceof MediaVideoItem) {
final MediaVideoItem mvi = (MediaVideoItem) mediaItem;
serializer.attribute("", ATTR_BEGIN_TIME, Long.toString(mvi.getBoundaryBeginTime()));
serializer.attribute("", ATTR_END_TIME, Long.toString(mvi.getBoundaryEndTime()));
serializer.attribute("", ATTR_VOLUME, Integer.toString(mvi.getVolume()));
serializer.attribute("", ATTR_MUTED, Boolean.toString(mvi.isMuted()));
if (mvi.getAudioWaveformFilename() != null) {
serializer.attribute("", ATTR_AUDIO_WAVEFORM_FILENAME, mvi.getAudioWaveformFilename());
}
} else if (mediaItem instanceof MediaImageItem) {
serializer.attribute("", ATTR_DURATION, Long.toString(mediaItem.getTimelineDuration()));
}
final List<Overlay> overlays = mediaItem.getAllOverlays();
if (overlays.size() > 0) {
serializer.startTag("", TAG_OVERLAYS);
for (Overlay overlay : overlays) {
serializer.startTag("", TAG_OVERLAY);
serializer.attribute("", ATTR_ID, overlay.getId());
serializer.attribute("", ATTR_TYPE, overlay.getClass().getSimpleName());
serializer.attribute("", ATTR_BEGIN_TIME, Long.toString(overlay.getStartTime()));
serializer.attribute("", ATTR_DURATION, Long.toString(overlay.getDuration()));
if (overlay instanceof OverlayFrame) {
final OverlayFrame overlayFrame = (OverlayFrame) overlay;
overlayFrame.save(getPath());
if (overlayFrame.getBitmapImageFileName() != null) {
serializer.attribute("", ATTR_FILENAME, overlayFrame.getBitmapImageFileName());
}
if (overlayFrame.getFilename() != null) {
serializer.attribute("", ATTR_OVERLAY_RGB_FILENAME, overlayFrame.getFilename());
serializer.attribute("", ATTR_OVERLAY_FRAME_WIDTH, Integer.toString(overlayFrame.getOverlayFrameWidth()));
serializer.attribute("", ATTR_OVERLAY_FRAME_HEIGHT, Integer.toString(overlayFrame.getOverlayFrameHeight()));
serializer.attribute("", ATTR_OVERLAY_RESIZED_RGB_FRAME_WIDTH, Integer.toString(overlayFrame.getResizedRGBSizeWidth()));
serializer.attribute("", ATTR_OVERLAY_RESIZED_RGB_FRAME_HEIGHT, Integer.toString(overlayFrame.getResizedRGBSizeHeight()));
}
}
/**
* Save the user attributes
*/
serializer.startTag("", TAG_OVERLAY_USER_ATTRIBUTES);
final Map<String, String> userAttributes = overlay.getUserAttributes();
for (String name : userAttributes.keySet()) {
final String value = userAttributes.get(name);
if (value != null) {
serializer.attribute("", name, value);
}
}
serializer.endTag("", TAG_OVERLAY_USER_ATTRIBUTES);
serializer.endTag("", TAG_OVERLAY);
}
serializer.endTag("", TAG_OVERLAYS);
}
final List<Effect> effects = mediaItem.getAllEffects();
if (effects.size() > 0) {
serializer.startTag("", TAG_EFFECTS);
for (Effect effect : effects) {
serializer.startTag("", TAG_EFFECT);
serializer.attribute("", ATTR_ID, effect.getId());
serializer.attribute("", ATTR_TYPE, effect.getClass().getSimpleName());
serializer.attribute("", ATTR_BEGIN_TIME, Long.toString(effect.getStartTime()));
serializer.attribute("", ATTR_DURATION, Long.toString(effect.getDuration()));
if (effect instanceof EffectColor) {
final EffectColor colorEffect = (EffectColor) effect;
serializer.attribute("", ATTR_COLOR_EFFECT_TYPE, Integer.toString(colorEffect.getType()));
if (colorEffect.getType() == EffectColor.TYPE_COLOR || colorEffect.getType() == EffectColor.TYPE_GRADIENT) {
serializer.attribute("", ATTR_COLOR_EFFECT_VALUE, Integer.toString(colorEffect.getColor()));
}
} else if (effect instanceof EffectKenBurns) {
final Rect startRect = ((EffectKenBurns) effect).getStartRect();
serializer.attribute("", ATTR_START_RECT_LEFT, Integer.toString(startRect.left));
serializer.attribute("", ATTR_START_RECT_TOP, Integer.toString(startRect.top));
serializer.attribute("", ATTR_START_RECT_RIGHT, Integer.toString(startRect.right));
serializer.attribute("", ATTR_START_RECT_BOTTOM, Integer.toString(startRect.bottom));
final Rect endRect = ((EffectKenBurns) effect).getEndRect();
serializer.attribute("", ATTR_END_RECT_LEFT, Integer.toString(endRect.left));
serializer.attribute("", ATTR_END_RECT_TOP, Integer.toString(endRect.top));
serializer.attribute("", ATTR_END_RECT_RIGHT, Integer.toString(endRect.right));
serializer.attribute("", ATTR_END_RECT_BOTTOM, Integer.toString(endRect.bottom));
final MediaItem mItem = effect.getMediaItem();
if (((MediaImageItem) mItem).getGeneratedImageClip() != null) {
serializer.attribute("", ATTR_IS_IMAGE_CLIP_GENERATED, Boolean.toString(true));
serializer.attribute("", ATTR_GENERATED_IMAGE_CLIP, ((MediaImageItem) mItem).getGeneratedImageClip());
} else {
serializer.attribute("", ATTR_IS_IMAGE_CLIP_GENERATED, Boolean.toString(false));
}
}
serializer.endTag("", TAG_EFFECT);
}
serializer.endTag("", TAG_EFFECTS);
}
serializer.endTag("", TAG_MEDIA_ITEM);
}
serializer.endTag("", TAG_MEDIA_ITEMS);
serializer.startTag("", TAG_TRANSITIONS);
for (Transition transition : mTransitions) {
serializer.startTag("", TAG_TRANSITION);
serializer.attribute("", ATTR_ID, transition.getId());
serializer.attribute("", ATTR_TYPE, transition.getClass().getSimpleName());
serializer.attribute("", ATTR_DURATION, Long.toString(transition.getDuration()));
serializer.attribute("", ATTR_BEHAVIOR, Integer.toString(transition.getBehavior()));
serializer.attribute("", ATTR_IS_TRANSITION_GENERATED, Boolean.toString(transition.isGenerated()));
if (transition.isGenerated() == true) {
serializer.attribute("", ATTR_GENERATED_TRANSITION_CLIP, transition.mFilename);
}
final MediaItem afterMediaItem = transition.getAfterMediaItem();
if (afterMediaItem != null) {
serializer.attribute("", ATTR_AFTER_MEDIA_ITEM_ID, afterMediaItem.getId());
}
final MediaItem beforeMediaItem = transition.getBeforeMediaItem();
if (beforeMediaItem != null) {
serializer.attribute("", ATTR_BEFORE_MEDIA_ITEM_ID, beforeMediaItem.getId());
}
if (transition instanceof TransitionSliding) {
serializer.attribute("", ATTR_DIRECTION, Integer.toString(((TransitionSliding) transition).getDirection()));
} else if (transition instanceof TransitionAlpha) {
TransitionAlpha ta = (TransitionAlpha) transition;
serializer.attribute("", ATTR_BLENDING, Integer.toString(ta.getBlendingPercent()));
serializer.attribute("", ATTR_INVERT, Boolean.toString(ta.isInvert()));
if (ta.getMaskFilename() != null) {
serializer.attribute("", ATTR_MASK, ta.getMaskFilename());
}
}
serializer.endTag("", TAG_TRANSITION);
}
serializer.endTag("", TAG_TRANSITIONS);
serializer.startTag("", TAG_AUDIO_TRACKS);
for (AudioTrack at : mAudioTracks) {
serializer.startTag("", TAG_AUDIO_TRACK);
serializer.attribute("", ATTR_ID, at.getId());
serializer.attribute("", ATTR_FILENAME, at.getFilename());
serializer.attribute("", ATTR_START_TIME, Long.toString(at.getStartTime()));
serializer.attribute("", ATTR_BEGIN_TIME, Long.toString(at.getBoundaryBeginTime()));
serializer.attribute("", ATTR_END_TIME, Long.toString(at.getBoundaryEndTime()));
serializer.attribute("", ATTR_VOLUME, Integer.toString(at.getVolume()));
serializer.attribute("", ATTR_DUCK_ENABLED, Boolean.toString(at.isDuckingEnabled()));
serializer.attribute("", ATTR_DUCKED_TRACK_VOLUME, Integer.toString(at.getDuckedTrackVolume()));
serializer.attribute("", ATTR_DUCK_THRESHOLD, Integer.toString(at.getDuckingThreshhold()));
serializer.attribute("", ATTR_MUTED, Boolean.toString(at.isMuted()));
serializer.attribute("", ATTR_LOOP, Boolean.toString(at.isLooping()));
if (at.getAudioWaveformFilename() != null) {
serializer.attribute("", ATTR_AUDIO_WAVEFORM_FILENAME, at.getAudioWaveformFilename());
}
serializer.endTag("", TAG_AUDIO_TRACK);
}
serializer.endTag("", TAG_AUDIO_TRACKS);
serializer.endTag("", TAG_PROJECT);
serializer.endDocument();
/**
* Save the metadata XML file
*/
final FileOutputStream out = new FileOutputStream(new File(getPath(), PROJECT_FILENAME));
out.write(writer.toString().getBytes());
out.flush();
out.close();
}
use of android.media.videoeditor.MediaItem in project XobotOS by xamarin.
the class VideoEditorImpl method removeMediaItem.
/*
* {@inheritDoc}
*/
public synchronized MediaItem removeMediaItem(String mediaItemId) {
final String firstItemString = mMediaItems.get(0).getId();
final MediaItem mediaItem = getMediaItem(mediaItemId);
if (mediaItem != null) {
mMANativeHelper.setGeneratePreview(true);
/**
* Remove the media item
*/
mMediaItems.remove(mediaItem);
if (mediaItem instanceof MediaImageItem) {
((MediaImageItem) mediaItem).invalidate();
}
final List<Overlay> overlays = mediaItem.getAllOverlays();
if (overlays.size() > 0) {
for (Overlay overlay : overlays) {
if (overlay instanceof OverlayFrame) {
final OverlayFrame overlayFrame = (OverlayFrame) overlay;
overlayFrame.invalidate();
}
}
}
/**
* Remove the adjacent transitions
*/
removeAdjacentTransitions(mediaItem);
computeTimelineDuration();
}
/**
* If string equals first mediaItem, then
* generate Project thumbnail
*/
if (firstItemString.equals(mediaItemId)) {
generateProjectThumbnail();
}
if (mediaItem instanceof MediaVideoItem) {
/**
* Delete the graph file
*/
((MediaVideoItem) mediaItem).invalidate();
}
return mediaItem;
}
Aggregations