通过视频视图在 android 中添加视频

Adding Video in android through Video View

我正在尝试在我的 Android 应用程序中添加视频。但它只是显示一个空白屏幕,然后应用程序崩溃。

这是代码 任何帮助都将被取消。

谢谢

@SuppressLint("SdCardPath")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);



    DisplayMetrics dm;
    VideoView video_player_view = (VideoView) findViewById(R.id.video_player_view);

    android.widget.MediaController media_Controller = new android.widget.MediaController(getApplicationContext(),null );
    video_player_view.setMediaController(media_Controller);
    dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int height = dm.heightPixels;
    int width = dm.widthPixels;
    video_player_view.setMinimumWidth(width);
    video_player_view.setMinimumHeight(height);
    video_player_view.setVideoPath("/sdcard/video");
    video_player_view.start(); 

这里是xml。

 <FrameLayout 
     android:id="@+id/video_frame" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 


     <VideoView android:id="@+id/video_player_view"
          android:layout_width="fill_parent"
           android:layout_height="fill_parent" />


     </FrameLayout> 

这是我的Logcat。也请告诉我在 setVideoPath 中添加什么作为路径。

01-07 13:48:58.304: E/Trace(25305): error opening trace file: No such file or directory (2)
01-07 13:49:39.474: E/MediaPlayer(25305): MediaPlayer: Uri ........ 
01-07 13:49:39.674: E/AndroidRuntime(25305): FATAL EXCEPTION: main
01-07 13:49:39.674: E/AndroidRuntime(25305): java.lang.NullPointerException
01-07 13:49:39.674: E/AndroidRuntime(25305):    at                android.widget.MediaController.updateFloatingWindowLayout(MediaController.java:171)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.widget.MediaController.access[=3=]0(MediaController.java:71)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at       android.widget.MediaController.onLayoutChange(MediaController.java:181)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.View.layout(View.java:13763)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.View.layout(View.java:13754)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.View.layout(View.java:13754)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.View.layout(View.java:13754)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.View.layout(View.java:13754)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewGroup.layout(ViewGroup.java:4364)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1922)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1033)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4295)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.Choreographer.doFrame(Choreographer.java:525)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.os.Handler.handleCallback(Handler.java:615)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at  android.os.Handler.dispatchMessage(Handler.java:92)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.os.Looper.loop(Looper.java:137)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at android.app.ActivityThread.main(ActivityThread.java:4827)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at java.lang.reflect.Method.invokeNative(Native Method) 
01-07 13:49:39.674: E/AndroidRuntime(25305):    at java.lang.reflect.Method.invoke(Method.java:511)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-07 13:49:39.674: E/AndroidRuntime(25305):    at dalvik.system.NativeStart.main(Native Method)
01-07 13:49:40.185: E/MediaPlayer(25305): error (100, 0)
01-07 13:49:40.185: E/MediaPlayer(25305): Mediaserver died in 4 state

ViewView 设置 MediaController 为:

MediaController media_Controller = new MediaController(CurrentActivity.this);
video_player_view.setMediaController(media_Controller);

您可以试试下面的代码:

MediaController media_Controller = new MediaController(getApplicationContext());
        video_player_view.setMediaController(media_Controller);
        dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;
        int width = dm.widthPixels;
        video_player_view.setMinimumWidth(width);
        video_player_view.setMinimumHeight(height);
        video_player_view.setVideoPath("/sdcard/video");
        video_player_view.start();