获取系统卷 iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7255006/
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
Get System Volume iOS
提问by Joris van Liempd iDeveloper
My case is simple: I need to play a warning signal and want to make sure the user will hear it, so I want to check the system volume.
我的情况很简单:我需要播放警告信号并希望确保用户会听到它,所以我想检查系统音量。
How can I find out what the current system volume is?
我怎样才能知道当前的系统音量是多少?
采纳答案by papahabla
Update for Swift
Swift 更新
let vol = AVAudioSession.sharedInstance().outputVolume
The audio session can provide output volume (iOS >= 6.0).
音频会话可以提供输出音量(iOS >= 6.0)。
float vol = [[AVAudioSession sharedInstance] outputVolume];
NSLog(@"output volume: %1.2f dB", 20.f*log10f(vol+FLT_MIN));
回答by borked
Try this:
尝试这个:
MPMusicPlayerController *iPod = [MPMusicPlayerController iPodMusicPlayer];
float volumeLevel = iPod.volume;
You need to import the MediaPlayer framework.
您需要导入 MediaPlayer 框架。
回答by ziguli
This works fine:
这工作正常:
Float32 volume;
UInt32 dataSize = sizeof(Float32);
AudioSessionGetProperty (
kAudioSessionProperty_CurrentHardwareOutputVolume,
&dataSize,
&volume
);
回答by Vasilii Muravev
Swift 3.1
斯威夫特 3.1
let audioSession = AVAudioSession.sharedInstance()
var volume: Float?
do {
try audioSession.setActive(true)
volume = audioSession.outputVolume
} catch {
print("Error Setting Up Audio Session")
}
audioSession.setActive(true)
- important
audioSession.setActive(true)
-重要
回答by Dasoga
For Swift 2:
对于Swift 2:
let volume = AVAudioSession.sharedInstance().outputVolume
print("Output volume: \(volume)")
回答by Suraj Pathak
You can use the default system's volume View and add to wherever you need it. In my case I required it in my own music player. It's easy and hassle free. Just add the view, and everything is done. This is explained in Apple's MPVolume Class Reference.
您可以使用默认系统的卷视图并添加到您需要的任何位置。就我而言,我需要在我自己的音乐播放器中使用它。这很容易,没有麻烦。只需添加视图,一切就完成了。这在 Apple 的MPVolume Class Reference 中有解释。
mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView =
[[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
[myVolumeView release];
回答by LuAndre
Swift 2.2, make sure to import MediaPlayer
Swift 2.2,确保导入 MediaPlayer
private func setupVolumeListener()
{
let frameView:CGRect = CGRectMake(0, 0, 0, 0)
let volumeView = MPVolumeView(frame: frameView)
//self.window?.addSubview(volumeView) //use in app delegate
self.view.addSubview(volumeView) //use in a view controller
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(volumeChanged(_:)), name: "AVSystemController_SystemVolumeDidChangeNotification", object: nil)
}//eom
func volumeChanged(notification:NSNotification)
{
let volume = notification.userInfo!["AVSystemController_AudioVolumeNotificationParameter"]
let category = notification.userInfo!["AVSystemController_AudioCategoryNotificationParameter"]
let reason = notification.userInfo!["AVSystemController_AudioVolumeChangeReasonNotificationParameter"]
print("volume: \(volume!)")
print("category: \(category!)")
print("reason: \(reason!)")
print("\n")
}//eom
回答by Suat KARAKUSOGLU
I have prepared a class with static methods in order to deal with the volume of ios devices. Let me share with you :)
我准备了一个带有静态方法的类来处理 ios 设备的数量。让我与你分享:)
import AVFoundation
class HeadPhoneDetectHelper {
class func isHeadPhoneConnected() -> Bool
{
do{
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setActive(true)
let currentRoute = audioSession.currentRoute
let headPhonePortDescriptionArray = currentRoute.outputs.filter{##代码##.portType == AVAudioSessionPortHeadphones}
let isHeadPhoneConnected = headPhonePortDescriptionArray.count != 0
return isHeadPhoneConnected
}catch{
print("Error while checking head phone connection : \(error)")
}
return false
}
class func isVolumeLevelAppropriate() -> Bool
{
let minimumVolumeLevelToAccept = 100
let currentVolumeLevel = HeadPhoneDetectHelper.getVolumeLevelAsPercentage()
let isVolumeLevelAppropriate = currentVolumeLevel >= minimumVolumeLevelToAccept
return isVolumeLevelAppropriate
}
class func getVolumeLevelAsPercentage() -> Int
{
do{
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setActive(true)
let audioVolume = audioSession.outputVolume
let audioVolumePercentage = audioVolume * 100
return Int(audioVolumePercentage)
}catch{
print("Error while getting volume level \(error)")
}
return 0
}
}