【问题标题】:How to thumbnail image show when audio file play in background (IOS)?音频文件在后台播放时如何缩略图显示(IOS)?
【发布时间】:2016-12-05 06:32:04
【问题描述】:

我正在使用 MPMediaPickerController 从媒体库中选择音频文件。代码是:

    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 的音频文件。这个音频 url 使用 AVQueuePlayer 播放(因为我必须一个一个播放多个音频)。

对于背景音频播放,我必须在 AppDelegate.m 中编写代码,即:

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

一切运行良好,但问题是背景音乐播放时未显示音频缩略图。只显示播放控制,时长和音乐文件名,默认音乐缩略图显示,但我想根据音频文件显示缩略图。

【问题讨论】:

    标签: ios objective-c audio


    【解决方案1】:

    您可以将此代码添加到锁屏图片和经过的时间。

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

    【讨论】:

    • 这可以在 AVQueuePlayer 或 AVplayer 中实现吗??
    • 希望因为它与 avplayer 没有依赖关系,只是您必须更改总持续时间和经过的时间
    猜你喜欢
    • 2017-11-09
    • 2021-12-30
    • 1970-01-01
    • 2011-07-13
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多