MediaController不起作用

时间:2014-03-27 09:53:54

标签: android mediacontroller

我的布局包含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>

以及java代码中的Medicontrolleri:

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();

但它不起作用,有截图: enter image description here

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

试试这个:

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) {

}
});
}

答案 1 :(得分:1)

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

<强>爪哇

// 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();
 }
});

答案 2 :(得分:0)

在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"
        />

并将其设置为您的媒体控制器

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