我已经能够将我的音频路由到扬声器电话,但它仍然无法以最大音量播放。我已经尝试过 [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];
Best Answer-推荐答案 strong>
这会将整个系统的音量重置为最大。
float 体积 = 1.0;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];
关于iphone - 播放最大扬声器电话音量,然后使用 AVAudioPlayer 重置为以前的音量 --- IOS,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/12397658/
|