MediaController总是在Android上显示

时间:2010-12-16 07:54:25

标签: android mediacontroller

我在我的应用程序中使用mediacontroller,但它只显示3秒钟。我搜索了很多,但在每个文档中我只看到show函数,设置超时,但它没有效果。我怎么能一直展示mediacontroller?

我测试了show(0),但没有效果。

10 个答案:

答案 0 :(得分:25)

您可以扩展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);

答案 1 :(得分:12)

您可以创建内联匿名类并覆盖某些方法。您需要覆盖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;
        }
    };

答案 2 :(得分:11)

以这种方式尝试show方法:

new media controller().show(50000);

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

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

答案 3 :(得分:9)

你也可以创建一个内联的匿名类,并覆盖那里的hide方法,而不必为它创建一个全新的类:

mediaController = new MediaController(this) {
    @Override
    public void hide() {
    //Do not hide.
    }
};

答案 4 :(得分:6)

您需要做的是,覆盖自定义控制器中的hide方法,不执行任何操作。

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

    }
...
}

PS:我仍然需要点击视频来显示媒体控制器。

答案 5 :(得分: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();
        }
    });

答案 6 :(得分: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();
    }
};

答案 7 :(得分:1)

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

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 Class" show"中发表评论。方法

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

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

欢呼声。

答案 8 :(得分: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);

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

其他手机控制按钮继续正常工作。

答案 9 :(得分: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);
            }

        };