如何将nsdata转换为MPMediaitem歌曲iOS Sdk [英] how to convert nsdata to MPMediaitem song iOS Sdk

查看:115
本文介绍了如何将nsdata转换为MPMediaitem歌曲iOS Sdk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MPMediaItem转换为NSData并将其存储在数据库中,现在我必须将该NSData转换为MPMediaItem以在mediaplayer中播放该歌曲。
用于将MPMediaItem转换为NSData的代码:

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. Used below code to convert MPMediaItem to 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"]];


 }];

任何人都可以告诉我如何将NSData转换为MPMediaItem。

can anyone please tell me how to convert NSData to MPMediaItem.

推荐答案

你不能。 MPMediaItem 表示iPod库中的项目,但第三方应用程序无法向该库添加任何内容。

You can't. An MPMediaItem represents an item in the iPod library but third-party apps cannot add anything to that library.

您然而,可以将 NSData 对象写入文件,然后将其URL与 AVPlayerItem / 一起使用AVPlayer MPMoviePlayerController (不要被名称愚弄,它也可以播放纯音频文件)。

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转换为MPMediaitem歌曲iOS Sdk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆