2014-02-17 10 views
9

Ich möchte AVAudioSessionCategoryMultiRoute verwenden, aber leider gibt es kein Beispiel auf Apple Dev Center oder Google. Wie verwende/implementiere ich AVAudioSessionCategoryMultiRoute, um 2 verschiedene Routen auf einem iPhone ios7.0.4 zu definieren? Mein Ziel ist es, Audio sowohl über Lautsprecher als auch über Kopfhörer zu routen. (Ich weiß, das ist nicht möglich, in der Vergangenheit war, aber ich würde gerne mit ios7 versuchen)Wie verwende ich AVAudioSessionCategoryMultiRoute auf dem iPhone?

Vielen Dank für Ihre Hilfe,

Antwort

2

Diese mir helfen: AVAudioEngine and Multiroute, und diese: Audio Session and Multiroute Audio in iOS.

In meinem Fall verwende ich zwei Methoden zu implementieren:

ersten fordert die MultiRoute Kategorie

[_session setCategory:AVAudioSessionCategoryMultiRoute error:nil]; 

method1: setup channelAssignments von AVAudioPlayer:

// 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]; 
    } 

method2: Custom-Channel Karte

// 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]; 

Es funktioniert für mich.

+0

Wie machen Sie eine Kanalzuordnung für Eingänge? Was ist, wenn ich einen externen USB-Mikrofoneingang habe und von Kanal 5 lesen möchte? Es scheint nur kAudioOutputUnitProperty_ChannelMap und keine kAudioInputUnitProperty_ChannelMap zu geben – jaybers