Ios 允许用户设置iphone接收器音量,而不是在通话期间

Ios 允许用户设置iphone接收器音量,而不是在通话期间,ios,iphone,avaudiosession,Ios,Iphone,Avaudiosession,到目前为止,我的应用程序通过使用iphone接收器(电话扬声器)播放音频 我让用户有机会使用MPVolumeView MPVolumeView *volumeView1 = [[MPVolumeView alloc] initWithFrame:CGRectMake(4, 316, 300, 50)]; [volumeView1 sizeToFit]; [self.view addSubview:volumeView1]; 但是,调整MPVolumeView只会影响手机大扬声器的音量,而不会影

到目前为止,我的应用程序通过使用iphone接收器(电话扬声器)播放音频

我让用户有机会使用
MPVolumeView

MPVolumeView *volumeView1 = [[MPVolumeView alloc] initWithFrame:CGRectMake(4, 316, 300, 50)];
[volumeView1 sizeToFit];
[self.view addSubview:volumeView1];
但是,调整
MPVolumeView
只会影响手机大扬声器的音量,而不会影响接收器的音量


当会话中没有电话呼叫时,我如何向用户提供调整接收器音量的机会?

无论如何,API都不允许通过公共方法这样做。请参阅苹果关于设置音量的文档:

我让它工作了,下面的代码允许用户设置接收器的音量,尽管音频文件必须在后台播放。另外,
AVAudioPlayer
必须是
@属性

AVAudioSession *sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[sharedSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];

MPVolumeView *volumeView1 = [[MPVolumeView alloc] initWithFrame:CGRectMake(4, 316, 300, 50)];
[volumeView1 sizeToFit];
[self.view addSubview:volumeView1];

NSURL *soundFileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"silence" ofType:@"m4a"]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[self.audioPlayer play];

我不相信你有权访问这个,这是一个iOS本机功能。是的,我看了一下,但我没有看到它提到允许用户设置接收器的音量通常是不允许的。换句话说,我想知道是否有其他方法来完成它,可能不涉及MPVolumeView
AVAudioSession *sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[sharedSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];

MPVolumeView *volumeView1 = [[MPVolumeView alloc] initWithFrame:CGRectMake(4, 316, 300, 50)];
[volumeView1 sizeToFit];
[self.view addSubview:volumeView1];

NSURL *soundFileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"silence" ofType:@"m4a"]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[self.audioPlayer play];