objective c - Detecting active AVAudioSessions on iOS device -
i'm trying figure out if possible - app activates audio session initialized as:
[[[avaudiosession alloc] init] setcategory:avaudiosessioncategoryplayback withoptions:avaudiosessioncategoryoptionmixwithothers error:&error];
i able understand when additional audio session originated app or os playing.
i know ability implement delegate methods begininterruption:
, endinterruption
these won't invoked because of avaudiosessioncategoryoptionmixwithothers
option i'm using.
is there way achieve without using private api?
thanks in advance.
the way manage application's audio session has had significant changes since ios 6.0, , deserves brief mention first. before ios 6.0 make use of avaudiosession , audiosessionservices classes, incorporating delegation , property listening respectively. ios 6.0 onwards use avaudiosession class , incorporate notifications.
the following ios 6.0 onwards.
to tell if other audio outside applications sandbox playing use -
// query if other audio playing bool isplayingwithothers = [[avaudiosession sharedinstance] isotheraudioplaying]; // test with... (isplayingwithothers) ? nslog(@"other audio playing") : nslog(@"no other audio playing");
as interruption handling you'll need observe avaudiosessioninterruptionnotification
, avaudiosessionroutechangenotification
. in class manages audio session put following - should called once @ start of application lifecycle , don't forget remove observer in dealloc method of same class.
// ensure have singleton object [avaudiosession sharedinstance]; // register notifications [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(interruption:) name:avaudiosessioninterruptionnotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(routechange:) name:avaudiosessionroutechangenotification object:nil];
and add following selectors interruption:
, routechange:
- these receive nsnotification
object has property called userinfo of type nsdictionary
read assist conditionals application has.
- (void)interruption:(nsnotification*)notification { // user info dictionary nsdictionary *interuptiondict = notification.userinfo; // avaudiosessioninterruptiontypekey enum dictionary nsinteger interuptiontype = [[interuptiondict valueforkey:avaudiosessioninterruptiontypekey] integervalue]; // decide based on interruption type here... switch (interuptiontype) { case avaudiosessioninterruptiontypebegan: nslog(@"audio session interruption case started."); // fork handling method here... // eg:[self handleinterruptionstarted]; break; case avaudiosessioninterruptiontypeended: nslog(@"audio session interruption case ended."); // fork handling method here... // eg:[self handleinterruptionended]; break; default: nslog(@"audio session interruption notification case default."); break; } }
and similarly...
- (void)routechange:(nsnotification*)notification { nsdictionary *interuptiondict = notification.userinfo; nsinteger routechangereason = [[interuptiondict valueforkey:avaudiosessionroutechangereasonkey] integervalue]; switch (routechangereason) { case avaudiosessionroutechangereasonunknown: nslog(@"routechangereason : avaudiosessionroutechangereasonunknown"); break; case avaudiosessionroutechangereasonnewdeviceavailable: // headset added or removed nslog(@"routechangereason : avaudiosessionroutechangereasonnewdeviceavailable"); break; case avaudiosessionroutechangereasonolddeviceunavailable: // headset added or removed nslog(@"routechangereason : avaudiosessionroutechangereasonolddeviceunavailable"); break; case avaudiosessionroutechangereasoncategorychange: // called @ start - when other audio wants play nslog(@"routechangereason : avaudiosessionroutechangereasoncategorychange");//avaudiosessionroutechangereasoncategorychange break; case avaudiosessionroutechangereasonoverride: nslog(@"routechangereason : avaudiosessionroutechangereasonoverride"); break; case avaudiosessionroutechangereasonwakefromsleep: nslog(@"routechangereason : avaudiosessionroutechangereasonwakefromsleep"); break; case avaudiosessionroutechangereasonnosuitablerouteforcategory: nslog(@"routechangereason : avaudiosessionroutechangereasonnosuitablerouteforcategory"); break; default: break; } }
there no need poll long check state of applications audio session example in viewdidload
of root view controller, @ start of apps lifecycle. changes there onwards applications audio session known via these 2 main notifications. replace nslog
statements ever code needs based on cases contained in switch.
you can find more information avaudiosessioninterruptiontypekey
, avaudiosessionroutechangereasonkey
in avaudiosession
class reference documentation.
my apologies long answer think audio session management in ios rather fiddly , apple's audio session programming guide, @ time of writing this, not include code examples using notifications interruption handling.
Comments
Post a Comment