2014-12-19 76 views
1

我正在處理使用mediaMetadataRetriever.setDataSource抓取視頻幀的pp。但是,當我設置視頻路徑時,它給了我一個非法的參數異常並崩潰。我是java和Android新手。我在互聯網上搜索了很多,但目前還沒有運氣。這裏是我的全碼:獲取mediaMetadataRetriever.setDataSource拋出非法參數異常

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) 
+0

根據stacktrace你得到NullPointerException不IllegalArgument。只要檢查MainActivity的第47行是什麼導致它。 – birdy 2014-12-19 11:52:11

+0

@birdy:對不起,這是我更新了代碼和堆棧跟蹤的錯誤.. – 2014-12-19 12:41:16

回答

0

試試這個,我有這個問題,花了幾個小時把它與原始文件的工作。

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