iphone - AVAudioPlayer 影响系统声音

标签 iphone ios audio avaudioplayer effect

我使用此代码播放简短的声音效果。当用户单击键盘的返回按钮时会发生这种情况,这会使键盘单击声音效果比平时响亮。是否可以在不影响系统声音的情况下以一定的音量播放效果?

- (void)playCorrectAnswerSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"correct" ofType:@"mp3"];
NSURL *urlPath = [NSURL fileURLWithPath:path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:nil];
self.player.volume = 0.04;
[self.player prepareToPlay];
[self.player play];

}

最佳答案

将应用程序的 Audio Session 类别设置为 AVAudioSessionCategoryAmbient通过将以下代码添加到您的应用程序委托(delegate)的 applicationDidFinishLaunchingWithOptions:方法:

    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
    [[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
    [[AVAudioSession sharedInstance] setDelegate:self];

来自苹果的doc :

This category allows audio from the iPod, Safari, and other built-in applications to play while your application is playing audio.

关于iphone - AVAudioPlayer 影响系统声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299493/

相关文章:

ios - Touchesbegan 和 touchesmoved 函数不适用于 UIImageView

c# - 使用SoundPlayer在C#中播放.wav文件

c# - 具有多个视频帧的DirectShow变换过滤器-与音频同步

audio - 将音频文件下载到iTunes

ios - 当通过文件 URL 共享时,UIActivityViewController 认为我的 jpg 是视频

ios - 我可以编写一个在 Iphone 上共享音乐流的应用程序吗(将手机用作路由器)

iphone - 如何在 iphone 的 uitableview 中只显示前 5 个单元格

ios - 如果滚动,swift collectionview cell didSelectItemAtIndexPath 会显示错误的单元格

ios - 从没有 Controller 的 UITableView 触发 segue

ios - sd_setImageWithUrl 第一次没有显示图像