mediaMetadataRetriever.setDataSource(getBaseContext(),uri) 抛出非法参数异常
Posted
技术标签:
【中文标题】mediaMetadataRetriever.setDataSource(getBaseContext(),uri) 抛出非法参数异常【英文标题】:mediaMetadataRetriever.setDataSource(getBaseContext(),uri) throws illegal argument exception 【发布时间】:2014-12-19 07:57:27 【问题描述】:开发人员您好,我有一段可以抓取视频的帧...似乎它可以正常工作,除了其中的一部分我遇到非法参数异常...当我设置视频的路径时崩溃..这是我的代码,它在该行崩溃 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 ComponentInfocom.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)
任何想法都将不胜感激。
【问题讨论】:
请添加整个堆栈跟踪。 @jens:堆栈跟踪已更新 也许this 有帮助。 @jens: 非常感谢...但是这种方法中的标头是什么???我需要在那里放什么?? 标题?我在代码中看不到标题? 【参考方案1】:使用这个方法。
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) 抛出非法参数异常的主要内容,如果未能解决你的问题,请参考以下文章