24

我在我的应用程序中使用媒体控制器,但它只显示 3 秒。我搜索了很多,但在每个文档中我只看到显示功能,设置超时,但它没有效果。我怎样才能始终显示媒体控制器?

我已经测试过show(0),但它没有效果。

4

10 回答 10

27

您可以扩展 MediaController 类并以编程方式将其实例设置为 VideoView 类:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.MediaController;

public class MyMediaController extends MediaController {
    public MyMediaController(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyMediaController(Context context, boolean useFastForward) {
        super(context, useFastForward);
    }

    public MyMediaController(Context context) {
        super(context);
    }

    @Override
    public void show(int timeout) {
        super.show(0);
    }

}

这是用法:

VideoView myVideoView = (VideoView) findViewById(R.id.my_video_view);
MediaController mc = new MyMediaController(myVideoView.getContext());
mc.setMediaPlayer(myVideoView);
myVideoView.setMediaController(mc);
于 2011-12-07T02:57:42.547 回答
13

您可以创建匿名类内联并覆盖某些方法。您需要覆盖 hide 方法并且在其中什么也不做。您还需要重写 dispatchKeyEvent 方法来检查后退按键并调用 super.hide()。否则在后按时控制器不会隐藏并且活动无法关闭。

 mediaController = new MediaController(this){
        @Override
        public void hide() {
            // TODO Auto-generated method stub
            //do nothing
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {

            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

                if (mediaPlayer != null) {
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
                super.hide();
                Activity a = (Activity)getContext();
                a.finish();

            }
        return true;
        }
    };
于 2014-01-19T11:21:08.120 回答
11

您还可以内联创建一个匿名类并在那里重写 hide 方法,而不必为它创建一个全新的类:

mediaController = new MediaController(this) {
    @Override
    public void hide() {
    //Do not hide.
    }
};
于 2013-10-24T19:37:25.140 回答
11

用这种方式试试 show 方法:

new media controller().show(50000);

并检查http://developer.android.com/reference/android/widget/MediaController.html#show()

SudeepSR:请注意,如果您调用了 show(0),它将显示媒体控制器,直到调用 hide()。

于 2010-12-16T08:20:00.637 回答
6

您需要做的是,覆盖自定义控制器中的 hide 方法并且什么都不做。

public class MyMediaController extends MediaController {
..
@Override
    public void hide() {
        // Do nothing here in order to always show

    }
...
}

PS:您仍然需要点击视频才能显示媒体控制器。

于 2012-07-30T05:08:12.903 回答
5

在尽我所能之后,以下代码对我有用!

    mVideoView = (VideoView) findViewById(R.id.video);

    mMediaController = new MediaController(this) {
        //for not hiding
        @Override
        public void hide() {}

        //for 'back' key action
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                Activity a = (Activity)getContext();
                a.finish();
            }
            return true;
        }
    };

    mMediaController.setAnchorView(mVideoView);
    mMediaController.setMediaPlayer(mVideoView);
    mVideoView.setMediaController(mMediaController);
    mMediaController.requestFocus();

    //only this showed the controller for me!!
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mVideoView.start();
            mMediaController.show(900000000);
        }
    });

    //finish after playing
    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            finish();
        }
    });
于 2014-08-21T19:28:04.870 回答
2

这可能是一个旧线程,但仍然没有答案,试试这个:

final MediaController mediaController = new MediaController(this);
mediaController.setAlwaysDrawnWithCacheEnabled(true);
mediaController.setAnchorView(vView);
mediaController.requestFocus(); 
    vView.setOnPreparedListener( new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaController.show( 0 );
        }
    });

vView.setVideoPath(Preview_Path);
vView.setMediaController(mediaController);
vView.start(); 

MediaController 类“show”方法中有一条注释

**Use 0 to show
* the controller until hide() is called**

所以使用 900000 或更大的值不会有帮助。希望它可以帮助你。

干杯。

于 2015-02-24T01:41:46.853 回答
2

尝试这个:

videoView.setOnCompletionListener(onVideoCompleted);
videoView.setOnPreparedListener(onVideoPrepared);

mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);

MediaController mc = new MediaController(this);
videoView.setMediaController(mc);

MediaPlayer.OnPreparedListener onVideoPrepared = new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mc.show(0);
    }
};

MediaPlayer.OnCompletionListener onVideoCompleted = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        mc.hide();
    }
};
于 2015-08-20T02:27:16.043 回答
0

我想淡化视频控制器并始终将其显示为音频。这有效

    mController = new MediaController(this) {
        @Override
        public void hide() {

            if (mType != TYPE_AUDIO) super.hide();
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                mController.hide();
                Activity a = (Activity)getContext();
                a.finish();
                return true;
            }
            return false;
        }
    };

在 MediaPlayer.onPrepared 我添加了:

if (mType == TYPE_AUDIO) mController.show(0);

这会导致控制器在音频播放开始时显示,而不是视频播放。

其他电话控制按钮继续正常工作。

于 2017-03-07T14:31:34.483 回答
0

简单的!在事件隐藏中设置可见性“GONE”并在显示中设置可见性“VISIBLE”!

        MediaController mc= new MediaController(zoom.this){

            @Override
            public void setMediaPlayer(MediaPlayerControl player) {
                super.setMediaPlayer(player);
                this.show(4000);
            }
            @Override
            public void show(int timeout) {
                super.show(timeout);
                this.setVisibility(View.VISIBLE);
            }
            //instead of press twice with press once "back" button to back
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                    Activity a = (Activity)getContext();
                    a.finish();
                }
                return true;
            }
            @Override
            public void hide() {
                // TODO Auto-generated method stub
                super.hide();
                this.setVisibility(View.GONE);
                //super.show(3000);
            }

        };
于 2017-07-14T23:50:16.290 回答