MPMusicPlayerController setVolume为deprecated since iOS 7
还有其他方法可以改变系统音乐音量吗?最好没有用户交互。 其重要特性:为AppStore的任何闹钟自动增加音量。
答案 0 :(得分:51)
准确地回答你的问题: 是的,还有其他方法可以在没有用户交互的情况下更改系统容量。
直到最近,我才认为只能使用私有API 以编程方式使用MPVolumeView更改音量。但我刚刚验证过,更改volumeSlider和faking滑块的touchUP事件的值有效:
MPVolumeView* volumeView = [[MPVolumeView alloc] init];
//find the volumeSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
[volumeViewSlider setValue:1.0f animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
(当滑块接收到touchUP事件时,它将自行调用_commitVolumeChange
方法,这将改变系统音量)
答案 1 :(得分:13)
在Apple认为适合取消此决定之前,我发现了两种补救方法:
答案 2 :(得分:9)
这样做:
let masterVolumeSlider: MPVolumeView = MPVolumeView()
if let view = masterVolumeSlider.subviews.first as? UISlider{
view.value = 1.0
}
答案 3 :(得分:4)
@Hurden,我写了一个Swift代码来实现MPVolumeSlider:
for view in mpVolumeView.subviews {
let uiview: UIView = view as UIView
//println("\(uiview.description)")
if uiview.description.rangesOfString("MPVolumeSlider").first != nil {
mpVolumeSilder = (uiview as UISlider)
currentDeviceVolume = mpVolumeSilder!.value
return
}
}
func rangeOfString扩展名String可以在Swift: A pure Swift method for returning ranges of a String instance (Xcode 6 Beta 5)
找到并使用此代码让手势和mpvolumeslider一起工作
@IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.view)
let dx = (translation.x-lastTranslationX)
let volumeChanged = Float(dx / mpVolumeView.frame.width)
currentDeviceVolume = currentDeviceVolume + Float(volumeChanged)
if currentDeviceVolume > 1 {
currentDeviceVolume = 1
} else if currentDeviceVolume < 0 {
currentDeviceVolume = 0
}
mpVolumeSilder!.value = currentDeviceVolume
if recognizer.state == .Changed {
lastTranslationX = translation.x
}
if recognizer.state == .Ended || recognizer.state == .Began {
lastTranslationX = 0
}
}
答案 4 :(得分:1)
Swift版本:
// Outlet added in Storyboard (Add UIView then set class to MPVolumeView)
@IBOutlet weak var mpVolumeView: MPVolumeView!
// Get volume slider within MPVolumeView
for subview in self.mpVolumeView.subviews {
if (subview as UIView).description.rangeOfString("MPVolumeSlider") != nil {
// Set volume
let volumeSlider = subview as UISlider
volumeSlider.value = 1
// Works with or without the following line:
// volumeSlider.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
break
}
}
答案 5 :(得分:1)
显然 是一种更改系统音量而不显示任何内容的方法。
最重要的是,它适用于 iOS 11 。
以下是我如何实现它:
1)在所需的ViewController中创建两个变量
let volumeView = MPVolumeView()
var slider: UISlider?
2)将以下代码添加到viewDidLoad
中volumeView.alpha = 0.01
self.view.addSubview(volumeView)
if let view = volumeView.subviews.first as? UISlider {
slider = view
}
3)随时改变音量
slider?.value = 0.4
答案 6 :(得分:0)
这个解决方案有点紧张,我认为官方API不存在有点奇怪但是这里是基于环境光的帖子构建的我的Swift解决方案
var _volumeView = MPVolumeView()
var _volumeSlider : UISlider? = nil
self.view.addSubview(_volumeView)
_volumeView.hidden = true
var i = 0
while i < _volumeView.subviews.count {
if let _r = _volumeView.subviews[i] as? UISlider {
_volumeSlider = _r
break
}
++i
}
答案 7 :(得分:0)
let masterVolumeSlider : MPVolumeView = MPVolumeView()
if let view = masterVolumeSlider.subviews.first as? UISlider{
view.value = fVolume!
view.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
}