标签: avaudiosession

使用VoiceProcessingIO音频单元子类型而不是RemoteIO会导致不必要的输出音量下降

我在语音聊天应用程序中使用RemoteIO.为了启用回声消除,在将"kAudioSessionCategory_PlayAndRecord"设置为会话类型后,我将"kAudioUnitSubType_RemoteIO"更改为"kAudioUnitSubType_VoiceProcessingIO".现在回声消除工作,但输出音量水平相对于之前的RemoteIO输出水平显着下降.有没有人知道如何在使用VoiceProcessingIO时获得与使用RemoteIO时相同的输出音量?

core-audio audiounit ios avaudiosession remoteio

17
推荐指数
1
解决办法
2384
查看次数

检测AVAudioSession何时被"剔除"

我正在开发一个Podcast应用程序.这会使用AVAudioSessionCategoryPlayback会话类型播放音频.这几乎可以完成我想要它做的所有事情.它会暂停其他播放的音乐,当有来电时它会被打断并且通常可以按照我想要的方式工作.

一个例外.当播放更高优先级的音频时(例如,当运行逐向导航应用程序时),我的应用程序中的音频会在音量上下降,但会继续运行.这造成了两种不希望的声音的可怕混合.我宁愿选择我的应用程序中的音频在过度播放的声音中暂停,然后在完成后再次继续.

我尝试过更改不同的AVAudioSession属性AVAudioSessionCategoryOptionMixWithOthers,但这些只会改变低优先级声音与我的应用程序混合的方式.它们不会改变我的如何与更高优先级的声音混合.我也试过观察它的otherAudioPlaying属性,sharedSession但是当我将这些短片重叠时,这并没有引发变化.

有没有办法检测我的应用程序中的音频何时被躲避,以便我可以暂停它?或者,为了防止我的应用程序的音频被躲避,以便将这些其他声音视为中断?

谢谢.

iphone ios avaudiosession avplayer

16
推荐指数
2
解决办法
2493
查看次数

如何设置iOS应用程序以使用USB音频输入和输出到内置扬声器

我有两种不同的吉他适配器,使用闪电连接器连接到我的iphone

当插入适配器1时,该设备成为USB音频麦克风,它通过我的iPhone扬声器播放声音,因为适配器不包含耳机插孔

插入适配器2后,设备将变为USB音频麦克风,但通过适配器上的耳机插孔播放声音.

我正在尝试编写一个与适配器2兼容的应用程序,但不是将声音输出到适配器的耳机插孔,我想通过iPhone的扬声器进行路由.

下面的代码应该可以工作,但我发现,AVAudioSessionPortOverride使用AVAudioSessionPortOverrideSpeaker选项和音频会话的类别调用AVAudioSessionCategoryPlayAndRecord会导致音频使用内置扬声器和麦克风而不管其他设置,基本上忽略setPreferredInput

我不太明白适配器1如何设法从USB音频和输出到扬声器输入,但由于上述限制我的应用程序不能.有人知道解决方案吗?

AVAudioSession* session = [AVAudioSession sharedInstance];
//Set the audioSession category. Needs to be Record or PlayAndRecord to use audioRouteOverride:
[session    setCategory:AVAudioSessionCategoryPlayAndRecord 
            withOptions:AVAudioSessionCategoryOptionMixWithOthers 
            error:nil];

//set the audioSession override
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker 
error:nil];

//activate the audio session
[session setActive:YES error:nil];

//set input to usb
for (AVAudioSessionPortDescription *destPort in session.availableInputs){
    if ([destPort.portType isEqualToString:AVAudioSessionPortUSBAudio]) {
        [setPreferredInput:(AVAudioSessionPortDescription *)inPort
                error:(nil)outError
                session setPreferredInput:destPort error:nil];
    }
}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c avfoundation ios avaudiosession

16
推荐指数
1
解决办法
3808
查看次数

Swift 4.2中的AVAudioSession setCategory可用性

迁移到Swift 4.2后,我遇到了多个错误,其中一个很奇怪.这似乎是Xcode 10中的一个错误,但有没有可用的解决方法?

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, with: options)
} catch {
    NSLog("Could not set audio session category")
}
Run Code Online (Sandbox Code Playgroud)

****'setCategory(_:with :)'在Swift中不可用

xcode avaudiosession swift xcode10 swift4.2

16
推荐指数
1
解决办法
6569
查看次数

在iOS中实现和排除背景音频

StackOverflow上有很多关于iOS背景音乐播放的问题.没有完全探索所有边缘情况,这个问题的目的是成为iOS背景音频问题的最后一个词

定义和假设

所有代码,问题和示例均参考.

"背景" - 当用户按下主页按钮或电源按钮时应用程序的状态(因此设备显示锁定屏幕).该应用程序还可以使用多任务切换器或iPad上的多任务手势进入后台.

"audio" - 使用AudioQueue播放的音频(包括AVAudioPlayer)

先决条件

据我了解,有一个要求让应用程序在后台播放音频.

  1. 设置UIBackgroundModesaudioInfo.plist
  2. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

要求

我的用例是在后台播放相对较长的音频(音乐).可能有数百个曲目,应用程序将按顺序播放它们.可以认为音频将无限播放.

该应用程序将通过暂停播放来处理中断.

问题

我的成功喜忧参半:

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:...];
Run Code Online (Sandbox Code Playgroud)

允许音频在后台播放.但是我很困惑它是否需要以及它与以下内容的区别:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
Run Code Online (Sandbox Code Playgroud)

边缘案例

  1. 中断.如果您注册成为音频中断(电话等)的通知,则成为其代表AVAudioPlayer.例如,如果您在中断开始时暂停或停止音频,则在结束时恢复,如果中断超过10分钟(后台任务完成的最长时间),您的应用程序将被暂停?

  2. 如果调用Lock或Home,模拟器将停止音频,同时使用:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    Run Code Online (Sandbox Code Playgroud)

但是这适用于设备.这是一个已知的问题?

iphone avaudioplayer audioqueue avaudiosession ios5

15
推荐指数
1
解决办法
2498
查看次数

在Mojave上更新为Xcode 10.1,现在我的应用程序总是要求使用麦克风

我正在运行AVAudioSession,我怀疑它正在激活麦克风请求,但我只将它用于声音和音乐.在macOS Mojave上运行Xcode 10.1 Simulator时,为什么要激活访问麦克风的请求?我还没有写过一行甚至要求访问它.我的应用程序不需要访问它也不需要它.这是播放音乐的代码:

AVAudioSession *title = [AVAudioSession sharedInstance];
[title setCategory:AVAudioSessionCategoryAmbient error:nil];
[title setActive:YES error:nil];


//MUSIC
NSString *path = [[NSBundle mainBundle] pathForResource:@"music_nature" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
titleMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];

titleMusic.numberOfLoops = -1;
[titleMusic setVolume:1.0];
[titleMusic play];
Run Code Online (Sandbox Code Playgroud)

如果我注释掉上面的代码,我将不再收到麦克风的请求.但后来我没有任何音乐.

示例项目:https://github.com/Coeur/StackOverflow53109911
(您需要macOS Mojave来重现该问题)

屏幕截图演示错误消息

xcode ios avaudiosession ios-simulator macos-mojave

15
推荐指数
1
解决办法
1068
查看次数

使用音频会话录制时如何降低录制噪音?

我有一些录音代码正常工作,但录制的音频(来自iPod touch内置麦克风)非常嘈杂.

这是我的配置:

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
    if (err) {
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }
    [audioSession setActive:YES error:&err];
    err = nil;
    if (err) {
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }

    recordSetting = [[NSMutableDictionary alloc] init];

    // We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
    [recordSetting setValue:[NSNumber kAudioFormatLinearPCM] forKey:AVFormatIDKey];

    // We can use 44100, …
Run Code Online (Sandbox Code Playgroud)

audio core-audio avaudiorecorder ios avaudiosession

14
推荐指数
1
解决办法
1879
查看次数

不录制时在iOS应用程序中隐藏红色录制状态栏

当应用程序在后台并且没有录制时,我无法将红色"录制"状态栏隐藏在我的应用程序中.

在此输入图像描述

我碰巧使用了The Amazing Audio Engine,但我认为这个问题可以解决这个库的问题.它设置如下:

audioController = [[AEAudioController alloc] initWithAudioDescription:desc inputEnabled:YES];
audioController.audioSessionCategory = kAudioSessionCategory_MediaPlayback;
Run Code Online (Sandbox Code Playgroud)

当用户想要录制时,我打开麦克风就像这样:

[audioController addInputReceiver:mic];
audioController.audioSessionCategory = kAudioSessionCategory_PlayAndRecord;
Run Code Online (Sandbox Code Playgroud)

当用户想要停止录制时,我将其关闭:

[audioController removeInputReceiver:mic];
audioController.audioSessionCategory = kAudioSessionCategory_MediaPlayback;
Run Code Online (Sandbox Code Playgroud)

问题是,当应用程序未录制且用户离开应用程序时,红色的"录制"状态栏仍会显示.我无法停止/处置audioController,因为应用程序可能仍在播放音频.

如果我没有录制,我不希望红色录制状态栏显示.任何想法如何做到这一点?

更新

我设置了以下代码块,在我的应用程序中每2秒运行一次.

audioController.audioSessionCategory = kAudioSessionCategory_MediaPlayback;

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError* error = nil;
[audioSession setActive:NO error: &error];
NSLog(@"error: %@", error);
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
Run Code Online (Sandbox Code Playgroud)

这个日志:

TAAE: Setting audio session category to MediaPlayback
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput)    result 2003329396 77686174 what
Error Domain=NSOSStatusErrorDomain Code=560030580 "The operation couldn’t be completed. (OSStatus error 560030580.)" …
Run Code Online (Sandbox Code Playgroud)

audio objective-c ios avaudiosession

14
推荐指数
1
解决办法
1万
查看次数

在录制时实时上传音频片段?

如何在录制时将音频片段实时上传到服务器?基本上我的要求是在录制时将音频剪辑上传为chucks/packets.我已经使用IQAudioRecorderController https://github.com/hackiftekhar/IQAudioRecorderController完成了录制部分.它记录音频并保存到TemporaryDirectory.

我想知道如何在不保存音频片段的情况下实时上传.

这是录音部分

//Unique recording URL


NSString *fileName = [[NSProcessInfo processInfo] globallyUniqueString];
_recordingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.m4a",fileName]];


// Initiate and prepare the recorder

_audioRecorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:_recordingFilePath] settings:recordSetting error:nil];
_audioRecorder.delegate = self;
_audioRecorder.meteringEnabled = YES;


// Recording start
- (void)recordingButtonAction:(UIBarButtonItem *)item
{
 if (_isRecording == NO)
  {
    _isRecording = YES;

    //UI Update
    {
        [self showNavigationButton:NO];
        _recordButton.tintColor = _recordingTintColor;
        _playButton.enabled = NO;
        _trashButton.enabled = NO;
    }

    /*
     Create the recorder
     */
    if ([[NSFileManager defaultManager] fileExistsAtPath:_recordingFilePath])
    {
        [[NSFileManager …
Run Code Online (Sandbox Code Playgroud)

objective-c avaudiorecorder ios avaudiosession

14
推荐指数
1
解决办法
1107
查看次数

在接听来电后无法重新开始录制

我在录制音频时为中断通知添加了一个观察者.

这在执行拨出电话,接听来电而不是应答,Siri等时工作正常.

现在我的应用程序在后台运行,屏幕顶部有红色条,继续上述状态下的录制不是问题.

但是当我真正接听来电时.我收到另一个AVAudioSessionInterruptionTypeBegan通知,然后当我停止通话时,我从未收到通知AVAudioSessionInterruptionTypeEnded类型.

我已经尝试使用CTCallCenter来检测呼叫何时开始,但我无法从该回调重新开始录制.

有谁知道如何让中断机制与实际得到回答的来电一起工作?

这是我正在使用的代码(的一部分);

CFNotificationCenterAddObserver(
                CFNotificationCenterGetLocalCenter(),
                this,
                &handle_interrupt,
                (__bridge CFStringRef) AVAudioSessionInterruptionNotification,
                NULL,
                CFNotificationSuspensionBehaviorDeliverImmediately );
Run Code Online (Sandbox Code Playgroud)

...

static void handle_interrupt( CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo )
{
    au_recorder *recorder = static_cast<au_recorder*>( observer );

    NSNumber* interruptionType = [( ( __bridge  NSDictionary* ) userInfo ) objectForKey:AVAudioSessionInterruptionTypeKey];
    switch ( [interruptionType unsignedIntegerValue] )
    {
        case AVAudioSessionInterruptionTypeBegan:
        {
            // pause recorder without stopping recording (already done by OS)
            recorder->pause();
            break; …
Run Code Online (Sandbox Code Playgroud)

core-audio objective-c++ audiounit ios avaudiosession

14
推荐指数
1
解决办法
624
查看次数