【问题标题】:How to use AVAudioSessionCategoryMultiRoute on iPhone device?如何在 iPhone 设备上使用 AVAudioSessionCategoryMultiRoute?
【发布时间】:2014-02-17 15:17:26
【问题描述】:

我想使用 AVAudioSessionCategoryMultiRoute,但不幸的是,苹果开发中心和谷歌都没有示例。 如何使用/实现 AVAudioSessionCategoryMultiRoute 以在 iPhone ios7.0.4 上定义 2 个不同的路由? 我的目标是通过扬声器和耳机路由音频。 (我知道这在过去是不可能的,但我想用 ios7 试试)

感谢您的帮助,

【问题讨论】:

    标签: ios iphone categories avaudiosession


    【解决方案1】:

    帮帮我:AVAudioEngine and Multiroute,这个:Audio Session and Multiroute Audio in iOS

    就我而言,我使用两种方法来实现:

    首先,请求MultiRoute类别

    [_session setCategory:AVAudioSessionCategoryMultiRoute error:nil];
    

    方法1:设置AVAudioPlayer的channelAssignments:

        // My hardware has 4 output channels
        if (_outputPortChannels.count == 4) {
            AVAudioSessionChannelDescription *desiredChannel1 = [_outputPortChannels objectAtIndex:2];
            AVAudioSessionChannelDescription *desiredChannel2 = [_outputPortChannels objectAtIndex:3];
    
            // Create an array of desired channels
            NSArray *channelDescriptions = [NSArray arrayWithObjects:desiredChannel1, desiredChannel2, nil];
    
            // Assign the channels
            _avAudioPlayer1.channelAssignments = channelDescriptions;
    
            NSLog(@"_player.channelAssignments: %@", _avAudioPlayer1.channelAssignments);
    
            // Play audio to output channel3, channel4
            [_avAudioPlayer1 play];
        }
    

    方法2:自定义频道图

        // Get channel map indices based on user specified channelNames
        NSMutableArray *channelMapIndices = [self getOutputChannelMapIndices:_inChannelNames];
    
        NSAssert(channelMapIndices && channelMapIndices.count > 0, @"Error getting indices for user specified channel names!");
    
        // AVAudioEngine setup
        _engine = [[AVAudioEngine alloc] init];
        _output = _engine.outputNode;
        _mixer = _engine.mainMixerNode;
        _player = [[AVAudioPlayerNode alloc] init];
        [_engine attachNode:_player];
    
        // open the file to play
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"yujian" ofType:@"mp3"];
        NSURL *songURL1 = [NSURL fileURLWithPath:path1];
        _songFile = [[AVAudioFile alloc] initForReading:songURL1 error:nil];
    
        // create output channel map
        SInt32 source1NumChannels = (SInt32)_songFile.processingFormat.channelCount;
    
        // I use constant map
        // Play audio to output channel3, channel4
        SInt32 outputChannelMap[4] = {-1, -1, 0, 1};
    
        // This will play audio to output channel1, channel2
        //SInt32 outputChannelMap[4] = {0, 1, -1, -1};
    
        // set channel map on outputNode AU
        UInt32 propSize = (UInt32)sizeof(outputChannelMap);
        OSStatus err = AudioUnitSetProperty(_output.audioUnit, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Global, 1, outputChannelMap, propSize);
        NSAssert(noErr == err, @"Error setting channel map! %d", (int)err);
    
        // make connections
        AVAudioChannelLayout *channel1Layout = [[AVAudioChannelLayout alloc] initWithLayoutTag:kAudioChannelLayoutTag_DiscreteInOrder | (UInt32)source1NumChannels];
        AVAudioFormat *format1 = [[AVAudioFormat alloc] initWithStreamDescription:_songFile.processingFormat.streamDescription channelLayout:channel1Layout];
        [_engine connect:_player to:_mixer format:format1];
        [_engine connect:_mixer to:_output format:format1];
    
        // schedule the file on player
        [_player scheduleFile:_songFile atTime:nil completionHandler:nil];
    
        // start engine and player
        if (!_engine.isRunning) {
            [_engine startAndReturnError:nil];
        }
    
        [_player play];
    

    它对我有用。

    【讨论】:

    • 如何为输入制作通道映射?如果我有一个外部 USB 麦克风输入并且我想从通道 5 读取数据怎么办?似乎只有 kAudioOutputUnitProperty_ChannelMap 而没有 kAudioInputUnitProperty_ChannelMap
    • 原来输出单元既有输入又有输出。这个音频单元是您使用kAudioOutputUnitProperty_SetInputCallback property 安装输入回调的地方。所以从技术上讲,有两张地图:一张用于输入范围,一张用于输出范围。所以上面的代码应该使用kAudioUnitScope_Output而不是kAudioUnitScope_Global
    猜你喜欢
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多