2

我已经能够将我的音频路由到扬声器电话,但它仍然无法以最大音量播放。我已经尝试过 [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];
4

2 回答 2

4

这会将整个系统的音量重置为最大值。

浮动体积 = 1.0; [[MPMusicPlayerController applicationMusicPlayer] setVolume:volume];

于 2012-09-13T00:13:41.710 回答
0

您如何首先将其初始化为 0,然后在将其[audioAlert prepareToPlay]设置回 1 之后:

[audioAlert prepareToPlay]
[audioAlert setVolume:1.0]
于 2012-09-13T00:06:53.560 回答