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

查看:27
本文介绍了覆盖 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天全站免登陆