回答
音頻會話可以提供輸出音量(iOS> = 6.0)。
float vol = [[AVAudioSession sharedInstance] outputVolume];
NSLog(@"output volume: %1.2f dB", 20.f*log10f(vol+FLT_MIN));
這工作得很好:
Float32 volume;
UInt32 dataSize = sizeof(Float32);
AudioSessionGetProperty (
kAudioSessionProperty_CurrentHardwareOutputVolume,
&dataSize,
&volume
);
更好的在我的情況!現在我可以溝MediaPlayer Framework –
這在iOS 7中已被棄用 - 有人知道新方法嗎? –
您可以使用系統默認的音量查看和添加,無論你需要它。在我的情況下,我需要在我自己的音樂播放器中播放。這很容易,無憂無慮。只需添加視圖,一切都完成了。這在Apple的MPVolume Class Reference中有解釋。
mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView =
[[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
[myVolumeView release];
問題不在於如何爲用戶提供更改音量的方法,而是如何檢測音量是否足夠高。 –
真正與問題無關,但在如何調整系統音量的更大範圍內仍然有用 –
我準備用靜態方法的類,以應付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
}
}
對於斯威夫特2:
let volume = AVAudioSession.sharedInstance().outputVolume
print("Output volume: \(volume)")
雨燕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
雨燕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)
- 重要
- 1. iOS系統音量控制
- 2. iOS:系統聲音預覽
- 3. 如何獲取iOS中可用系統聲音的列表?
- 4. 較低的系統音量
- 5. 觸發系統音量條
- 6. 更改系統音量
- 7. c + + win32獲取系統音量加速器
- 8. c#在Windows 10 Mobile中獲取系統音量級別
- 9. 在Python中使用Python獲取系統音量(聲級)
- 10. 系統聲音忽略音量級別
- 11. 系統聲音的控制音量
- 12. 檢查系統音量是否靜音?
- 13. 獲取系統
- 14. 如何設置音量在iOS和避免系統音量彈出
- 15. 的iOS:獲取音量鍵事件
- 16. Android系統音量不會取消靜音直到卸載
- 17. 在iOS中播放系統聲音
- 18. iOS - 系統聲音未播放
- 19. 覆蓋iOS系統音量播放鬧鐘
- 20. 在IOS下設置Swift中的系統音量
- 21. 如何根據UIslider值設置系統音量? IOS Swift 3.0
- 22. 如何控制/強制iOS系統音量[科爾多瓦]
- 23. iOS系統音量變化達到9dB提升
- 24. 如何使用UISlider控制iOS系統音量
- 25. 獲取android系統
- 26. 如何根據系統音量(iOS設備音量物理按鍵)分別設置應用音量?
- 27. 如何播放iOS系統聲音並取消回撥
- 28. 我們可以在iOS中獲取iOS系統鈴聲嗎?
- 29. 系統音量與鈴聲音量掛鉤
- 30. 系統設置將通知音量與鈴聲音量關聯
測試:在iOS 7上工作。 – LordParsley
無法在iOS 7中工作Borked解決方案正在工作 – ashokdy
仍在iOS 9.1中工作 – picciano