d0ping
d0ping

Reputation: 472

How to set metadata of sound track (artwork, album, etc.) on the lock screen iOS?

I'm trying to develop a simple audio player app for iPhone or iPad. I'm made working on the background mode and controlling playback on the lock screen, but I can't set data about current music track (artwork, album, etc.) on the lock screen like it work on default iOS music player. How can i accomplish this? Any help is appreciated in advance, Thank You.

Upvotes: 2

Views: 1506

Answers (2)

Ramani Hitesh
Ramani Hitesh

Reputation: 214

 //  set Audio file metadata  Modify and get //

AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
session.outputURL =  [NSURL fileURLWithPath:_audioPath];
session.outputFileType = AVFileTypeAppleM4A;

AVMutableMetadataItem *Titlenm = [[AVMutableMetadataItem alloc] init];
Titlenm.identifier = AVMetadataCommonIdentifierTitle;
Titlenm.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;     // more in CoreMedia/CMMetadata.h

Titlenm.value =@" Enter Audio Title";



AVMutableMetadataItem *Artistnm = [[AVMutableMetadataItem alloc] init];
Artistnm.identifier = AVMetadataCommonIdentifierArtist;
Artistnm.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;
Artistnm.value  =@" Enter Audio ArtistName";


AVMutableMetadataItem *AlbumName = [[AVMutableMetadataItem alloc] init];
AlbumName.identifier = AVMetadataCommonIdentifierAlbumName;
AlbumName.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;
AlbumName.value =@" Enter Audio AlbumName";;



AVMutableMetadataItem *dateset = [[AVMutableMetadataItem alloc] init];
dateset.identifier = AVMetadataCommonIdentifierCreationDate;
dateset.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;
dateset.value = self.datelbl.text;




AVMutableMetadataItem *audioArtwork = [[AVMutableMetadataItem alloc]init];

//self.audioimg.image in this line set image
audioArtwork.value = UIImagePNGRepresentation(self.audioimg.image);

audioArtwork.dataType = (__bridge NSString * _Nullable)(kCMMetadataBaseDataType_PNG);
audioArtwork.identifier = AVMetadataCommonIdentifierArtwork;
audioArtwork.extendedLanguageTag =@"und";

session.metadata = @[Titlenm, AlbumName,Artistnm,dateset,audioArtwork];

[session exportAsynchronouslyWithCompletionHandler:
 ^{

     if (session.status == AVAssetExportSessionStatusCompleted)
     {

         AVAsset *asset = [AVURLAsset URLAssetWithURL:outputURL options:nil];
         NSArray *metadata = [asset commonMetadata];






        finalurl=outputURL;


         for(AVMetadataItem* item in metadata)
         {
             NSString *key = [item commonKey];
             NSString *value = [item stringValue];
             NSLog(@"key = %@, value = %@", key,value);
         }



         [self performSelectorOnMainThread:@selector(myviewpresent) withObject:self waitUntilDone:YES];

     }

 }];

Upvotes: 1

d0ping
d0ping

Reputation: 472

I solved this problem. In order to show the metadata about sound track (artwork, album, title, etc.) on the lock screen iOS you should use MPNowPlayingInfoCenter class. For example:

    MPMediaItemArtwork *albumArtwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"artwork.png"]];
    NSDictionary *nowPlayingInfo = @{MPMediaItemPropertyTitle: @"Title",
                                     MPMediaItemPropertyArtist: @"Artist",
                                     MPMediaItemPropertyAlbumTitle: @"Album",
                                     MPMediaItemPropertyArtwork: albumArtwork};
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlayingInfo];

Upvotes: 5

Related Questions