Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an Android MediaController to appear from layout xml?

Tags:

android

I've created a layout.xml file with the following XML, but I can't get the MediaController to appear at all.

<MediaController android:id="@+id/media_controller"
    android:layout_width="fill_parent"
    android:layout_height="200px"
    android:visibility="visible"/>

In code, I can obtain a reference to it from code, call show(), etc, but it never appears.

I have noticed in the android dev documentation (MediaController.html) that it states, "The way to use this class is to instantiate it programatically.", so maybe I'm a dufus and just need to do something differently such as not do what I'm doing. :)

I CAN get it to appear if I do it programmatically, but I need it to always appear on the screen. Am I just being stupid, or is it just not meant to be inflated via XML?

like image 655
DustinB Avatar asked Nov 18 '09 23:11

DustinB


People also ask

Why my XML is not opening in Android Studio?

Go to plugins -> Android support and disable this. Restart Android studio and enable the plugin again. Worked for me.

What is MediaController android?

android.widget.MediaController. A view containing controls for a MediaPlayer. Typically contains the buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress slider. It takes care of synchronizing the controls with the state of the MediaPlayer. The way to use this class is to instantiate it programmatically.


2 Answers

From this sample project:

ctlr=new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);

Then, it will appear when you tap the screen towards the bottom edge of your VideoView.

In terms of keeping it on screen all the time...I don't know if that's possible. You can always create your own controller -- here's a sample project that creates its own pop-up translucent controller. It's really not all that hard, and you get full control over everything, including arranging for it to be on-screen all the time.

like image 199
CommonsWare Avatar answered Sep 19 '22 01:09

CommonsWare


You can prevent the MediaController from hiding extending MediaController and override hide() to do nothing. eg:

class UnhideableMediaController extends MediaController
{
    // override whichever contstructors you need to. 
    public UnhideableMediaController(Context context)
    {
      super(context);
    }

    // override hide to do nothing
    public void hide()
    {
      // don't hide
    }
}
like image 44
Rob Gorman Avatar answered Sep 21 '22 01:09

Rob Gorman