首页 > 代码库 > 解决:信息中插入avi格式的视频时,提示“unsupported video format”

解决:信息中插入avi格式的视频时,提示“unsupported video format”

【测试步骤】:新建信息,添加AVI格式的视频

【测试结果】:添加时弹出提示“unsupported video format”

该问题主要提现在手机彩信视频附件不支持该AVI格式的视频,因此我们通过操作流程对代码进行追踪,查找“unsupported video format”产生的位置。

我们从添加附件界面ComposeMessageActivity类的onActivityResult()方法开始。

--》onActivityResult()调用代码如下:

            case REQUEST_CODE_ATTACH_VIDEO:
                if (data != null) {
                    mAttachFileUri = data.getData();
                    addVideoAsync(mAttachFileUri, false);
                }
                break;

--》addVideo()--》setAttachment()<WorkingMessage.java> 调用代码如下:

        result = append ? appendMedia(type, dataUri, slideShowEditor)
                : changeMedia(type, dataUri, slideShowEditor);

--》changeMedia()--》internalChangeMedia()--》changeVideo()<SlideshowEditor.java>

--》new VideoModel()<TAG 1-1>

    public VideoModel(Context context, Uri uri, RegionModel region)
            throws MmsException {
        this(context, null, null, uri, region);
        initModelFromUri(uri);
        checkContentRestriction();
    }

--》initModelFromUri()<VideoModel.java> 调用代码如下:

    private void initModelFromUri(Uri uri) throws MmsException {
        String scheme = uri.getScheme();
        if (scheme.equals("content")) {
            initFromContentUri(uri);
        } else if (uri.getScheme().equals("file")) {
            initFromFile(uri);
        }
        initMediaDuration();
    }

上述代码通过添加Log进行输出,这里的scheme的值为“file”;

--》initFromFile()

    private void initFromFile(Uri uri) throws MmsException {
        String path = uri.getPath();
        mSrc = http://www.mamicode.com/path.substring(path.lastIndexOf(‘/‘) + 1);
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        String extension = MimeTypeMap.getFileExtensionFromUrl(mSrc);
        if (TextUtils.isEmpty(extension)) {
            // getMimeTypeFromExtension() doesn‘t handle spaces in filenames nor can it handle
            // urlEncoded strings. Let‘s try one last time at finding the extension.
            int dotPos = mSrc.lastIndexOf(‘.‘);
            if (0 <= dotPos) {
                extension = mSrc.substring(dotPos + 1);
            }
        }
        mContentType = mimeTypeMap.getMimeTypeFromExtension(extension.toLowerCase());
        // It‘s ok if mContentType is null. Eventually we‘ll show a toast telling the
        // user the video couldn‘t be attached.
        if (TextUtils.isEmpty(mContentType)) {
            throw new MmsException("Type of media is unknown.");
        }

        if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
            Log.v(TAG, "New VideoModel initFromFile created:"
                    + " mSrc=http://www.mamicode.com/" + mSrc
                    + " mContentType=" + mContentType
                    + " mUri=" + uri);
        }
    }

上述代码中对mContentType进行赋值,这里我们通过Log输出,mContentType的值为“video/x-msvideo”;

接下来接着进行构造函数<TAG1-1>中的方法checkContentRestriction();

    protected void checkContentRestriction() throws ContentRestrictionException {
        ContentRestriction cr = ContentRestrictionFactory.getContentRestriction();
        cr.checkVideoContentType(mContentType);
    }

--》checkVideoContentType()<CarrierContentRestriction.java>

    public void checkAudioContentType(String contentType)
            throws ContentRestrictionException {
        if (null == contentType) {
            throw new ContentRestrictionException("Null content type to be check");
        }

        if (!sSupportedAudioTypes.contains(contentType)) {
            throw new UnsupportContentTypeException("Unsupported audio content type : "
                    + contentType);
        }
    }

上述代码中加粗标识的代码就是测试描述的问题,我们接着分析本类中静态语句快中的代码;

sSupportedVideoTypes = ContentType.getVideoTypes();

最终我们最终到ContentType.java类,并且发现该类中没有添加对该视频格式的支持。因此添加以下代码进行支持。

        ......

    public static final String VIDEO_UNSPECIFIED = "video/*";
    public static final String VIDEO_3GPP        = "video/3gpp";
    public static final String VIDEO_3G2         = "video/3gpp2";
    public static final String VIDEO_H263        = "video/h263";
    public static final String VIDEO_MP4         = "video/mp4";
    public static final String VIDEO_X_MSVIDEO         = "video/x-msvideo";

   ......

   // add supported video types
   sSupportedVideoTypes.add(VIDEO_X_MSVIDEO);
   sSupportedVideoTypes.add(VIDEO_3GPP);
   sSupportedVideoTypes.add(VIDEO_3G2);
   sSupportedVideoTypes.add(VIDEO_H263);
   sSupportedVideoTypes.add(VIDEO_MP4);

   ......

OK!