背景
在连接耳机时尝试将音量增加到某个点以上时,Apple 的一些 iOS 版本引入了警告。根据我的阅读,这是由于欧盟的健康和安全建议,并且仅在欧盟销售/使用的设备上可见。
当音量增加到超过某个点时,音量 slider 停止, slider 顶部闪烁黄色/橙色。警告出现后,用户仍然可以将音量提高到这一点,就好像 slider 只是想确认他们确实知道自己在做什么。
当 slider 不在屏幕上时,可以通过音量叠加看到类似的效果。在第一个(黄色)“高音量”警告中,似乎需要两次单击硬件音量增大按钮才能超过推荐的音量限制。
代码以查看它的实际效果:
import UIKit
import MediaPlayer
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let volumeView = MPVolumeView()
view.addSubview(volumeView)
volumeView.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[volume]-|", options: nil, metrics: nil, views: ["volume": volumeView]) as! [NSLayoutConstraint])
view.addConstraint(NSLayoutConstraint(item: volumeView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: volumeView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 50))
let player = MPMusicPlayerController.applicationMusicPlayer()
player.setQueueWithQuery(MPMediaQuery.songsQuery())
player.play()
}
}
注意:我相信您只会在欧盟激活的设备上看到音量警告问题
据我所知,这种行为是无证的,并且不一致。几次后,闪烁的 slider 音量警告停止出现。在 future 的某个时刻,它会再次出现,可能是经过一段时间后,或者在设备重启后;我不知道。
我在测试 slider 时第一次看到警告,该 slider 在公共(public)控件状态下为轨道和拇指图像设置了空白图像:
let controlStates: [UIControlState] = [.Normal, .Highlighted, .Selected, .Disabled]
for state in controlStates {
volumeView.setMinimumVolumeSliderImage(transparentPixel, forState: state)
volumeView.setMaximumVolumeSliderImage(transparentPixel, forState: state)
volumeView.setVolumeThumbImage(transparentPixel, forState: state)
}
因此,黄色条可能是 .Application
的图像。或 .Reserved
控制状态,或者它可能是完全私有(private)的。我还没有发现,因为我正在等待警告再次开始出现......这使得自定义和测试 slider 的外观变得非常困难。在我的情况下,轨道的闪烁部分与我的自定义 slider 看起来格格不入。我不是在寻找消除警告的方法,只是了解如何自定义其外观,并确保它在所有情况下看起来都很好。
任何人都可以对此有所了解吗?也许任何描述它如何/何时发生的文档(开发人员或其他),或者关于如何测试如此短暂和无法控制的东西的一些想法?
最佳答案
它在 MPVolumeView.h
中进行了讨论头文件。
// Sets the image for the EU volume limit. When appropriate, this image will be displayed on top of the
// maximumVolumeSliderImage. It must be visually distinct from the maximumVolumeSliderImage, and use
// a color similar to the default, to convey a sense of warning to the user. The same image is used for
// all control states. For debugging purposes, switch on the "EU Volume Limit" setting in the Developer
// menu of the Settings application to always enable the volume limit.
@property (nonatomic, strong, nullable) UIImage *volumeWarningSliderImage NS_AVAILABLE_IOS(7_0);
为了进行测试,当您更改上述开发人员设置时,您还应该点击其下方的“重置媒体服务”按钮(或重新启动)。这将使 MPVolumeView 每次超过一定水平时都会闪烁警告栏。 (正如您所提到的,在正常设置下,它实际上仅在高音量聆听一段时间后才会闪烁,并且仅在欧盟 iPhone 上闪烁)。
关于ios - 在 iOS 上自定义 MPVolumeView 的高音量警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29193657/