amisha.beladiya
amisha.beladiya

Reputation: 363

How to thumbnail image show when audio file play in background (IOS)?

I am using MPMediaPickerController for pick the audio file from media library.code is :

    for (MPMediaItem *item in mediaItemCollection.items) {
            NSLog(@"%@",mediaItemCollection.items);
            NSURL* assetURL2 = [item valueForProperty:MPMediaItemPropertyAssetURL];}
    NSString* ext = [TSLibraryImport extensionForAssetURL:assetURL];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSURL* outURL = [[NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",thumbName]]] URLByAppendingPathExtension:ext];

Now I upload the audio file [outURL path] on server and I get audio file with url. This audio url play using the AVQueuePlayer(Because I have to play multiple audio one by one) .

For background audio play, I have to written code in AppDelegate.m ,that is:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

All is running well, but the problem is that audio thumbnail image doesn't show on background music play. Only show the playback controll,duration and music file name and by default music thumbnail show but I want to show thumbnail according to audio file.

Upvotes: 2

Views: 900

Answers (1)

Return Zero
Return Zero

Reputation: 434

You can add this code for lock screen artwork and elapsed time.

NSMutableDictionary albumInfo = [[NSMutableDictionary alloc] init];
[albumInfo setObject:_item.title forKey:MPMediaItemPropertyTitle];
[albumInfo setObject:_item.username forKey:MPMediaItemPropertyArtist];
int dur=CMTimeGetSeconds( player.currentItem.asset.duration) ;  //total duratiom
int now=CMTimeGetSeconds( [player currentTime]); // time elapsed
MPMediaItemArtwork *albumArt= [[MPMediaItemArtwork alloc] initWithImage: ART_IMAGE];// ART_IMAGE replace with your image name .

[albumInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
 [albumInfo setObject:[NSString stringWithFormat:@"%d",dur] forKey:MPMediaItemPropertyPlaybackDuration];
 [albumInfo setObject:[NSString stringWithFormat:@"%d",now]forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:albumInfo];

Upvotes: 1

Related Questions