Android - mediacontroller.show上的空指针异常()

时间:2015-07-05 17:53:12

标签: android android-mediaplayer android-videoview

我正在开发一个Android应用程序,我尝试从URL播放视频。

xml中的视频视图如下所述,

<VideoView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/VideoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

在ViewPager中的片段中,调用xml的VideoView引用并将其附加到MediaController

videoView = (VideoView) studyVideo.findViewById(R.id.VideoView);
        mediaController = new MediaController(getActivity());

        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(Uri.parse(studyVideoUrl));
        videoView.requestFocus();

        videoView.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                progressDialog.dismiss();
                isVideoBuffered=true;
                videoView.start();
            }
        });

我可以显示如下视频,

enter image description here

在此屏幕截图中,MediaController显示在Activity的底部。但是,我需要像在Youtube中一样显示在VideoView的底部。

请帮我找到解决方案。

更新

我在下面的xml中添加了MediaController,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <VideoView 
    android:id="@+id/VideoView"
    android:layout_above="@+id/mediaController"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

    <MediaController
        android:id="@+id/mediaController"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </MediaController>
</RelativeLayout>

在Java中,我在两个地方进行了更新,

  1. 替换

    mediaController = new MediaController(getActivity());

    (MediaController) studyVideo
                    .findViewById(R.id.mediaController); 
    
  2. mediaController.show();方法

  3. 中添加了onPrepared(MediaPlayer mp)

    但是,App崩溃时出现以下logcat错误,

    07-06 00:25:25.936: E/MediaPlayer(32614): setDataSource: IOException! uri=https://ia700401.us.archive.org/19/items/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4
    07-06 00:25:35.331: E/AndroidRuntime(32614): FATAL EXCEPTION: main
    07-06 00:25:35.331: E/AndroidRuntime(32614): java.lang.NullPointerException
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at android.widget.MediaController.show(MediaController.java:435)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at android.widget.MediaController.show(MediaController.java:394)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at com.prime.soft.fragments.StudyVideoFragment$1.onPrepared(StudyVideoFragment.java:70)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at android.widget.VideoView$2.onPrepared(VideoView.java:399)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2125)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at android.os.Handler.dispatchMessage(Handler.java:107)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at android.os.Looper.loop(Looper.java:194)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at android.app.ActivityThread.main(ActivityThread.java:5391)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at java.lang.reflect.Method.invoke(Method.java:525)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    07-06 00:25:35.331: E/AndroidRuntime(32614):    at dalvik.system.NativeStart.main(Native Method)
    

2 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <MediaController
        android:id="@+id/mediaController"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </MediaController>

    <VideoView 
    android:id="@+id/VideoView"
    android:layout_above="@+id/mediaController"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


</RelativeLayout>

应该是这个

mediaController = (MediaController) 
                .findViewById(R.id.mediaController); 

而不是这个,

(MediaController) studyVideo
                .findViewById(R.id.mediaController); 

或者您是否发生了更改 mediaPlayer 成员变量的名称?

答案 1 :(得分:0)

最后,我通过扩展MediaController

解决了问题

更新代码:

videoView = (VideoView) studyVideo.findViewById(R.id.VideoView);

            mediaController = new ConstantAnchorMediaController(getActivity(),
                    videoView);

            try {
                mediaController.setAnchorView(videoView);
                mediaController.setMediaPlayer(videoView);
                videoView.setMediaController(mediaController);
                videoView.setVideoURI(Uri.parse(studyVideoUrl));

            } catch (Exception e) {
                AlertMessage.showAlertMessage(getActivity(), e.toString());
            }
            videoView.setOnPreparedListener(new OnPreparedListener() {
                // Close the progress bar and play the video
                public void onPrepared(MediaPlayer mp) {
                    progressDialog.dismiss();
                    isVideoBuffered = true;
                    videoView.requestFocus();
                    videoView.start();
                }
            });

MediaController类

  public class ConstantAnchorMediaController extends MediaController {

        public ConstantAnchorMediaController(Context context, View anchor) {
            super(context);
            super.setAnchorView(anchor);
        }

        @Override
        public void setAnchorView(View view) {
            // Do nothing
        }
    }

<强> study_video.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent" >

    <VideoView
        android:id="@+id/VideoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="15dp" />


</RelativeLayout>

希望,对任何人都有帮助。