How to get System Volume iOS?

夙愿已清 提交于 2020-01-04 04:18:07

问题


I found an example here, but it does not work on ios6.1.3 (iphone 4s). always return 0.187500

Code:

Float32 volume;
UInt32 dataSize = sizeof(Float32);

AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionGetProperty (
                     kAudioSessionProperty_CurrentHardwareOutputVolume,
                     &dataSize,
                     &volume
                     );
NSLog(@"%f", volume);

[AVAudioSession sharedInstance].outputVolume - also return 0.187500


回答1:


You need to initialize the audio session with:

AudioSessionInitialize (NULL, NULL, NULL, NULL);

in viewDidLoad or similar.

However, it's generally a better idea to make use of the new AVAudioSession class if you're targeting iOS 6+, which handles that for you:

float volume = [AVAudioSession sharedInstance].outputVolume;



回答2:


float volume = [[AVAudioSession sharedInstance] outputVolume];



来源:https://stackoverflow.com/questions/18257199/how-to-get-system-volume-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!