2016-12-05 23 views
1

我使用MPMediaPickerController爲挑選從媒體library.code音頻文件是:如何縮略圖顯示音頻文件在後臺播放(IOS)?

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]; 

現在我上傳的音頻文件[outURL路徑]服務器和我得到的URL的音頻文件。使用AVQueuePlayer播放此音頻網址(因爲我必須逐個播放多個音頻)。

有關背景音頻播放,我必須在AppDelegate.m編寫的代碼,那就是:

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

一切都運行良好,但問題是,音頻縮略圖不顯示在背景音樂播放。只顯示回放控制,持續時間和音樂文件名稱,默認情況下音樂縮略圖顯示,但我想根據音頻文件顯示縮略圖。

回答

0

您可以添加此代碼鎖定屏幕圖稿和經過時間。

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]; 
+0

這是可能在AVQueuePlayer或AVplayer? – ABJ

+0

希望如此,因爲它不依賴於avplayer,只需要改變總持續時間和流逝的時間 –