获取 mediaMetadataRetriever.setDataSource 抛出非法参数异常

Posted

技术标签:

【中文标题】获取 mediaMetadataRetriever.setDataSource 抛出非法参数异常【英文标题】:Getting mediaMetadataRetriever.setDataSource throws illegal argument exception 【发布时间】:2014-12-19 11:37:44 【问题描述】:

我正在开发一个使用 mediaMetadataRetriever.setDataSource 抓取视频帧的应用程序。但是,当我设置视频路径时,它会给我一个非法参数异常并崩溃。我是java和android的新手。我在互联网上搜索了很多,但到目前为止没有运气。这是我的完整代码:

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
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/sdcard0/DCIM/100MEDIA/VIDEO0009.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(viewSource);
        Log.v("MA", " set datasource");
        myVideoView = (VideoView) findViewById(R.id.videoview);
        myVideoView.setVideoURI(Uri.parse(viewSource));
        myMediaController = new MediaController(this);
        myVideoView.setMediaController(myMediaController);

        myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
        myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
        myVideoView.setOnErrorListener(myVideoViewErrorListener);

        myVideoView.requestFocus();
        myVideoView.start();

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

            @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();
                

            
        );
    

    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( 3411): FATAL EXCEPTION: main
E/AndroidRuntime( 3411): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.captureframe/com.example.captureframe.MainActivity: java.lang.IllegalArgumentException
E/AndroidRuntime( 3411):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime( 3411):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime( 3411):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 3411):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime( 3411):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3411):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3411):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime( 3411):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3411):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime( 3411):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime( 3411):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 3411):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3411): Caused by: java.lang.IllegalArgumentException
E/AndroidRuntime( 3411):    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:68)
E/AndroidRuntime( 3411):    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:155)
E/AndroidRuntime( 3411):    at com.example.captureframe.MainActivity.onCreate(MainActivity.java:46)
E/AndroidRuntime( 3411):    at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime( 3411):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime( 3411):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

【问题讨论】:

根据堆栈跟踪,您得到的是 NullPointerException 而不是 IllegalArgument。只需检查 MainActivity 第 47 行的原因。 @birdy:对不起,这是一个错误,我已经更新了代码和堆栈跟踪.. 【参考方案1】:

试试这个,我遇到了这个问题,花了几个小时才让它处理原始文件。

Resources res = getResources();
AssetFileDescriptor afd = res.openRawResourceFd(R.raw.rawResourceId);
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

【讨论】:

以上是关于获取 mediaMetadataRetriever.setDataSource 抛出非法参数异常的主要内容,如果未能解决你的问题,请参考以下文章

获取 mediaMetadataRetriever.setDataSource 抛出非法参数异常

【Android音视频】MediaMetadataRetriever使用

音视频开发系列——全面了解Android MediaMetadataRetriever

MediaMetaDataRetriever.setDataSource 返回 null

Android MediaMetadataRetriever.getFrameAtTime 跳过帧

Android多媒体信息获取