2011-08-31 181 views

回答

35

音頻會話可以提供輸出音量(iOS> = 6.0)。

float vol = [[AVAudioSession sharedInstance] outputVolume]; 
NSLog(@"output volume: %1.2f dB", 20.f*log10f(vol+FLT_MIN)); 
+3

測試:在iOS 7上工作。 – LordParsley

+0

無法在iOS 7中工作Borked解決方案正在工作 – ashokdy

+4

仍在iOS 9.1中工作 – picciano

14

試試這個:

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

您需要導入MediaPlayer的框架。

+0

謝謝!解決方案就像一個魅力。 –

+1

這個解決方案不適用於iOS 7 :( – mbelsky

+0

這也適用於iOS 7 ...謝謝Borked – ashokdy

13

這工作得很好:

Float32 volume; 
UInt32 dataSize = sizeof(Float32); 

AudioSessionGetProperty (
        kAudioSessionProperty_CurrentHardwareOutputVolume, 
        &dataSize, 
        &volume 
        ); 
+0

更好的在我的情況!現在我可以溝MediaPlayer Framework –

+6

這在iOS 7中已被棄用 - 有人知道新方法嗎? –

4

您可以使用系統默認的音量查看和添加,無論你需要它。在我的情況下,我需要在我自己的音樂播放器中播放。這很容易,無憂無慮。只需添加視圖,一切都完成了。這在Apple的MPVolume Class Reference中有解釋。

mpVolumeViewParentView.backgroundColor = [UIColor clearColor]; 
MPVolumeView *myVolumeView = 
[[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds]; 
[mpVolumeViewParentView addSubview: myVolumeView]; 
[myVolumeView release]; 
+0

問題不在於如何爲用戶提供更改音量的方法,而是如何檢測音量是否足夠高。 –

+0

真正與問題無關,但在如何調整系統音量的更大範圍內仍然有用 –

0

我準備用靜態方法的類,以應付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 
} 
} 
9

對於斯威夫特2

let volume = AVAudioSession.sharedInstance().outputVolume 
print("Output volume: \(volume)") 
1

雨燕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 
4

雨燕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) - 重要