如何为 android MediaController 创建自定义 UI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12482203/
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
how to create custom UI for android MediaController
提问by user1681001
I want to customize the controls in the MediaController for my video player. I want to swap out the image for the play button, change the skin, change the color, ect.
我想为我的视频播放器自定义 MediaController 中的控件。我想换出播放按钮的图像,更改皮肤,更改颜色等。
Is there a known way of doing this?
有没有一种已知的方法可以做到这一点?
Thanks
谢谢
回答by Blago
I had the same problem on a recent project and ended up creating a custom implementation based on the stock MediaController. It adds a fullscreen button at the far right, but even if that's not what you want this class should be a good starting point.
我在最近的一个项目中遇到了同样的问题,最终创建了一个基于库存 MediaController 的自定义实现。它在最右侧添加了一个全屏按钮,但即使这不是您想要的,这个类也应该是一个很好的起点。
Code:
代码:
- VideoControllerView.java - http://pastebin.com/1V63aVSg
- media_controller.xml - http://pastebin.com/rS4xqMej
- VideoControllerView.java - http://pastebin.com/1V63aVSg
- media_controller.xml - http://pastebin.com/rS4xqMej
Image resources:
图片资源:
Gotchas:
陷阱:
- The class assumes the anchor view is a FrameLayout to position itself at the bottom.
- The class does not use a Window to ensure control floats on top of the anchor. However, this hasn't been a problem for me as the show() method does mAnchor.addView() every time.
- The class uses its own MediaPlayerControl protocol. This allows customizations such as toggleFullScreen()
- 该类假定锚视图是一个 FrameLayout 以将自身定位在底部。
- 该类不使用 Window 来确保控件浮动在锚点之上。但是,这对我来说不是问题,因为 show() 方法每次都执行 mAnchor.addView()。
- 该类使用其自己的 MediaPlayerControl 协议。这允许自定义,例如 toggleFullScreen()
EDIT (2013): See this tutorial for more details: http://www.brightec.co.uk/blog/custom-android-media-controller
编辑(2013 年):有关更多详细信息,请参阅本教程:http: //www.brightec.co.uk/blog/custom-android-media-controller
EDIT (2018): You can now use the wonderful ExoPlayer and fully customize your player UI simply by overriding a layout file.
编辑(2018 年):您现在可以使用出色的 ExoPlayer 并通过覆盖布局文件来完全自定义您的播放器 UI。
回答by hussamabd
I tested it and it works great on 4.4 kitkat, but like KAPLANDROID said blackscreen appeared on android 2.3.6 but the music and videocontroller works fine,
我测试了它,它在 4.4 kitkat 上运行良好,但就像 KAPLANDROID 所说的黑屏出现在 android 2.3.6 但音乐和视频控制器工作正常,
Have found a solution, just put this code
找到了解决办法,把这个代码
videoHolder.setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
after this
在这之后
videoHolder.addCallback( this );
and you are ready to go
你准备好了