本文介绍了mediaMetadataRetriever.setDataSource(getBaseContext(),uri)引发非法参数异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个开发人员可以抓取视频的帧...似乎可以正常工作,但其中一部分会出现非法参数异常...当我设置视频的路径时崩溃..这是我的代码在行崩溃mediaMetadataRetriever.setDataSource(getBaseContext(),uri)这是完整的代码:

Hello developers I have a piece of that grabs the frames of a video...It seems it will work fine except a part of it where I am getting illegal argument exception...As I set the path of the video it crashes..Here is my code it crashes at the linemediaMetadataRetriever.setDataSource(getBaseContext(),uri)Here is the full code:

import java.io.IOException;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

    MediaMetadataRetriever mediaMetadataRetriever;
    MediaController myMediaController;
    VideoView myVideoView;
    String viewSource = "/storage/test.mp4";

    // String viewSource = "/storage/test.mp4";
    Uri uri = null;

    @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            AssetFileDescriptor afd = getAssets().openFd("test.mp4");
            Log.v("MA", "Before setdatasource");
            uri = Uri.parse("E:/test.mp4");
            mediaMetadataRetriever = new MediaMetadataRetriever();
            **mediaMetadataRetriever.setDataSource(getBaseContext(),uri);**

            // mediaMetadataRetriever.setDataSource(afd.getFileDescriptor(),
            // afd.getStartOffset(), afd.getLength());
            Log.v("MA", "After setdatasource" + afd.getStartOffset());
            myVideoView = (VideoView) findViewById(R.id.videoview);
            Log.v("MA", "VIdeoview found");
            myVideoView.setVideoURI(Uri.parse(viewSource));
            Log.v("MA", "After setdatasource");
            myMediaController = new MediaController(this);
            Log.v("MA", "After setdatasource");
            myVideoView.setMediaController(myMediaController);
            Log.v("MA", "myMediaController initialised");
            myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
            Log.v("MA", "setOnCompletionListener");
            myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
            Log.v("MA", "setOnPreparedListener");
            myVideoView.setOnErrorListener(myVideoViewErrorListener);
            Log.v("MA", "setOnErrorListener");
            myVideoView.requestFocus();
            Log.v("MA", "focus set");
            myVideoView.start();
            Log.v("MA", "video started");

            Button buttonCapture = (Button) findViewById(R.id.capture);
            buttonCapture.setOnClickListener(new OnClickListener() {

                @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
                @Override
                public void onClick(View arg0) {

                    int currentPosition = myVideoView.getCurrentPosition(); // in
                    // millisecond
                    Toast.makeText(MainActivity.this,
                            "Current Position: " + currentPosition + " (ms)",
                            Toast.LENGTH_LONG).show();

                    Bitmap bmFrame = mediaMetadataRetriever
                            .getFrameAtTime(currentPosition * 1000); // unit in
                    // microsecond

                    if (bmFrame == null) {
                        Toast.makeText(MainActivity.this, "bmFrame == null!",
                                Toast.LENGTH_LONG).show();
                    } else {
                        AlertDialog.Builder myCaptureDialog = new AlertDialog.Builder(
                                MainActivity.this);
                        ImageView capturedImageView = new ImageView(
                                MainActivity.this);
                        capturedImageView.setImageBitmap(bmFrame);
                        LayoutParams capturedImageViewLayoutParams = new LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT);
                        capturedImageView
                                .setLayoutParams(capturedImageViewLayoutParams);

                        myCaptureDialog.setView(capturedImageView);
                        myCaptureDialog.show();
                    }

                }
            });
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    MediaPlayer.OnCompletionListener myVideoViewCompletionListener = new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {
            Toast.makeText(MainActivity.this, "End of Video", Toast.LENGTH_LONG)
                    .show();
        }
    };

    MediaPlayer.OnPreparedListener MyVideoViewPreparedListener = new MediaPlayer.OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {

            long duration = myVideoView.getDuration(); // in millisecond
            Toast.makeText(MainActivity.this,
                    "Duration: " + duration + " (ms)", Toast.LENGTH_LONG)
                    .show();

        }
    };

    MediaPlayer.OnErrorListener myVideoViewErrorListener = new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {

            Toast.makeText(MainActivity.this, "Error!!!", Toast.LENGTH_LONG)
                    .show();
            return true;
        }
    };

}

踪迹:

E/AndroidRuntime( 4317): FATAL EXCEPTION: main

E/AndroidRuntime( 4317): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.captureframe/com.example.captureframe.MainActivity}: java.lang.IllegalArgumentException

E/AndroidRuntime( 4317):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

E/AndroidRuntime( 4317):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

E/AndroidRuntime( 4317):    at android.app.ActivityThread.access$600(ActivityThread.java:141)

E/AndroidRuntime( 4317):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)

E/AndroidRuntime( 4317):    at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime( 4317):    at android.os.Looper.loop(Looper.java:137)

E/AndroidRuntime( 4317):    at android.app.ActivityThread.main(ActivityThread.java:5103)

E/AndroidRuntime( 4317):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime( 4317):    at java.lang.reflect.Method.invoke(Method.java:525)

E/AndroidRuntime( 4317):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

E/AndroidRuntime( 4317):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

E/AndroidRuntime( 4317):    at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime( 4317): Caused by: java.lang.IllegalArgumentException

E/AndroidRuntime( 4317):    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:165)

E/AndroidRuntime( 4317):    at com.example.captureframe.MainActivity.onCreate(MainActivity.java:46)

E/AndroidRuntime( 4317):    at android.app.Activity.performCreate(Activity.java:5133)

E/AndroidRuntime( 4317):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

E/AndroidRuntime( 4317):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

任何想法都会受到赞赏.

Any ideas will be really appreciated.

推荐答案

使用此方法.

 public static Bitmap retriveVideoFrameFromVideo(String videoPath)
                throws Throwable
        {
            Bitmap bitmap = null;
            MediaMetadataRetriever mediaMetadataRetriever = null;
            try
            {
                mediaMetadataRetriever = new MediaMetadataRetriever();
                if (Build.VERSION.SDK_INT >= 14)
                    mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
                    else
                        mediaMetadataRetriever.setDataSource(videoPath);
             //   mediaMetadataRetriever.setDataSource(videoPath);
                bitmap = mediaMetadataRetriever.getFrameAtTime();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                throw new Throwable(
                        "Exception in retriveVideoFrameFromVideo(String videoPath)"
                                + e.getMessage());

            }
            finally
            {
                if (mediaMetadataRetriever != null)
                {
                    mediaMetadataRetriever.release();
                }
            }
            return bitmap;
        }

这篇关于mediaMetadataRetriever.setDataSource(getBaseContext(),uri)引发非法参数异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:23