我正在创建一个音乐应用程序,用户可以在其中访问ios音乐库并将歌曲保存到其应用程序(即文档目录)。我可以使用MPMediaPickerController访问音乐库,但不知道如何处理其委托方法,以便将选定的歌曲保存到我的文档目录。 目前我正在使用此代码
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
didPickMediaItems: (MPMediaItemCollection *) collection
{
[self dismissViewControllerAnimated:YES completion:nil];
[self playSelectedMediaCollection: collection];
}
- (void) playSelectedMediaCollection: (MPMediaItemCollection *) collection {
if (collection.count == 1) {
NSArray *items = collection.items;
MPMediaItem *mediaItem = [items objectAtIndex:0];
[self mediaItemToData:mediaItem];
}
}
-(void)mediaItemToData:(MPMediaItem*)mediaItem
{
// Implement in your project the media item picker
MPMediaItem *curItem = mediaItem;//musicPlayer.nowPlayingItem;
NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
presetName: AVAssetExportPresetPassthrough];
exporter.outputFileType = @"public.mpeg-4";
NSString *exportFile = [[self myDocumentsDirectory] stringByAppendingPathComponent:
@"exported.mp4"];
NSURL *exportURL = [NSURL fileURLWithPath:exportFile] ;
exporter.outputURL = exportURL;
NSData *data = [NSData dataWithContentsOfFile: [[self myDocumentsDirectory]
stringByAppendingPathComponent: @"exported.mp4"]];
//
// NSLog(@"%@",data);
// NSURL *audioUrl = exportURL;
// NSLog(@"Audio Url=%@",audioUrl);
// audioData = [NSData dataWithContentsOfURL:audioUrl];
// NSLog(@"%@",audioData);
// Do with data something
// do the export
// (completion handler block omitted)
[exporter exportAsynchronouslyWithCompletionHandler:
^{
// int exportStatus=exporter.status;
NSLog(@"%d export status",exporter.status);
if (exporter.status==AVAssetExportSessionStatusCompleted)
{
NSLog(@"successfull");
}
NSData *data = [NSData dataWithContentsOfFile: [[self myDocumentsDirectory]
stringByAppendingPathComponent: @"exported.mp4"]];
NSURL *audioUrl = exportURL;
NSLog(@"Audio Url=%@",audioUrl);
audioData = [NSData dataWithContentsOfURL:audioUrl];
NSLog(@"%@",audioData);
// Do with data something
}];
}
在上面的代码中,调试器永远不会出现在导出会话异步块中。如果上述代码中有任何修改,或者您有任何可用的代码可用于我的要求,请告诉我。 谢谢......
答案 0 :(得分:1)
我认为错过了
/
试试这段代码
NSString *exportFile = [[self myDocumentsDirectory] stringByAppendingPathComponent: @"/exported.mp4"];
<强>已更新强>
或原因可以是您使用的预设名称
/ *此导出选项将导致所有轨道的媒体完全按照源资源中的存储方式传递到输出,但 通道无法通过的轨道,通常是由于指定的outputFileType指示的容器格式的约束。 此选项不包含在-allExportPresets和-exportPresetsCompatibleWithAsset返回的数组中。 * / AVF_EXPORT NSString * const AVAssetExportPresetPassthrough NS_AVAILABLE(10_7,4_0);
这里有关于exportAsynchronouslyWithCompletionHandler的良好描述:https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAssetExportSession_Class/Reference/Reference.html
答案 1 :(得分:0)
适用于Swift 3.0或4
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
mediaPicker.dismiss(animated: true) {
print("You selected \(mediaItemCollection)")
let item: MPMediaItem = mediaItemCollection.items[0]
let pathURL: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
if pathURL == nil {
print("Picking Error")
return
}
// get file extension andmime type
let str = pathURL!.absoluteString
let str2 = str.replacingOccurrences( of : "ipod-library://item/item", with: "")
let arr = str2.components(separatedBy: "?")
var mimeType = arr[0]
mimeType = mimeType.replacingOccurrences( of : ".", with: "")
// Export the ipod library as .m4a file to local directory for remote upload
let exportSession = AVAssetExportSession(asset: AVAsset(url: pathURL!), presetName: AVAssetExportPresetAppleM4A)
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.outputFileType = AVFileTypeAppleM4A
let documentURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let outputURL = documentURL.appendingPathComponent("custom.m4a")
//Delete Existing file
do {
try FileManager.default.removeItem(at: outputURL)
} catch let error as NSError {
print(error.debugDescription)
}
exportSession?.outputURL = outputURL
exportSession?.exportAsynchronously(completionHandler: { () -> Void in
if exportSession!.status == AVAssetExportSessionStatus.completed {
print("Export Successfull")
}
})
}
}