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

前端 未结 7 1774
无人及你
无人及你 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条回答
  •  猫巷女王i
    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();
    

提交回复
热议问题