简体   繁体   English

如何将nsdata转换为MPMediaitem歌曲iOS SDK

[英]how to convert nsdata to MPMediaitem song iOS Sdk

i converted MPMediaItem to NSData and stored that in database, now i have to convert that NSData to MPMediaItem to play that song in mediaplayer. 我将MPMediaItem转换为NSData并将其存储在数据库中,现在我必须将该NSData转换为MPMediaItem才能在mediaplayer中播放该歌曲。 Used below code to convert MPMediaItem to NSData: 以下代码用于将MPMediaItem转换为NSData:

NSURL *url = [song valueForProperty: MPMediaItemPropertyAssetURL];

AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
                                                                  presetName: AVAssetExportPresetPassthrough];

exporter.outputFileType = @"public.mpeg-4";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *exportFile = [documentsDirectory stringByAppendingPathComponent:           
                        @"exported.mp4"];

NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
exporter.outputURL = exportURL; 

// do the export
// (completion handler block omitted) 
[exporter exportAsynchronouslyWithCompletionHandler:
 ^{
     NSData *data = [NSData dataWithContentsOfFile: [documentsDirectory 
                                                     stringByAppendingPathComponent: @"exported.mp4"]];


 }];

can anyone please tell me how to convert NSData to MPMediaItem. 谁能告诉我如何将NSData转换为MPMediaItem。

You can't. 你不能 An MPMediaItem represents an item in the iPod library but third-party apps cannot add anything to that library. MPMediaItem表示iPod库中的一个项目,但是第三方应用程序无法向该库添加任何内容。

You could however write your NSData object to a file and then use its URL with either AVPlayerItem / AVPlayer or MPMoviePlayerController (don't be fooled by the name, it can also play audio-only files). 但是,您可以将NSData对象写入文件,然后将其URL与AVPlayerItem / AVPlayerMPMoviePlayerController (不要被名称所迷惑,它也可以播放仅音频文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM