覆盖iPhone应用程序中的铃声音量 [英] Override ringer volume in iPhone apps

查看:185
本文介绍了覆盖iPhone应用程序中的铃声音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个可以轻松播放大量声音的应用程序:

I have built an app that plays lots of sounds the easy way:

AudioServicesPlaySystemSound(someSoundID);

当我使用设备音量按钮增大或减小音量时,我实际改变的音量是手机的铃声音量。因此,如果你减少它并退出应用程序,那么你的手机会安静地响起......

When I use the device volume buttons to increase or decrease the volume, the volume I actually change is the phone's ringer volume. So if you decrease it and exit the app then your phone will ring quietly...

有没有一种简单的方法来覆盖它并实际改变我的应用程序的音量?

Is there an easy way to override this and actually change my application's volume?

推荐答案

我找到了解决方案,我认为这是一个常见的问题。所以这就是你的应用程序如何拥有自己的音量,而不是弄乱用户的铃声音量,即使你只是以系统声音的形式播放声音。

I have found the solution to what I thought would be a common problem. So here is how your app can have its own volume, and not mess with the user's ringer volume, even if you are only playing sounds as System Sounds.

你必须导入AVFoundation框架并在一个对象中保持加载整个时间你的应用程序运行(或查看或应用程序委托)你初始化AVAudioPlayer,给它一个文件来播放并将其设置为prepareToPlay它...

You have to import the AVFoundation framework and in an object that stays loaded the whole time your app runs (or view, or the app delegate) you have initialize a AVAudioPlayer, give it a file to play and set it to "prepareToPlay" it...

这是我在主视图中所做的(用于将其他视图作为子视图加载):头文件中的

This is what i did in my main View (that is used to load other views as subviews): in the header file:

#import <AVFoundation/AVFoundation.h>

@interface MainViewController : UIViewController {
    AVAudioPlayer *volumeOverridePlayer;
}

@property (nonatomic, retain) AVAudioPlayer *volumeOverridePlayer;

在实施文件中:

    @synthesize volumeOverridePlayer;

- (void)viewDidLoad
{
    [super viewDidLoad];

    volumeOverridePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"something" ofType:@"caf"]] error:nil];
    [volumeOverridePlayer prepareToPlay];
//...
}

让玩家准备好玩你的文件,享受自己的音量控制,而不必通过它播放声音!
当然,请记得以dealloc发布它。

Just leave the player prepared to play your file and enjoy your own volume control without having to play the sounds through it! And of course, remember to release it in dealloc.

这篇关于覆盖iPhone应用程序中的铃声音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆