mediaMetadataRetriever.setDataSource(getBaseContext(),uri)会引发非法参数异常。[英] mediaMetadataRetriever.setDataSource(getBaseContext(),uri) throws illegal argument exception

本文是小编为大家收集整理的关于mediaMetadataRetriever.setDataSource(getBaseContext(),uri)会引发非法参数异常。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

您好开发人员,我有一部分抓住视频的框架...似乎可以正常工作,除了一部分我会得到非法论证异常...当我设置视频的路径时崩溃..这里是我的代码,它在线崩溃 MediaMetAdatareTriever.setDataSource(getBaseContext(),URI) 这是完整的代码:

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)

任何想法都会非常感谢.

推荐答案

使用此方法.

 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;
        }

本文地址:https://www.itbaoku.cn/post/2091205.html

问题描述

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 line mediaMetadataRetriever.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;
        }
    };

}

Trace:

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.

推荐答案

Use this method.

 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;
        }