首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >播放最大speakerPhone音量,然后使用AVAudioPlayer - IOS重置为上一个音量

播放最大speakerPhone音量,然后使用AVAudioPlayer - IOS重置为上一个音量
EN

Stack Overflow用户
提问于 2012-09-13 07:58:19
回答 2查看 2.9K关注 0票数 2

我已经能够将我的音频路由到speakerPhone,但是它仍然不能以最大音量播放。我尝试过audioAlert setVolume:1.0,它仍然保持在用户当前设置的音量。如果有人知道我做错了什么,我将不胜感激。

下面是我的代码:

代码语言:javascript
复制
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];

感谢PaulPerry!

现在解决了:

我包含了一些额外的内容,以便使用下面的代码将其设置回以前的卷:

代码语言:javascript
复制
 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];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-13 08:13:42

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

浮动音量= 1.0;[MPMusicPlayerController applicationMusicPlayer设置音量:音量];

票数 4
EN

Stack Overflow用户

发布于 2012-09-13 08:06:54

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

代码语言:javascript
复制
[audioAlert prepareToPlay]
[audioAlert setVolume:1.0]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12397658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档