如何在iPhone设备上使用AVAudioSessionCategoryMultiRoute? [英] How to use AVAudioSessionCategoryMultiRoute on iPhone device?
问题描述
我想使用AVAudioSessionCategoryMultiRoute,但遗憾的是苹果开发中心和谷歌都没有例子。
如何使用/实现AVAudioSessionCategoryMultiRoute以便在iPhone ios7.0.4上定义2条不同的路由?
我的目标是通过扬声器和耳机传输音频。 (我知道过去是不可能的,但我想尝试使用ios7)
I would like to use AVAudioSessionCategoryMultiRoute, but unfortunately there is no example on apple dev center nor Google. How to use/implement AVAudioSessionCategoryMultiRoute in order to define 2 distinct routes on an iPhone ios7.0.4 ? My goal is to route audio both through speaker and headphones. (I know that is was not possible in the past, but I would like to try with ios7)
感谢您的帮助,
推荐答案
这有助于我: AVAudioEngine and Multiroute ,以及: iOS中的音频会话和多路径音频 。
This help me: AVAudioEngine and Multiroute, and this: Audio Session and Multiroute Audio in iOS.
在我的情况下,我使用两种方法来实现:
In my case, I use two methods to implement:
首先请求MultiRoute类别
first,request the MultiRoute category
[_session setCategory:AVAudioSessionCategoryMultiRoute error:nil];
方法1:AVAudioPlayer的设置channelAssignments:
method1: setup channelAssignments of 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];
}
方法2:自定义渠道地图
method2: custom channel map
// 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];
它对我有用。
这篇关于如何在iPhone设备上使用AVAudioSessionCategoryMultiRoute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!