MPVolumeView,避免显示“无可用卷”
我有一个包含 MPVolumeView
的项目。它已设置完毕,并且可以正常工作,唯一的问题是,当我将设备静音时,会出现文本“无可用卷”,而不是 MPVolumeView
。我希望在设备静音时禁用 MPVolumeView
的滑块。
VolumeView 在视图 volumeBounds
中使用该视图的边界进行初始化。
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:volumeBounds.bounds] autorelease];
[volumeBounds addSubview:volumeView];
[volumeView sizeToFit];
谢谢:)
如果您有兴趣帮助我解决其他问题,请查看此问题
I have a project with a MPVolumeView
in it. It is set up, and it works, the only thing is that when I mute the device, the text "No Volume Available"
comes up instead of the MPVolumeView
. I would rather like the slider of the MPVolumeView
to be disabled when the device is muted.
The volumeView is initialized in the view volumeBounds
, with that view's bounds.
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:volumeBounds.bounds] autorelease];
[volumeBounds addSubview:volumeView];
[volumeView sizeToFit];
Thanks :)
If you are interested in helping me with something else, check out this question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 AudioServices 来监听硬件体积。当音量变为零时,将 MPVolumeSlider 的 alpha 设置为零,并将您自己禁用的 UISlider 放置在同一位置。使滑块外观看起来像音量滑块。
kAudioSessionProperty_AudioRouteChanged
也可能有用。如果您在 MPVolumeView 下浏览视图层次结构,您应该会找到一个 UISlider。如果没有,或者它被隐藏,您就知道静音字符串正在显示。
编辑:
这描述了您的侦听器的函数原型。要将消息传递给类的实例,请执行类似以下操作:
Use AudioServices to listen for the hardware volume. When the volume goes to zero, set the alpha of the MPVolumeSlider to zero and place your own disabled UISlider in the same position. Skin your slider to look like the volume slider.
kAudioSessionProperty_AudioRouteChanged
might also be useful.If you walk the view hierarchy under the MPVolumeView, you should find a UISlider. If not, or if it is hidden, you know the mute string is showing.
Edit:
This describes the function prototype for your listener. To pass the message to an instance of your class, do something similar to:
为了简单起见,我最终采用了这个解决方案。
在 Objective-C 中:
在 Swift 中:
请参阅以下
appearanceWhenContainedWithin
方法的答案:Swift 中的appearanceWhenContainedIn
它只是隐藏“没有可用卷”文本,而不是替换为禁用的文本UISlider 不用担心 MPVolumeView 滑块和 UISlider 之间的对齐。
AVPlayer 有
volume
属性,但其文档说:AVAudioSession 具有只读
outputVolume
属性,其文档如下:对于这些限制,简单的解决方案(或解决方法)是将文本颜色设置为透明。
I ended up with this solution for simplicity.
In Objective-C:
In Swift:
Refer to the following answer for
appearanceWhenContainedWithin
method:appearanceWhenContainedIn in Swift
It just hides "No Volume Available" text rather than replacing with a disabled UISlider not to worry about the alignment between the MPVolumeView slider and UISlider.
AVPlayer has
volume
property but its document says:AVAudioSession has read-only
outputVolume
property and its document says:For the limitations, the simple solution (or workaround) is just setting the text color to clear.