Search in sources :

Example 31 with MediaImageItem

use of android.media.videoeditor.MediaImageItem 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();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) XmlPullParser(org.xmlpull.v1.XmlPullParser) MediaImageItem(android.media.videoeditor.MediaImageItem) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) MediaItem(android.media.videoeditor.MediaItem) File(java.io.File)

Example 32 with MediaImageItem

use of android.media.videoeditor.MediaImageItem 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();
}
Also used : Rect(android.graphics.Rect) MediaImageItem(android.media.videoeditor.MediaImageItem) StringWriter(java.io.StringWriter) MediaItem(android.media.videoeditor.MediaItem) FileOutputStream(java.io.FileOutputStream) File(java.io.File) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 33 with MediaImageItem

use of android.media.videoeditor.MediaImageItem 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;
}
Also used : MediaItem(android.media.videoeditor.MediaItem) MediaImageItem(android.media.videoeditor.MediaImageItem)

Example 34 with MediaImageItem

use of android.media.videoeditor.MediaImageItem in project android_frameworks_base by ParanoidAndroid.

the class VideoEditorHelper method createMediaItem.

/**
     *This Method Create a Media Image Item with the specified params
     *
     * @return an Object of MediaImageItem
     */
public MediaImageItem createMediaItem(VideoEditor videoEditor, String MediaId, String filename, long duration, int renderingMode) {
    MediaImageItem mii = null;
    try {
        mii = new MediaImageItem(videoEditor, MediaId, filename, duration, renderingMode);
        assertNotNull("Can not create an object of MediaImageItem", mii);
    } catch (IllegalArgumentException e) {
        assertTrue("Can not create an object of Media Image with file name = " + filename + " Issue = " + e.toString(), false);
    } catch (IOException e) {
        assertTrue("Can not create an object of Media Image with file name = " + filename + " Issue = " + e.toString(), false);
    }
    return mii;
}
Also used : MediaImageItem(android.media.videoeditor.MediaImageItem) IOException(java.io.IOException)

Example 35 with MediaImageItem

use of android.media.videoeditor.MediaImageItem in project android_frameworks_base by ParanoidAndroid.

the class MediaItemThumbnailTest method testThumbnailListVGAImage.

/**
     *To test ThumbnailList for VGA Image
     */
@LargeTest
public void testThumbnailListVGAImage() throws Exception {
    final String imageItemFilename = INPUT_FILE_PATH + "IMG_640x480.jpg";
    final int imageItemDuration = 10000;
    final int startTime = 0;
    final int endTime = 0;
    final int tnCount = 10;
    final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
    final MediaImageItem mediaImageItem = mVideoEditorHelper.createMediaItem(mVideoEditor, "m1", imageItemFilename, imageItemDuration, renderingMode);
    final int outWidth = mediaImageItem.getWidth() / 2;
    final int outHeight = mediaImageItem.getHeight() / 2;
    final Bitmap[] thumbNailBmp = mediaImageItem.getThumbnailList(outWidth, outHeight, startTime, endTime, tnCount);
    assertNotNull("Thumbnail Retrived is Null", thumbNailBmp);
    assertEquals("Thumbnail Count", tnCount, thumbNailBmp.length);
    for (int i = 0; i < thumbNailBmp.length; i++) {
        validateThumbnail(thumbNailBmp[i], outWidth, outHeight);
        thumbNailBmp[i] = null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) MediaImageItem(android.media.videoeditor.MediaImageItem) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Aggregations

MediaImageItem (android.media.videoeditor.MediaImageItem)59 LargeTest (android.test.suitebuilder.annotation.LargeTest)49 MediaVideoItem (android.media.videoeditor.MediaVideoItem)28 Bitmap (android.graphics.Bitmap)14 TransitionCrossfade (android.media.videoeditor.TransitionCrossfade)12 MediaProcessingProgressListener (android.media.videoeditor.VideoEditor.MediaProcessingProgressListener)12 MediaItem (android.media.videoeditor.MediaItem)11 VideoEditor (android.media.videoeditor.VideoEditor)11 Rect (android.graphics.Rect)10 AudioTrack (android.media.videoeditor.AudioTrack)10 EffectColor (android.media.videoeditor.EffectColor)10 OverlayFrame (android.media.videoeditor.OverlayFrame)9 TransitionAlpha (android.media.videoeditor.TransitionAlpha)9 EffectKenBurns (android.media.videoeditor.EffectKenBurns)8 ExportProgressListener (android.media.videoeditor.VideoEditor.ExportProgressListener)8 TransitionFadeBlack (android.media.videoeditor.TransitionFadeBlack)6 TransitionSliding (android.media.videoeditor.TransitionSliding)6 File (java.io.File)5 IOException (java.io.IOException)5 OverlayData (android.media.videoeditor.VideoEditor.OverlayData)3