MediaController doesn't work

爱⌒轻易说出口 提交于 2019-12-02 02:33:19

问题


My layout that contains videoView

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imgv_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:scaleType="centerCrop"
            android:visibility="invisible" />

        <VideoView
            android:id="@+id/videoview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" />
    </RelativeLayout>

And the Medicontrolleri in java code:

final MediaController mediaController = new MediaController(context);
            mediaController.setAnchorView(videoView);
            mediaController.setMediaPlayer(videoView);
            mediaController.show(0);

            videoView.setMediaController(mediaController);
            videoView.setVideoURI(mediaUri);
            videoView.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaController.show(3000);
                }
            });
            videoView.start();

But it doesn't work, there is screenshot:

Thank for your helping!


回答1:


Try this :

public static void playVideo(String urlPath) {

VideoView mVideoview; // Added this line
mVideoView =(VideoView) findViewByid(R.yourvideoviewid);

try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(urlPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);

} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();

}

mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();

}
});

mVideoview.setOnCompletionListener(new OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

}
});
}



回答2:


<VideoView
 android:id="@+id/videoView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />

Java

// Get VideoView in layout xml
VideoView mVideoView = (VideoView) findViewById(R.id.videoView);

// MediaController provide controller, contains the buttons
// like "Play/Pause", "Rewind", "Fast Forward" and a progress slide
MediaController mediaController = new MediaController(this);

mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);

mVideoView.setVideoPath("/path/to/your/videofile");
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
 public void onPrepared(MediaPlayer mp) {
  mVideoView.start();
 }
});



回答3:


Add frame layout for anchor view in your xml

<FrameLayout android:id="@+id/controllerAnchor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        />

and set this to your media controller

mediacontroller.setAnchorView(findViewById(R.id.controllerAnchor);



来源:https://stackoverflow.com/questions/22683900/mediacontroller-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!