开发者

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

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?


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.


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
    }
}


By using show(0) media controller will be shown until hide() is called by an interaction with the player. I found unfortunately no way to prevent hide() yet;-(

MediaController mc = new MediaController(MPlayer.this);  
 mc.setMediaPlayer(MPlayer.this);  
 mc.setEnabled(true);

 View mMediaControllerView = (View)findViewById(R.id.media_controller); //get it from your layout

 mc.setAnchorView(mMediaControllerView);
 mMediaControllerView.setOnTouchListener(mPlayerTouchListener);//also use show() in onTouchListener

 new Handler().postDelayed(new Runnable() {

     public void run() {
        mc.show(0);
     } }, 1500);

Wait for screen to buid in order to avoid a bug in android (1,5 seconds here)


You can inflate a mediacontroller from the layout xml. Also after obtaining a reference of it can set a videoview as an anchor.

  • But this will cause a crash as soon as you try to touch mediacontroller (I am trying to figure out why its happening).

Also In this mediaController will appear always.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/mVideoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <MediaController
        android:id="@+id/mediaController1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</RelativeLayout>

In activity,

public class AnimationActivity extends Activity implements OnClickListener {

    private MediaController mController = null;
    private VideoView mVideoView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        mController = (MediaController) findViewById(R.id.mediaController1);
        mController.post(new Runnable() {
            @Override
            public void run() {
                mVideoView = (VideoView) findViewById(R.id.mVideoView);
                mVideoView.setVideoURI(Uri.parse("android.resource://"
                        + getPackageName() + "/" + R.raw.video0));
                mController.setAnchorView(mVideoView);
                mVideoView.setSoundEffectsEnabled(false);
                mVideoView.start();
            }
        });
    }
}


http://www.docstoc.com/docs/9811647/Media-on-the-Android-Platform

This doc suggests you may need to extend the Controller to get the behaviour that you need.

You could also show it for a very long time i suppose ;)

I'm presently struggling to even get the controller to show then I press a button :\

(also interesting how quickly google added this question to search results)


When declaring my MediaController in a layout file, getting a handle on it in code (using the Activity.findViewById(int id) method) and attaching it to my VideoView (using the VideoView.setMediaController(MediaController controller) method), I find it behaves undesirably (force closes the app) at times. Instead, I create it in code, attach it to the VideoView and then specify where it should display in the Activity using the VideoView.setAnchorView(View view) method as follows:

myVideoView.setVideoURI(Uri.parse(myVideoUrl));
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
  public void onPrepared(MediaPlayer mp)
  {
    MediaController mediaController = new MediaController(MyActivity.this);
    videoView.setMediaController(mediaController);

    // put the MediaController at the bottom of the Activity rather than directly beneath the VideoView
    // (note that my Activity's layout file has root node with id 'myactivity_layoutroot')
    if (findViewById(R.id.myactivity_layoutroot) != null)
      mediaController.setAnchorView(findViewById(R.id.myactivity_layoutroot));

    videoView.requestFocus();
  }
});
myVideoView.start();


I think it is your "mVideoView.setVideoURI" section. I have recently been working on a similar thing including this and it seems that the video has to be retrieved via server. You will need to store your videos on a server then call it here. That's what happened with me anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜