Keeping MediaController on the screen in a VideoView

15,169

Solution 1

this worked for me. Just extend the media controller class. And override the hide method.

    MediaController mediaController = new MyMediaController(this, true);

public class MyMediaController extends MediaController {

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

    @Override
    public void hide() {
        mediaController.show(0);
    }

}

Solution 2

Ok I have found! After spending hours on it!! So there is to my knowledge no clue on how to do it on the different forums. Here is a trick I found:

FIRST CLASS (example...)

package com.sample.VideoViewExample;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;

public class VideoViewExample extends Activity implements SurfaceHolder.Callback{
   private VideoView mVideoView;


   @Override
   public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.main);

     mVideoView = (VideoView) findViewById(R.id.surface_view);     
     mVideoView.setKeepScreenOn(true);

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

     SurfaceHolder holder = mVideoView.getHolder();
     holder.addCallback(this);    
     MediaController_2 mMedia = new MediaController_2(this);

     mMedia.setMediaPlayer(mVideoView);
     mMedia.setAnchorView(mVideoView);     
     mVideoView.setMediaController(mMedia);
     mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.osa_patient));     
     mVideoView.requestFocus();
     mVideoView.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}
}

SECOND CLASS (WITH THE TRICK)

package com.sample.VideoViewExample;

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

public class MediaController_2 extends MediaController{


    public MediaController_2(Context context) {
        super(context);

        // TODO Auto-generated constructor stub
    }

    public void hide() {
    }

}

the trick is just to create a new MediaController that extends Mediacontroller and which function hide() does nothing!

If you want to have a look at the MediaController source code for a better understanding, you can look here: // source code for MediaController http://hi-android.info/src/android/widget/MediaController.java.html

Hope this will be of help to sb, JB

Share:
15,169
joachim
Author by

joachim

Updated on June 27, 2022

Comments

  • joachim
    joachim about 2 years

    I have trouble finding a way to keep the MediaController on the screen when using it with a VideoView. I want to play an Audio file which is located in res/raw in my app. The file is playing but I would like to keep the controller on the screen for the user to see the length of the file and how much time is left before the end etc. I found many time on forums that we should use the method .show(time) in order to do that, but I cannot figure out how to make it work.

    Here is the code I am using:

    package com.sample.VideoViewExample;
    import android.app.Activity;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.SurfaceHolder;
    import android.widget.MediaController;
    import android.widget.VideoView;
    
    public class VideoViewExample extends Activity implements SurfaceHolder.Callback{
       private VideoView mVideoView;
       private MediaController mMedia;
    
       @Override
       public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.main);
    
         mVideoView = (VideoView) findViewById(R.id.surface_view);
         //mVideoView.getHolder().addCallback(this);
         //mMedia.show(50000);
         //mVideoView.setMediaController(mMedia);
    
         MediaController mMedia = new MediaController(this);
         mMedia.setMediaPlayer(mVideoView);
         mMedia.setAnchorView(mVideoView);
         mVideoView.setMediaController(mMedia);
    
         mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.osa_patient)); 
    
         mVideoView.requestFocus();
         mVideoView.start();
    
       }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        mMedia.show(500000);
    
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    
    }
    }
    

    When I add the line mVideoView.getHolder().addCallback(this); the app bug, I am not sure why.

    Any help or alternative to what I would like to do is more than welcome :)

    Thanks, JB

  • SALMAN
    SALMAN over 12 years
    Extending Media Controller and overriding hide method Disable whole Screen and unable to detect any touch event of an Activity except media controller controls.
  • Vishwajit Palankar
    Vishwajit Palankar almost 9 years
    but it prevents from going back in the application.