use of android.media.videoeditor.MediaImageItem in project android_frameworks_base by ParanoidAndroid.
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.MediaImageItem in project android_frameworks_base by ParanoidAndroid.
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;
}
use of android.media.videoeditor.MediaImageItem in project android_frameworks_base by ParanoidAndroid.
the class VideoEditorImpl method parseMediaItem.
/**
* Parse the media item
*
* @param parser The parser
* @return The media item
*/
private MediaItem parseMediaItem(XmlPullParser parser) throws IOException {
final String mediaItemId = parser.getAttributeValue("", ATTR_ID);
final String type = parser.getAttributeValue("", ATTR_TYPE);
final String filename = parser.getAttributeValue("", ATTR_FILENAME);
final int renderingMode = Integer.parseInt(parser.getAttributeValue("", ATTR_RENDERING_MODE));
final MediaItem currentMediaItem;
if (MediaImageItem.class.getSimpleName().equals(type)) {
final long durationMs = Long.parseLong(parser.getAttributeValue("", ATTR_DURATION));
currentMediaItem = new MediaImageItem(this, mediaItemId, filename, durationMs, renderingMode);
} else if (MediaVideoItem.class.getSimpleName().equals(type)) {
final long beginMs = Long.parseLong(parser.getAttributeValue("", ATTR_BEGIN_TIME));
final long endMs = Long.parseLong(parser.getAttributeValue("", ATTR_END_TIME));
final int volume = Integer.parseInt(parser.getAttributeValue("", ATTR_VOLUME));
final boolean muted = Boolean.parseBoolean(parser.getAttributeValue("", ATTR_MUTED));
final String audioWaveformFilename = parser.getAttributeValue("", ATTR_AUDIO_WAVEFORM_FILENAME);
currentMediaItem = new MediaVideoItem(this, mediaItemId, filename, renderingMode, beginMs, endMs, volume, muted, audioWaveformFilename);
final long beginTimeMs = Long.parseLong(parser.getAttributeValue("", ATTR_BEGIN_TIME));
final long endTimeMs = Long.parseLong(parser.getAttributeValue("", ATTR_END_TIME));
((MediaVideoItem) currentMediaItem).setExtractBoundaries(beginTimeMs, endTimeMs);
final int volumePercent = Integer.parseInt(parser.getAttributeValue("", ATTR_VOLUME));
((MediaVideoItem) currentMediaItem).setVolume(volumePercent);
} else {
throw new IllegalArgumentException("Unknown media item type: " + type);
}
return currentMediaItem;
}
use of android.media.videoeditor.MediaImageItem in project android_frameworks_base by ParanoidAndroid.
the class VideoEditorAPITest method testFrameOverlayImageItem.
/**
* To test Frame Overlay for Media Image Item
*/
@LargeTest
public void testFrameOverlayImageItem() throws Exception {
final String imageItemFilename1 = INPUT_FILE_PATH + "IMG_640x480.jpg";
final String overlayFile1 = INPUT_FILE_PATH + "IMG_640x480_Overlay1.png";
final String overlayFile2 = INPUT_FILE_PATH + "IMG_640x480_Overlay2.png";
final MediaImageItem mediaImageItem1 = mVideoEditorHelper.createMediaItem(mVideoEditor, "m1", imageItemFilename1, 10000, MediaItem.RENDERING_MODE_BLACK_BORDER);
mVideoEditor.addMediaItem(mediaImageItem1);
final Bitmap mBitmap = mVideoEditorHelper.getBitmap(overlayFile1, 640, 480);
final OverlayFrame overlayFrame1 = mVideoEditorHelper.createOverlay(mediaImageItem1, "overlayId1", mBitmap, 5000, 5000);
mediaImageItem1.addOverlay(overlayFrame1);
assertEquals("Overlay : Media Item", mediaImageItem1, overlayFrame1.getMediaItem());
assertTrue("Overlay Id", overlayFrame1.getId().equals("overlayId1"));
assertEquals("Overlay Bitmap", mBitmap, overlayFrame1.getBitmap());
assertEquals("Overlay Start Time", 5000, overlayFrame1.getStartTime());
assertEquals("Overlay Duration", 5000, overlayFrame1.getDuration());
Bitmap upddateBmp = mVideoEditorHelper.getBitmap(overlayFile2, 640, 480);
overlayFrame1.setBitmap(upddateBmp);
assertEquals("Overlay Update Bitmap", upddateBmp, overlayFrame1.getBitmap());
upddateBmp.recycle();
}
use of android.media.videoeditor.MediaImageItem in project android_frameworks_base by ParanoidAndroid.
the class VideoEditorAPITest method testTransitionFadeBlack.
/**
* To test Transition : Fade To Black with all behavior
* SPEED_UP/SPEED_DOWN/LINEAR/MIDDLE_SLOW/MIDDLE_FAST
*/
@LargeTest
public void testTransitionFadeBlack() throws Exception {
final String videoItemFilename1 = INPUT_FILE_PATH + "H264_BP_640x480_30fps_256kbps_1_17.mp4";
final String videoItemFilename2 = INPUT_FILE_PATH + "H263_profile0_176x144_15fps_128kbps_1_35.3gp";
final String imageItemFilename1 = INPUT_FILE_PATH + "IMG_1600x1200.jpg";
final String videoItemFilename3 = INPUT_FILE_PATH + "H264_BP_640x480_30fps_192kbps_1_5.mp4";
final String videoItemFilename4 = INPUT_FILE_PATH + "MPEG4_SP_640x480_15fps_256kbps_0_30.mp4";
final String videoItemFilename5 = INPUT_FILE_PATH + "H263_profile0_176x144_10fps_96kbps_0_25.3gp";
boolean flagForException = false;
final MediaVideoItem mediaVideoItem1 = mVideoEditorHelper.createMediaItem(mVideoEditor, "m1", videoItemFilename1, MediaItem.RENDERING_MODE_BLACK_BORDER);
mediaVideoItem1.setExtractBoundaries(0, 15000);
mVideoEditor.addMediaItem(mediaVideoItem1);
final MediaVideoItem mediaVideoItem2 = mVideoEditorHelper.createMediaItem(mVideoEditor, "m2", videoItemFilename2, MediaItem.RENDERING_MODE_BLACK_BORDER);
mediaVideoItem2.setExtractBoundaries(0, 15000);
mVideoEditor.addMediaItem(mediaVideoItem2);
final TransitionFadeBlack transition1And2 = mVideoEditorHelper.createTFadeBlack("transition1And2", mediaVideoItem1, mediaVideoItem2, 3000, Transition.BEHAVIOR_SPEED_UP);
mVideoEditor.addTransition(transition1And2);
assertTrue("Transition ID", transition1And2.getId().equals("transition1And2"));
assertEquals("Transtion After Media item", mediaVideoItem1, transition1And2.getAfterMediaItem());
assertEquals("Transtion Before Media item", mediaVideoItem2, transition1And2.getBeforeMediaItem());
assertEquals("Transtion Duration", 3000, transition1And2.getDuration());
assertEquals("Transtion Behavior", Transition.BEHAVIOR_SPEED_UP, transition1And2.getBehavior());
final MediaImageItem mediaImageItem3 = mVideoEditorHelper.createMediaItem(mVideoEditor, "m3", imageItemFilename1, 15000, MediaItem.RENDERING_MODE_BLACK_BORDER);
mVideoEditor.addMediaItem(mediaImageItem3);
final TransitionFadeBlack transition2And3 = mVideoEditorHelper.createTFadeBlack("transition2And3", mediaVideoItem2, mediaImageItem3, 1000, Transition.BEHAVIOR_SPEED_DOWN);
mVideoEditor.addTransition(transition2And3);
assertTrue("Transition ID", transition2And3.getId().equals("transition2And3"));
assertEquals("Transtion After Media item", mediaVideoItem2, transition2And3.getAfterMediaItem());
assertEquals("Transtion Before Media item", mediaImageItem3, transition2And3.getBeforeMediaItem());
assertEquals("Transtion Duration", 1000, transition2And3.getDuration());
assertEquals("Transtion Behavior", Transition.BEHAVIOR_SPEED_DOWN, transition2And3.getBehavior());
final MediaVideoItem mediaVideoItem4 = mVideoEditorHelper.createMediaItem(mVideoEditor, "m4", videoItemFilename3, MediaItem.RENDERING_MODE_BLACK_BORDER);
mediaVideoItem4.setExtractBoundaries(0, 20000);
mVideoEditor.addMediaItem(mediaVideoItem4);
final TransitionFadeBlack transition3And4 = mVideoEditorHelper.createTFadeBlack("transition3And4", mediaImageItem3, mediaVideoItem4, 5000, Transition.BEHAVIOR_LINEAR);
mVideoEditor.addTransition(transition3And4);
assertTrue("Transition ID", transition3And4.getId().equals("transition3And4"));
assertEquals("Transtion After Media item", mediaImageItem3, transition3And4.getAfterMediaItem());
assertEquals("Transtion Before Media item", mediaVideoItem4, transition3And4.getBeforeMediaItem());
assertEquals("Transtion Duration", 5000, transition3And4.getDuration());
assertEquals("Transtion Behavior", Transition.BEHAVIOR_LINEAR, transition3And4.getBehavior());
final MediaVideoItem mediaVideoItem5 = mVideoEditorHelper.createMediaItem(mVideoEditor, "m5", videoItemFilename4, MediaItem.RENDERING_MODE_BLACK_BORDER);
mVideoEditor.addMediaItem(mediaVideoItem5);
final TransitionFadeBlack transition4And5 = mVideoEditorHelper.createTFadeBlack("transition4And5", mediaVideoItem4, mediaVideoItem5, 8000, Transition.BEHAVIOR_MIDDLE_FAST);
mVideoEditor.addTransition(transition4And5);
assertTrue("Transition ID", transition4And5.getId().equals("transition4And5"));
assertEquals("Transtion After Media item", mediaVideoItem4, transition4And5.getAfterMediaItem());
assertEquals("Transtion Before Media item", mediaVideoItem5, transition4And5.getBeforeMediaItem());
assertEquals("Transtion Duration", 8000, transition4And5.getDuration());
assertEquals("Transtion Behavior", Transition.BEHAVIOR_MIDDLE_FAST, transition4And5.getBehavior());
final MediaVideoItem mediaVideoItem6 = mVideoEditorHelper.createMediaItem(mVideoEditor, "m6", videoItemFilename5, MediaItem.RENDERING_MODE_BLACK_BORDER);
mediaVideoItem6.setExtractBoundaries(0, 20000);
mVideoEditor.addMediaItem(mediaVideoItem6);
final TransitionFadeBlack transition5And6 = mVideoEditorHelper.createTFadeBlack("transition5And6", mediaVideoItem5, mediaVideoItem6, 2000, Transition.BEHAVIOR_MIDDLE_SLOW);
mVideoEditor.addTransition(transition5And6);
assertTrue("Transition ID", transition5And6.getId().equals("transition5And6"));
assertEquals("Transtion After Media item", mediaVideoItem5, transition5And6.getAfterMediaItem());
assertEquals("Transtion Before Media item", mediaVideoItem6, transition5And6.getBeforeMediaItem());
assertEquals("Transtion Duration", 2000, transition5And6.getDuration());
assertEquals("Transtion Behavior", Transition.BEHAVIOR_MIDDLE_SLOW, transition5And6.getBehavior());
flagForException = false;
try {
mVideoEditorHelper.createTFadeBlack("transitiond6", mediaVideoItem5, mediaVideoItem6, 2000, Transition.BEHAVIOR_SPEED_UP - 1);
} catch (IllegalArgumentException e) {
flagForException = true;
}
assertTrue("Transition FadeBlack with Invalid behavior", flagForException);
flagForException = false;
try {
mVideoEditorHelper.createTFadeBlack("transitiond6", mediaVideoItem5, mediaVideoItem6, 2000, Transition.BEHAVIOR_MIDDLE_FAST + 1);
} catch (IllegalArgumentException e) {
flagForException = true;
}
assertTrue("Transition FadeBlack with Invalid behavior", flagForException);
}
Aggregations