0
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

//Direct audio to speakers when there is no headphone
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];


[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[player setAllowsAirPlayVideo:NO];

The above is my code trying to set the appropriate session to enable Airplay. It works sometimes, but regardless of whether it works or not, it usually lists 'iPhone' twice please see the attached image in the Airplay menu, sometimes not showing the actual Airplay device. Tapping either of those two duplicate options does not seem to do anything. Also the play icon on the status bar sometimes appears, and sometimes it doesn't. I am guessing the session is not getting set properly every time.

Can anybody kindly tell me what I am doing wrong here?! Also, if I want to enable playing over Bluetooth, do I need to implement some other delegate?

avikbagh
  • 43
  • 1
  • 7

2 Answers2

1

I see your post today, using your code in this mode:

In your AppDelegate .M this:

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
[[AVAudioSession sharedInstance] setActive:NO error:nil];

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

And in your View.m using this:

[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[player setAllowsAirPlayVideo:NO];//this is deprecated in iOS 6.0

Hope this help you or future people!

BlackSheep
  • 1,087
  • 12
  • 29
  • Yes i figured that out already, should have updated it here. The funny part is I cannot get it to work without using the deprecated method, even on iOS 6.0 – avikbagh Apr 12 '13 at 08:12
  • I see is this [player setAllowsAirPlayVideo:NO];//this is deprecated in iOS 6.0 right? – BlackSheep Apr 17 '13 at 07:49
0

I see but is simple and strange in same time :) you have to only remove the funcion.

this method:

[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[player setAllowsAirPlayVideo:NO];//this is deprecated in iOS 6.0

become to:

[player setAllowsExternalPlayback:YES];
[player setUsesExternalPlaybackWhileExternalScreenIsActive: YES];

in your info.plist allow Required Background Modes a and set: App Plays Audio and App Communicates with an accessory, then add one row and set: Application uses WiFi.

That's that's all ;)

PS: is possibile you can helpme on my problem here thanks.

Community
  • 1
  • 1
BlackSheep
  • 1,087
  • 12
  • 29
  • 1
    PPS if you want to allow you app from iOS 6.x and less you have to setting the info plist and do not remove the line [player setAllowsAirPlayVideo:NO]; because is deprecated on iOS 6.0 and later but is necessary on is 5.x ext. – BlackSheep Apr 17 '13 at 08:15