我们可以使用来自使用MPMediaPickerController
获取的iTunes的音频并将其上传到服务器吗?我已成功将录制的音频上传到服务器,没有任何问题。但是从MPMediaPickerController
收到资产网址时,我无法将其上传到服务器。
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
MPMediaItem *theChosenSong = [[mediaItemCollection items]objectAtIndex:0];
NSString *songTitle = [theChosenSong valueForProperty:MPMediaItemPropertyTitle];
NSLog(@"song Title: %@", songTitle);
NSURL *assetURL = [theChosenSong valueForProperty:MPMediaItemPropertyAssetURL];
NSLog(@"assetURL: %@", assetURL);
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
NSURL *testURl = theChosenSong.assetURL;
[self serviceUploadAudio:testURl];
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:0)
MPMediaItemPropertyAssetURL
是ipod-library的私有URL,它指向基于URL的AVFoundation对象,因此您无法直接将此url对象上载到服务器。
您必须先将此MPMediaItem
导出到文档目录或临时目录,然后再上传到服务器。
在Swift 3中
// MARK:- Convert itunes song to avsset in temp location Methode.
func exportiTunesSong(assetURL: URL, completionHandler: @escaping (_ fileURL: URL?) -> ()) {
let songAsset = AVURLAsset(url: assetURL, options: nil)
let exporter = AVAssetExportSession(asset: songAsset, presetName: AVAssetExportPresetAppleM4A)
exporter?.outputFileType = "com.apple.m4a-audio"
exporter?.metadata = songAsset.commonMetadata
let filename = AVMetadataItem.metadataItems(from: songAsset.commonMetadata, withKey: AVMetadataCommonKeyTitle, keySpace: AVMetadataKeySpaceCommon)
var songName = "Unknown"
if filename.count > 0 {
songName = ((filename[0] as AVMetadataItem).value?.copy(with: nil) as? String)!
}
//Export mediaItem to temp directory
let exportURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(songName)
.appendingPathExtension("m4a")
exporter?.outputURL = exportURL
// do the export
// (completion handler block omitted)
exporter?.exportAsynchronously(completionHandler: {
let exportStatus = exporter!.status
switch (exportStatus) {
case .failed:
let exportError = exporter?.error
print("AVAssetExportSessionStatusFailed: \(exportError)")
completionHandler(nil)
break
case .completed:
print("AVAssetExportSessionStatusCompleted")
completionHandler(exportURL)
break
case .unknown:
print("AVAssetExportSessionStatusUnknown")
break
case .exporting:
print("AVAssetExportSessionStatusExporting")
break
case .cancelled:
print("AVAssetExportSessionStatusCancelled")
completionHandler(nil)
break
case .waiting:
print("AVAssetExportSessionStatusWaiting")
break
}
})
}
此函数返回导出的MPMediaItem
的临时目录URL,您可以将其上传到服务器。
self.exportiTunesSong(assetURL: index as! URL, completionHandler: { (filePath) in
if (filePath != nil) {
//Upload mediaItem to server by filePath
[self serviceUploadAudio:filePath!];
}
})
目标 - C
#pragma mark Convert itunes song to avsset in temp location Methode.
-(void)exportiTunesSong:(NSURL*)assetURL fileURL:(void(^)(id))completionHandler {
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
AVAssetExportSession *exporter = [AVAssetExportSession exportSessionWithAsset:songAsset presetName:AVAssetExportPresetAppleM4A];
exporter.outputFileType = @"com.apple.m4a-audio";
exporter.metadata = songAsset.commonMetadata;
NSArray *filename = [AVMetadataItem metadataItemsFromArray:songAsset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon];
NSString *songName = @"Unknown";
if (filename.count > 0) {
AVMetadataItem *title = [filename objectAtIndex:0];
songName = [title.value copyWithZone:nil];
}
NSURL *exportURL = [[[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:songName] URLByAppendingPathExtension:@"m4a"];
exporter.outputURL = exportURL;
// do the export
// (completion handler block omitted)
[exporter exportAsynchronouslyWithCompletionHandler:^{
switch (exporter.status) {
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed: %@",exporter.error);
completionHandler(nil);
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"AVAssetExportSessionStatusCompleted");
completionHandler(exportURL);
break;
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled: %@",exporter.error);
completionHandler(nil);
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
}
}];
}
[self exportiTunesSong:assetURL fileURL:^(id filepath) {
if (filepath != nil) {
[self serviceUploadAudio:filepath];
}
}];