iOS: Airplay picker MPVolumeView alternative

拈花ヽ惹草 提交于 2019-12-05 12:19:22
ambientlight

So the precise answer to my question:

(i) It's not possible to switch audioRoutes programatically with public API except switching to build-in speakers.

[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];

(ii) You can only retrieve a name of airplay-device if it's active AudioRoute. Get name of AirPlay device using AVPlayer

So the practical solution to presenting customised UI Controls for selecting airplay would be:

To customise MPVolumeView, where you can disable volumeSliderand customise routeButton. However you have no other option as picking airplayDevice among list of apple-compatible wireless devices (airPlay, bluetooth, etc) in UIActionSheet that pop ups when you tap on routeButton, but you can observe when user will make a selection there by subscribing to audioRouteChangeNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteHasChangedNotification:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];

(also note that if you will plug in/out headphones, it also will trigger this notification)

If you are interested how to retrieve all available audioRoutes and switch programmatically with private API:

MPMediaPlayer framework contains a private class MPAVRoutingController, which allows you exactly that:

Class MPAVRoutingController = NSClassFromString(@"MPAVRoutingController");
Class MPAVRoute = NSClassFromString(@"MPAVRoute");

id routingController = [[MPAVRoutingController alloc] init];
NSArray* availableRoutes = [routingController performSelector:@selector(availableRoutes)];
BOOL isSwitchSuccesful = [[routingController performSelector:@selector(pickRoute:) withObject:availableRoutes.lastObject] boolValue];

(if you want to then access audioRoute info and check if it is Airplay: Detecting airplayRoute)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!