我从以下代码获取系统卷
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
- (void) volumeChanged:(NSNotification *)
{
float volume = [[[notification userInfo]
objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
floatValue];
// here volume is the .....
}
但如何从xcode(Objective C)设置系统或设备卷
答案 0 :(得分:1)
答案 1 :(得分:0)
也许这会对你有所帮助(如果你可以访问AVPlayer对象并希望改变播放资产的音量),我会在播放视频时使用这些代码修改AVPlayer中的音量。
- (void)setVolume:(CGFloat)volumeToSet forPlayer:(AVPlayer *)avPlayer {
AVURLAsset *asset = (AVURLAsset *) [[avPlayer currentItem] asset];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:volumeToSet atTime:kCMTimeZero];
[audioInputParams setTrackID:[track trackID]];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
[[avPlayer currentItem] setAudioMix:audioZeroMix];
}
此处有关此解决方案的更多详细信息Adjusting the volume of a playing AVPlayer
如果您想在此处更改设备的音量,这是一种解决方法,可能会在某些iOS版本中出现问题,但现在可以在iOS 7,iOS 8,iOS 9中运行(测试它)。 iOS 9: How to change volume programmatically without showing system sound bar popup?