gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:21 24 4
gpt4 key购买 nike

我正在开发一个使用 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 ComponentInfo{com.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)

最佳答案

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

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

关于java - 获取 mediaMetadataRetriever.setDataSource 抛出非法参数异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27565306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com