如何在 iOS 上更改设备音量 - 而不是音乐音量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10286744/
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 change device Volume on iOS - not music volume
提问by brush51
I want to change the device volume on iOS (iphone).
我想在 iOS (iphone) 上更改设备音量。
I know that i can change the volume of music library with this lines below:
我知道我可以使用以下几行更改音乐库的音量:
//implement at first MediaPlayer framework
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
musicPlayer.volume = 1;
But thats not my aim.
I want to change the device volumeor let me say the volume of ringer.
但这不是我的目标。
我想更改设备音量或让我说ringer的音量。
How can i do that? just change the DEVICEvolume?
我怎样才能做到这一点?只是改变设备音量?
回答by msrdjan
To answer brush51's question:
回答brush51的问题:
How can i do that? just change the DEVICE volume?
我怎样才能做到这一点?只是改变设备音量?
As 0x7fffffff suggested:
正如 0x7ffffffff 建议的那样:
You cannot change device volume programatically, however MPVolumeView (volume slider) is there to change device volume but only through user interaction.
您不能以编程方式更改设备音量,但是 MPVolumeView(音量滑块)可以更改设备音量,但只能通过用户交互。
So, Apple recommends using MPVolumeView
, so I came up with this:
所以,Apple 建议使用MPVolumeView
,所以我想出了这个:
Add volumeSlider
property:
添加volumeSlider
属性:
@property (nonatomic, strong) UISlider *volumeSlider;
Init MPVolumeView
and add somewhere to your view (can be hidden, without frame, or empty because of showsRouteButton = NO
and showsVolumeSlider = NO
):
初始化MPVolumeView
并将某处添加到您的视图中(可以隐藏,没有框架,或者因为showsRouteButton = NO
和 为空showsVolumeSlider = NO
):
MPVolumeView *volumeView = [MPVolumeView new];
volumeView.showsRouteButton = NO;
volumeView.showsVolumeSlider = NO;
[self.view addSubview:volumeView];
Find and save reference to UISlider
:
查找并保存对以下内容的引用UISlider
:
__weak __typeof(self)weakSelf = self;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UISlider class]]) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.volumeSlider = obj;
*stop = YES;
}
}];
Add target action for UIControlEventValueChanged
:
添加目标操作UIControlEventValueChanged
:
[self.volumeSlider addTarget:self action:@selector(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];
And then detect volume changing (i.e. by the hardware volume controls):
然后检测音量变化(即通过硬件音量控制):
- (void)handleVolumeChanged:(id)sender
{
NSLog(@"%s - %f", __PRETTY_FUNCTION__, self.volumeSlider.value);
}
and also other way around, you can set volume by:
以及其他方式,您可以通过以下方式设置音量:
self.volumeSlider.value = < some value between 0.0f and 1.0f >;
Hope this helps (and that Apple doesn't remove MPVolumeSlider from MPVolumeView).
希望这会有所帮助(并且 Apple 不会从 MPVolumeView 中删除 MPVolumeSlider)。
回答by Nikita Zernov
Here's what I've done:
这是我所做的:
func setSystemVolume(volume: Float) {
let volumeView = MPVolumeView()
for view in volumeView.subviews {
if (NSStringFromClass(view.classForCoder) == "MPVolumeSlider") {
let slider = view as! UISlider
slider.setValue(volume, animated: false)
}
}
}
As property volume
in MPMusicPlayerController
was deprecate in iOS 7. This is only method you can do that.
由于属性volume
inMPMusicPlayerController
在 iOS 7中已弃用。这是您可以执行的唯一方法。
回答by Lars335
This (Swift) solution worked great for me: http://weimenglee.blogspot.com/2015/07/ios-tip-programmatically-adjust-device.html
这个(Swift)解决方案对我很有用:http: //weimenglee.blogspot.com/2015/07/ios-tip-programmatically-adjust-device.html
import MediaPlayer
let volumeView = MPVolumeView()
if let view = volumeView.subviews.first as? UISlider{
view.value = 0.1 //---0 t0 1.0---
}
回答by Paul Slocum
You have to use applicationMusicPlayer instead of iPodMusicPlayer to set the system volume:
您必须使用 applicationMusicPlayer 而不是 iPodMusicPlayer 来设置系统音量:
#import <MediaPlayer/MediaPlayer.h>
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 1; // max volume
musicPlayer.volume = 0; // min volume (mute)
musicPlayer.volume = 0.0625; // 1 bar on the overlay volume display
回答by mojuba
Here is a little wrapper class that can set the system volume and aslo notify you on any changes (pass your own change responder to setTarget:action:).
这是一个小包装类,可以设置系统音量并通知您任何更改(将您自己的更改响应程序传递给 setTarget:action:)。
Edit: if you hide the MPVolumeView control or don't add it as a subview, then the system will show the default volume square in the middle of the screen whenever you change the volume programmatically. If you don't like what the system does, the solution is to set view coordinates outside of the screen. I edited my code below a bit to reflect this.
编辑:如果您隐藏 MPVolumeView 控件或不将其添加为子视图,则每当您以编程方式更改音量时,系统将在屏幕中间显示默认音量方块。如果您不喜欢系统的功能,解决方案是在屏幕外设置视图坐标。我在下面编辑了我的代码以反映这一点。
@import MediaPlayer;
@interface SysVolumeControl : MPVolumeView
@property (nonatomic) float value; // from 0 to 1.0
- (void)setTarget:(id)target action:(SEL)action;
@end
@implementation SysVolumeControl
{
UISlider* _slider;
}
- (id)init
{
if (self = [super initWithFrame:CGRectMake(-300, 0, 200, 50)])
{
for (UIView* view in self.subviews)
{
if ([view isKindOfClass:UISlider.class])
{
_slider = (UISlider*)view;
break;
}
}
}
return self;
}
- (float)value
{ return _slider.value; }
- (void)setValue:(float)value
{ _slider.value = value; }
- (void)setTarget:(id)target action:(SEL)action
{ [_slider addTarget:target action:action forControlEvents:UIControlEventValueChanged]; }
@end
Then create an instance and use it:
然后创建一个实例并使用它:
SysVolumeControl* sysVolumeControl = [SysVolumeControl new];
[myView addSubview:sysVolumeControl];
sysVolumeControl.value = 1.0;
[sysVolumeControl setTarget:self action:@selector(mySysVolumeResponder:)];
回答by Northern Captain
Creating MPVolumeView on the fly to get the slider and set the volume doesn't work in iOS 11.4 anymore. I solved the issue by adding new MPVolumeView to my UIViewController view, otherwise it didn't set the volume. As I added it to the controller I also need to set the volume view position to be outside of the screen.
动态创建 MPVolumeView 以获取滑块并设置音量在 iOS 11.4 中不再适用。我通过向 UIViewController 视图添加新的 MPVolumeView 解决了这个问题,否则它没有设置音量。当我将它添加到控制器时,我还需要将音量视图位置设置在屏幕之外。
The code is in Swift 4:
代码在 Swift 4 中:
let volumeControl = MPVolumeView(frame: CGRect(x: 0, y: 0, width: 120, height: 120))
override func viewDidLoad() {
self.view.addSubview(volumeControl);
}
override func viewDidLayoutSubviews() {
volumeControl.frame = CGRect(x: -120, y: -120, width: 100, height: 100);
}
func setMaxVolume() {
let lst = volumeControl.subviews.filter{NSStringFromClass(var player: AVPlayer!
...
player.volume = 0.5 // 50% level
.classForCoder) == "MPVolumeSlider"}
let slider = lst.first as? UISlider
slider?.setValue(1, animated: false)
}
回答by Alexander Volkov
The app should not change device global settings and you should not do that. User can do that using standard iOS features.
Instead, if you playing a sound/video, then use player's volume
:
该应用程序不应更改设备全局设置,您也不应这样做。用户可以使用标准的 iOS 功能做到这一点。相反,如果您播放声音/视频,请使用 player's volume
:
func setVolumeTo(volume: Float) {
(MPVolumeView().subviews.filter{NSStringFromClass(##代码##.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(volume, animated: false)
}
回答by budidino
Swift 3- I use this:
Swift 3- 我用这个:
##代码##