47

我的情况很简单:我需要播放警告信号并希望确保用户听到它,所以我想检查系统音量。

如何找出当前的系统音量是多少?

4

8 回答 8

48

斯威夫特更新

let vol = AVAudioSession.sharedInstance().outputVolume

音频会话可以提供输出音量(iOS >= 6.0)。

float vol = [[AVAudioSession sharedInstance] outputVolume];
NSLog(@"output volume: %1.2f dB", 20.f*log10f(vol+FLT_MIN));
于 2013-05-06T19:20:05.987 回答
15

斯威夫特 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)-重要

于 2017-05-08T14:32:49.627 回答
13

尝试这个:

MPMusicPlayerController *iPod = [MPMusicPlayerController iPodMusicPlayer];
float volumeLevel = iPod.volume;

您需要导入 MediaPlayer 框架。

于 2011-09-19T17:41:45.123 回答
12

这工作正常:

Float32 volume;
UInt32 dataSize = sizeof(Float32);

AudioSessionGetProperty (
                     kAudioSessionProperty_CurrentHardwareOutputVolume,
                     &dataSize,
                     &volume
                     );
于 2011-10-17T09:01:14.157 回答
12

对于斯威夫特 2

let volume = AVAudioSession.sharedInstance().outputVolume   
print("Output volume: \(volume)")
于 2016-03-17T00:35:39.637 回答
5

您可以使用默认系统的卷视图并添加到您需要的任何位置。就我而言,我需要在我自己的音乐播放器中使用它。这很容易而且没有麻烦。只需添加视图,一切就完成了。这在 Apple 的MPVolume Class Reference中有解释。

mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView =
[[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
[myVolumeView release];
于 2012-11-23T02:26:48.810 回答
2

我准备了一个带有静态方法的类,以处理 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{$0.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
}
}
于 2016-02-03T14:25:02.060 回答
1

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
于 2016-05-27T21:08:50.137 回答