I have a VideoView which plays a video and I call
videoPlayer.setMediaController(new MediaController(this));
to show the media controller buttons (play, pause, forward, backward).
The question is how am I able to add additional methods when I click the play or pause button of the MediaController?
Here's my class:
public class VideoPlayer extends Activity implements OnCompletionListener {
private String filename;
private VideoView videoPlayer;
private MediaPlayer mediaplayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.video_player);
System.gc();
Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("video_url");
System.out.println(filename);
videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
videoPlayer.setOnCompletionListener(this);
videoPlayer.setKeepScreenOn(true);
videoPlayer.setVideoPath(filename);
videoPlayer.setMediaController(new MediaController(this));
videoPlayer.requestFocus();
videoPlayer.start();
mediaplayer = new MediaPlayer();
mediaplayer = MediaPlayer.create(
this,
Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.alo));
mediaplayer.start();
}
/** This callback will be invoked when the file is finished playing */
#Override
public void onCompletion(MediaPlayer mp) {
// Statements to be executed when the video finishes.
this.finish();
}
/** Use screen touches to toggle the video between playing and paused. */
#Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (videoPlayer.isPlaying()) {
videoPlayer.pause();
mediaplayer.pause();
} else {
videoPlayer.start();
mediaplayer.start();
}
return true;
} else {
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I'm not sure what you are trying to achieve.
You could extend a class with MediaController and implement some methods of your own. Basic OO principles.
private class MyMediaController extends MediaController {
#Override
public boolean onTouchEvent(MotionEvent event) {
// Your logic here
return super.onTouchEvent(event);
}
}
Related
I'm using a MediaController to control video play back for my VideoView. I've overriden VideoView.setOnPreparedListener so that the ActionBar/Toolbar is hidden (hide()) after the video first finishes buffering. And I'd like the toolbar to comeback when the MediaController does. I've tried overriding the MediaController show() and hide() methods, like so:
mVideoView.start();
// Media Controller
mMediaController = new MediaController(this){
#Override
public void show() {
getSupportActionBar().show();
}
#Override
public void hide() {
getSupportActionBar().hide();
}
};
mMediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mMediaController);
// Hide toolbar once video starts
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// Hide support bar
getSupportActionBar().hide();
}
});
This works, except the playback controls stop showing up! And of course, calling a recursive mMediaController.show() within the overridden method doesn't work... Can I have my cake an eat it too?
Edit
So I've also, unsucessfully tried taking advantage of the VideoView.setOnTouchListener and VideoView.setOnCompletetionListener:
mVideoView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
getSupportActionBar().show();
return false;
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
getSupportActionBar().hide();
}
});
It does show, but it won't hide again, perhaps I misinterpret OnCompletetionListener?
Edit 2
From Amir's suggestion, I override the onTouch for VideoView, not perfect, but it's on like the right track:
mMediaController = new MediaController(this);
mVideoView = (VideoView) findViewById(R.id.media_player);
mVideoView.setOnTouchListener(new View.OnTouchListener() {
boolean flag;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(flag) {
mMediaController.hide();
getSupportActionBar().hide();
} else {
mMediaController.show();
getSupportActionBar().show();
}
flag = !flag;
return true;
}
return false;
}
});
This mostly works, it toggles the Toolbar and so sometimes the Toolbar will appear without the MediaController, and each time I toggle it, the MediaController does it's regular 'appear for a few seconds and then disappear.' In any case, it is a working solution.
Your original code seems to work if you add calls to the corresponding super methods.
mediaController = new MediaController(this){
#Override
public void show() {
super.show();
getSupportActionBar().show();
Toast.makeText(activity_media_player.this, "SHOW TOOLBAR", Toast.LENGTH_SHORT).show();
}
#Override
public void hide() {
super.hide();
getSupportActionBar().hide();
Toast.makeText(activity_media_player.this, "HIDE TOOLBAR", Toast.LENGTH_SHORT).show();
}
};
You should change your OnTouchListener a bit. I do following in my project and works fine:
final MediaController mediaController = new MediaController(this);
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnTouchListener(new View.OnTouchListener() {
boolean flag = true;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (flag) {
mediaController.hide();
hideToolbar();
}
else {
mediaController.show(0);
showToolbar();
}
flag = !flag;
return true;
}
return false;
}
});
videoView.setMediaController(mediaController);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
}
});
videoView.setVideoURI(Uri.parse("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"));
And hideToolbar() with some traslate animation:
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
showToolbar():
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();
Also if you need to HideStatusBar call setUiFlag() with true:
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setUiFlags(boolean fullscreen) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
View decorView = getWindow().getDecorView();
if (decorView != null) {
decorView.setSystemUiVisibility(fullscreen ? getFullscreenUiFlags() : View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private int getFullscreenUiFlags() {
int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
}
return flags;
}
I am interested in being able to "prebuffer" VideoView A while VideoView B is playing. Is this possible?
I have two instances of a VideoView.
Only one VideoView will be displayed at a given time, taking up the entire screen of the phone.
The purpose is to reduce the "black screen" when switching from VideoView A and VideoView B.
The app plays a list of videos one after another.
Added Below Code on Jan 19, 2015
The code below grabs a reference to each Player's MediaPlayer. When MediaPlayer A begins rendering, MediaPlayer B will call stop(), release(), setDataSource(url), prepareAsync(). When MediaPlayer A completes, Media Player B will call start().
public class PrebufferingActivity extends Activity {
private VideoView player1;
private VideoView player2;
private MediaPlayer mediaPlayer1;
private MediaPlayer mediaPlayer2;
public static final String URL_1 = "sample1.mp4";
public static final String URL_2 = "sample2.mp4";
public boolean FIRST_TIME = true;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prebuffering);
player1 = (VideoView) findViewById(R.id.videoPlayer1);
player2 = (VideoView) findViewById(R.id.videoPlayer2);
player1.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
player2.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
player1.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer2.start();
// Toggle visibility. Player1 is completed. Hide Player1. Show Player2.
player2.setVisibility(View.VISIBLE);
player1.setVisibility(View.INVISIBLE);
}
});
player1.setOnInfoListener(new OnInfoListener(){
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra)
{
if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
{
mediaPlayer2.stop();
mediaPlayer2.release();
try {
mediaPlayer2.setDataSource(URL_2);
mediaPlayer2.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
});
player1.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer1 = mp;
if(FIRST_TIME == true) {
mediaPlayer1.start();
player1.requestFocus();
FIRST_TIME = false;
}
}
});
player2.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer1.start();
player1.requestFocus();
// Toggle visibility. Player2 is completed. Hide Player2. Show Player1.
player1.setVisibility(View.VISIBLE);
player2.setVisibility(View.INVISIBLE);
}
});
player2.setOnInfoListener(new OnInfoListener(){
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra)
{
if(what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
{
// Prepare Player 2
mediaPlayer1.stop();
mediaPlayer1.release();
try {
mediaPlayer1.setDataSource(PrebufferingActivity.URL_1);
mediaPlayer1.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
});
player2.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer2 = mp;
}
});
// Player 1
player1.setMediaController(new MediaController(this));
player1.setVideoURI(Uri.parse(URL_1));
// Player 2
player2.setMediaController(new MediaController(this));
player2.setVideoURI(Uri.parse(URL_2));
}
Assuming you are using MediaPlayer to handle the playback of your videos within your VideoView, you can use the prepareAsync() method to achive this. The prepareAsync() method essentially performs the same function as prepare(), which is used to buffer and prepare a video, except it can be done asynchronously in the background.
EDIT
I did something similiar but I actually used SurfaceView. To handle another stream, simply create another instance of MediaPlayer. The following example will create two instances of MediaPlayer and buffer the first video. After the first video is prepared, the second video will be asynchronously buffered. The first MediaPlayer is then bound to the SurfaceView and the first video is started. When the first video is finished playing, the corresponding instance of MediaPlayer is released, the secondary instance is attached, and the second video is automatically started. The following example code has been tested and works fine:
public class StreamingActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener
{
MediaPlayer mediaPlayerPrimary;
MediaPlayer mediaPlayerSecondary;
SurfaceHolder surfaceHolder;
SurfaceView surfaceView;
String videoSourcePrimary = "videoSourcePrimaryURL";
String videoSourceSecondary = "videoSourceSecondaryURL";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.streaming_activity_layout);
/** Create SurfaceView */
surfaceView = (SurfaceView) findViewById(R.id.surface_view);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
#Override
public void surfaceCreated(SurfaceHolder arg0)
{
try
{
/** Create MediaPlayer(s) */
mediaPlayerPrimary = new MediaPlayer();
mediaPlayerSecondary = new MediaPlayer();
/** Attach primary MediaPlayer to SurfaceView */
mediaPlayerPrimary.setDisplay(surfaceHolder);
/** Set sources */
mediaPlayerPrimary.setDataSource(videoSourcePrimary);
mediaPlayerSecondary.setDataSource(videoSourceSecondary);
/** Prepare MediaPlayer(s) */
mediaPlayerPrimary.prepare();
mediaPlayerSecondary.prepareAsync();
/** Set listeners */
mediaPlayerPrimary.setOnPreparedListener(this);
mediaPlayerPrimary.setOnCompletionListener(this);
mediaPlayerSecondary.setOnPreparedListener(this);
mediaPlayerSecondary.setOnCompletionListener(this);
/** Set audio stream type */
mediaPlayerPrimary.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayerSecondary.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
catch (Exception e)
{
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0)
{
}
#Override
public void onPrepared(MediaPlayer mp)
{
if (mp == mediaPlayerPrimary)
{
/** First video ready, start playback */
mediaPlayerPrimary.start();
}
else if (mp == mediaPlayerSecondary)
{
/** Second video is ready */
}
}
#Override
public void onCompletion(MediaPlayer mp)
{
/** First video is completed, start second video */
mediaPlayerPrimary.release();
mediaPlayerSecondary.setDisplay(surfaceHolder);
mediaPlayerSecondary.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.streaming_activity_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have a VideoView in my Activity with some TextView's and EditText below it. The Video in VideoView is loaded from server. I have to always show the Media Controls of VideoView. I have overriden the hide() in MediaController. In OnPrepared(), after start() I am setting the VideoView to show(). It is showing the controls which is what I wanted. I have written code to finish my activity when device back button is pressed. Now my issue is when I press phone's back button the activity doesn't finish. When I move to some other activity, it shows PhoneWindowLeaked exception in console. I have no clue about this. I could not find anything in google or even in this site. Please help me to solve this.
Sample code snippet is below :
VideoView mVideoView=(VideoView)findViewById(R.id.videoView);
mVideoView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mVideoView.setVisibility(View.INVISIBLE);
}
});
mVideoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK||keyCode == KeyEvent.KEYCODE_HOME) {
if(mVideoView.isPlaying()){
mVideoView.stopPlayback();
mVideoView.suspend();
}
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
mediaController = new MediaController(SampleActivity.this, true){
public void hide() {};
};
mediaController.setMediaPlayer(mVideoView);
mediaController.setAnchorView(mVideoView);
mediaController.requestFocus();
mVideoView.setMediaController(mediaController);
Uri video = Uri.parse(getResources().getString(R.string.img_path) + csPath);
mVideoView.setVideoURI(video);
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mediaController.show(900000000);
}
});
#Override
protected void onPause() {
mediaController.hide();
mVideoView.stopPlayback();
mVideoView.suspend();
super.onPause();
}
#Override
protected void onStop() {
super.onStop();
mediaController.hide();
}
Thanks
I have an activity with different video files displayed. When I click a video file, I'm taken to another activity, where a VideoView plays the video.
My issue is that when I want to exit this activity and return back to previous, I should click twice the back button, in order to return back. If I click only once, the video starts playing once again, and only at the second attempt I'm allowed to exit the screen.
Then I tried this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d(Constants.LOG_TAG, "back pressed in videoplayer");
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
And, although I see in the logcat "back pressed in video player", the activity does not exit. I still should press twice the back button.
EDIT: This is the most relevant (I believe) source code. Note however, that the video is played from the internet, and I'm not using the Mediacontroller, instead I'm defining my own layout and link to videoview conrols.
public class VideoPlayer extends Activity implements OnClickListener, OnCompletionListener,
OnSeekBarChangeListener, OnPreparedListener, OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_view);
// Gets the position of clicked video, from an ArrayList of video urls.
selectedVideo = getPosition();
// the play button
play = (ImageButton) findViewById(R.id.play);
play.setOnClickListener(this);
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnCompletionListener(this);
videoView.setOnPreparedListener(this);
videoView.setOnTouchListener(this);
// the url to play
String path = videoUris.get(selectedVideo);
videoView.setVideoPath(getPath(path));
}
/**
* Play or Pause the current video file.
*
* If the video is paused, then invoking this method should start it. If the video is already playing, then the
* video should pause.
*/
private void play() {
if (!isVideoStarted) {
isVideoStarted = true;
videoView.start();
play.setImageResource(R.drawable.video_pause);
videoSeekBar.post(updateSeekBarRunnable);
} else if (isVideoStarted) {
isVideoStarted = false;
pause();
}
}
/**
* Start playing back a video file with the specified Uri.
*/
private void startPlayback() {
String path = videoUris.get(selectedVideo);
videoView.setVideoPath(getPath(path));
videoView.start();
}
/**
* Stops the currently playing video. (The SeekBar position is reset to beginning, 0.)
*/
private void stopPlayback() {
videoView.stopPlayback();
}
/**
* Pause the currently playing video. (The SeekBar remains in its position.)
*/
private void pause() {
videoView.pause();
#Override
public void onPrepared(MediaPlayer mp) {
play();
}
}
Code below work fine for me:
try
{
MediaController mc = new MediaController(YourActivity.this);
mc.setAnchorView(vd);
Uri uri = Uri.parse(YourURL);
vd.setMediaController(mc);
vd.setVideoURI(uri);
//vd.start();
}catch(Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
vd.requestFocus();
vd.start();
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Thank you for trying to help me.
It turned out that I was calling startActivity(intent); twice, and that is why I should press the back button twice.
Can you try overriding the onBackPressed instead of KeyEvent. Because your code seems to look correct.
Remove this,
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d(Constants.LOG_TAG, "back pressed in videoplayer");
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
And add,
#Override
public void onBackPressed() {
finish();
}
I am using mediacontroller in my app, but it shows only for 3 seconds. I have searched a lot, but in every document I see only the show function, set time out, but it has no effect. How can I always show mediacontroller?
I have tested show(0), but it had no effect.
You can extend the MediaController class and programmatically set an instance of it to a VideoView class:
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);
}
}
Here's the usage:
VideoView myVideoView = (VideoView) findViewById(R.id.my_video_view);
MediaController mc = new MyMediaController(myVideoView.getContext());
mc.setMediaPlayer(myVideoView);
myVideoView.setMediaController(mc);
You can create anonymous class inline and override certain methods. You need to override the hide method and do nothing in there. You also need to override the dispatchKeyEvent method to check for back key press and call the super.hide(). Otherwise on back press the controller wont hide and the activity cannot be closed.
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;
}
};
You can also create an anonymous class inline and override the hide method there instead of having to create a whole new class for it:
mediaController = new MediaController(this) {
#Override
public void hide() {
//Do not hide.
}
};
Try the show method in this way:
new media controller().show(50000);
And also check http://developer.android.com/reference/android/widget/MediaController.html#show().
SudeepSR: Please make a note of that, if you called show(0), it will show the Media Controller until hide() is called.
After trying all that I could, the following code worked for me!
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();
}
});
What you need to do is, overrride the hide method in the custom controller and do nothing.
public class MyMediaController extends MediaController {
..
#Override
public void hide() {
// Do nothing here in order to always show
}
...
}
PS: You still need to click on the video to show the media controller.
This may be an old thread, but still unanswered, try this :
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();
theres a comment inside the MediaController Class "show" method
**Use 0 to show
* the controller until hide() is called**
so using 900000 or larger value wont help.
hope it helps you.
cheers.
Try this:
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();
}
};
I wanted to fade the controller for videos and always show it for audio. This worked
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;
}
};
In MediaPlayer.onPrepared I added:
if (mType == TYPE_AUDIO) mController.show(0);
This causes the controller to show at the start of audio playback, but not video playback.
The other phone control buttons continue to work as normal.
Easy! Set visibility "GONE" in event hide and set visibility "VISIBLE" in show!
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);
}
};