ios - How to Implementing MPNowPlayingInfoCenter using AvAudioPlayer -


i in midst of creating ios mobile app client play variety of audio tracks.

one of features wanted implement display information currently-playing audio track on lock screen , banner . 1 of simple convenience mobile user , must-have if app has background audio playing. personally, use feature time!

you should first observe avplayeritem of avaudioplayer so:

[playeritem addobserver:self forkeypath:@"timedmetadata" options:nskeyvalueobservingoptionnew context:nil]; 

then create global variables:

nsstring *title; nsstring *artist; uiimage *artwork; 

you need function 1 below, observe key path timedmetadata , update infocenter through updateinfocenterwithtitle:andartist:andcover:.

- (void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary  *)change context:(void *)context {     if ([keypath isequaltostring:@"timedmetadata"])     {         (int = 0; < [audioplayer.currentitem.timedmetadata count]; i++)         {             avmetadataitem *metadata = [audioplayer.currentitem.timedmetadata objectatindex:i];              if ([[metadata commonkey] isequaltostring:avmetadatacommonkeyartist]) {                 artist = (nsstring *)metadata.value;             }             else if ([[metadata commonkey] isequaltostring:avmetadatacommonkeytitle])             {                 title = (nsstring *)metadata.value;                 [self updateinfocenterwithtitle:title andartist:artist andcover:artwork];             }             else if ([[metadata commonkey] isequaltostring:avmetadatacommonkeyartwork])             {                 if ([metadata.keyspace isequaltostring:avmetadatakeyspaceid3])                 {                     nsdictionary *dictionary = [metadata.value copywithzone:nil];                     artwork = [uiimage imagewithdata:[dictionary objectforkey:@"data"]]];                 } else if ([metadata.keyspace isequaltostring:avmetadatakeyspaceitunes]) {                     artwork = [uiimage imagewithdata:[metadata.value copywithzone:nil]];                 }             }             else {                 nslog(@"%@ --> %@", [metadata commonkey], metadata.value);             }         }     } } 

this magic happens:

- (void)updateinfocenterwithtitle:(nsstring *)title andartist:(nsstring *)artist andcover:(uiimage *)cover {     if (cover == nil) {         cover = [uiimage imagenamed:@"defaultcover"];     }      mpnowplayinginfocenter *infocenter = [mpnowplayinginfocenter defaultcenter];     [infocenter setnowplayinginfo:@{mpmediaitempropertytitle:title,                                     mpmediaitempropertyartist:artist,                                     mpmediaitempropertyartwork:[[mpmediaitemartwork alloc] initwithimage:cover]}]; } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -