我已经能够将我的音频路由到扬声器电话,但它仍然无法以最大音量播放。我已经尝试过 [audioAlert setVolume:1.0] 并且它仍然保持在用户当前设置的音量。如果有人知道我做错了什么,我会非常感激。
这是我的代码:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HsTR_soundEFX_2-1" ofType:@"mp3"]];
audioAlert= [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioAlert.delegate = self;
[audioAlert setVolume:1.0];
[audioAlert prepareToPlay];
[audioAlert play];
感谢保罗佩里!
现在解决了:
我添加了一些额外的内容,以便使用以下代码将其设置回上一卷:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:NULL];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HsTR_soundEFX_2-1" ofType:@"mp3"]];
audioAlert= [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioAlert.delegate = self;
[audioAlert prepareToPlay];
float maxVolume = 1.0;
float currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:maxVolume];
[audioAlert play];
//set up timer to wait until audioAlert is finished playing,
// and then call the line below to set the volume back to it's original setting
[[MPMusicPlayerController applicationMusicPlayer]setVolume:currentVolume];