2014-07-06 10 views
7

मैं AppRTC iOS example में वक्ताओं को ऑडियो रीडायरेक्ट करने का प्रयास कर रहा हूं।ऐपआरटीसी आईओएस उदाहरण में वक्ताओं को ऑडियो रीडायरेक्ट कैसे करें?

मैंने कोशिश की:

AVAudioSession* session = [AVAudioSession sharedInstance]; 

//error handling 
BOOL success; 
NSError* error; 

//set the audioSession category. 
//Needs to be Record or PlayAndRecord to use audioRouteOverride: 

success = [session setCategory:AVAudioSessionCategoryPlayAndRecord 
         error:&error]; 

if (!success) NSLog(@"AVAudioSession error setting category:%@",error); 

//set the audioSession override 
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker 
             error:&error]; 
if (!success) NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error); 

//activate the audio session 
success = [session setActive:YES error:&error]; 
if (!success) NSLog(@"AVAudioSession error activating: %@",error); 
else NSLog(@"audioSession active"); 

कोई त्रुटियां नहीं हैं, लेकिन यह काम नहीं करता। मैं इसे कैसे ठीक करूं?

+0

क्या आपको अपनी समस्या के लिए कुछ समाधान मिला? इस समय आप एक ही समस्या का सामना कर रहे हैं। –

+1

@ जोसिपबी। https://github.com/alongubkin/audiotoggle –

+0

मेरे लिए काम नहीं करता है ... –

उत्तर

0

मुझे अंत में समाधान मिला।

कारण यह था कि आपको AVAudioSession श्रेणी AVAudioSessionCategoryPlayback पर सेट करने की आवश्यकता है। लेकिन वेबआरटीसी कॉल की स्थापना के बाद किसी कारण से इसे AVAudioSessionCategoryPlayAndRecord पर सेट किया गया था। अंत में मैंने AVAudioSessionRouteChangeNotification के लिए पर्यवेक्षक जोड़ने का निर्णय लिया और प्रत्येक बार जब मैंने अवांछित श्रेणी परिवर्तन का पता लगाया तो AVAudioSessionCategoryPlayback पर स्विच करें। कुछ हैक समाधान लेकिन अंत में काम किया। आप इसे here देख सकते हैं।

9

मैंने इसे हल करके हल किया। बस AVAudioSessionRouteChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSessionRouteChange:) name:AVAudioSessionRouteChangeNotification object:nil]; 

सुनो और नीचे के रूप में didSessionRouteChange चयनकर्ता का उपयोग:

- (void)didSessionRouteChange:(NSNotification *)notification 
{ 
    NSDictionary *interuptionDict = notification.userInfo; 
    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; 

    switch (routeChangeReason) { 
     case AVAudioSessionRouteChangeReasonCategoryChange: { 
      // Set speaker as default route 
      NSError* error; 
      [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]; 
     } 
     break; 

    default: 
     break; 
    } 
} 
0

phuongle's answer सही है। यद्यपि जब आप इसे ओवरराइड सक्षम करते हैं तो वास्तव में ऑडियो आउटपुट को ओवरराइड कर देगा, भले ही उपयोगकर्ता हेडफ़ोन में प्लग करे। जब उपयोगकर्ता हेडफोन का उपयोग कर रहा है तो लाउडस्पीकर के माध्यम से ऑडियो खेलने में ज्यादा समझ नहीं है। इस उद्देश्य के लिए निम्नलिखित कोड का उपयोग करें:

- (void)didSessionRouteChange:(NSNotification *)notification 
{ 
    NSDictionary *interuptionDict = notification.userInfo; 
    const NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; 

    if (routeChangeReason == AVAudioSessionRouteChangeReasonRouteConfigurationChange) { 
     [self enableLoudspeaker]; 
    } 
} 

- (void)enableLoudspeaker { 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    AVAudioSessionCategoryOptions options = audioSession.categoryOptions; 
    if (options & AVAudioSessionCategoryOptionDefaultToSpeaker) return; 
    options |= AVAudioSessionCategoryOptionDefaultToSpeaker; 
    [audioSession setActive:YES error:nil]; 
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:options error:nil]; 
} 
संबंधित मुद्दे