如何让 Android MediaController 从布局 xml 中出现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1759862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:36:43  来源:igfitidea点击:

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

android

提问by DustinB

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

我已经使用以下 XML 创建了一个 layout.xml 文件,但是我根本无法显示 MediaController。

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

在代码中,我可以从代码中获取对它的引用,调用 show() 等,但它永远不会出现。

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. :)

我在 android 开发文档 ( MediaController.html) 中注意到它指出,“使用这个类的方法是以编程方式实例化它。”,所以也许我是一个笨蛋,只需要做一些不同的事情,比如不做我在做什么。:)

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?

如果我以编程方式执行它,我可以让它出现,但我需要它总是出现在屏幕上。我只是愚蠢,还是只是不想通过 XML 膨胀?

回答by CommonsWare

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.

然后,当您点击屏幕底部边缘时,它就会出现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 projectthat 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.

就一直保持在屏幕上而言……我不知道这是否可能。您始终可以创建自己的控制器——这是一个创建自己的弹出式半透明控制器的示例项目。这真的不是那么难,你可以完全控制一切,包括安排它一直在屏幕上。

回答by Rob Gorman

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

您可以防止 MediaController 隐藏扩展的 MediaController 并覆盖 hide() 什么也不做。例如:

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 duessi

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

通过使用 show(0)媒体控制器将一直显示,直到通过与播放器的交互调用 hide()。不幸的是,我发现还没有办法阻止 hide() ;-(

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)

等待屏幕构建以避免 android 中的错误(此处为 1.5 秒)

回答by Napolean

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

您可以从布局 xml 中膨胀 mediacontroller。同样在获得它的参考后,可以将视频视图设置为锚点。

  • But this will cause a crash as soon as you try to touch mediacontroller (I am trying to figure out why its happening).
  • 但是,只要您尝试触摸 mediacontroller,这就会导致崩溃(我想弄清楚为什么会发生这种情况)。

Also In this mediaController will appear always.

同样在这个 mediaController 中将始终出现。

<?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();
            }
        });
    }
}

回答by Andre

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.

我认为这是您的“mVideoView.setVideoURI”部分。我最近一直在做类似的事情,包括这个,似乎必须通过服务器检索视频。您需要将视频存储在服务器上,然后在此处调用。反正我就是这样。

回答by Adil Hussain

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:

当在布局文件中声明我的 MediaController,在代码中获取它的句柄(使用该Activity.findViewById(int id)方法)并将其附加到我的 VideoView(使用该VideoView.setMediaController(MediaController controller)方法)时,我发现它有时表现不佳(强制关闭应用程序)。相反,我在代码中创建它,将它附加到 VideoView,然后使用VideoView.setAnchorView(View view)如下方法指定它应该在 Activity 中的显示位置:

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();

回答by dpn

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

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)

(同样有趣的是谷歌将这个问题添加到搜索结果的速度有多快)