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

前端 未结 7 1726
无人及你
无人及你 2020-12-28 11:10

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



        
相关标签:
7条回答
  • 2020-12-28 11:51

    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 讨论(0)
  • 2020-12-28 11:54

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-28 11:58

    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.

    0 讨论(0)
  • 2020-12-28 12:01

    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();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-28 12:05

    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();
    
    0 讨论(0)
  • 2020-12-28 12:07

    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)

    0 讨论(0)
提交回复
热议问题